Loading...
Searching...
No Matches
PPM.cpp
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Rice University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Rice University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Author: Ioan Sucan */
36
37#include "ompl/util/PPM.h"
38#include "ompl/util/Exception.h"
39#include <cstdio>
40
41void ompl::PPM::loadFile(const char *filename)
42{
43 FILE *fp = fopen(filename, "r");
44 if (fp == nullptr)
45 throw Exception("Unable to load '" + std::string(filename) + "'");
46 struct AutoClose
47 {
48 AutoClose(FILE *f) : f_(f)
49 {
50 }
51 ~AutoClose()
52 {
53 fclose(f_);
54 }
55 FILE *f_;
56 };
57 AutoClose _(fp); // close fp when this variable goes out of scope.
58
59 char p6[2] = {0};
60 if (fscanf(fp, "%c", &p6[0]) != 1 || fscanf(fp, "%c", &p6[1]) != 1 || p6[0] != 'P' || p6[1] != '6')
61 throw Exception("Invalid format for file '" + std::string(filename) +
62 "'. PPM is expected to start with the "
63 "characters 'P6'.");
64 int nc = fgetc(fp);
65 while ((char)nc != '#' && ((char)nc > '9' || (char)nc < '0'))
66 nc = fgetc(fp);
67 if ((char)nc == '#')
68 while ((char)nc != '\n')
69 nc = fgetc(fp);
70 else
71 ungetc(nc, fp);
72 if (fscanf(fp, "%d", &width_) != 1 || fscanf(fp, "%d", &height_) != 1)
73 throw Exception("Unable to parse width and height for '" + std::string(filename) + "'");
74 if (width_ <= 0 || height_ <= 0)
75 throw Exception("Invalid image dimensions for '" + std::string(filename) + "'");
76 if (fscanf(fp, "%d", &nc) != 1 || nc != 255 /* RGB component color marker*/)
77 throw Exception("Invalid RGB component for '" + std::string(filename) + "'");
78 fgetc(fp);
79 nc = width_ * height_ * 3;
80 pixels_.resize(width_ * height_);
81 if ((int)fread(&pixels_[0], sizeof(unsigned char), nc, fp) != nc)
82 throw Exception("Unable to load image data from '" + std::string(filename) + "'");
83}
84
85void ompl::PPM::saveFile(const char *filename)
86{
87 if (pixels_.size() != width_ * height_)
88 throw Exception("Number of pixels is " + std::to_string(pixels_.size()) +
89 " but the set width and height require " + std::to_string(width_ * height_) + " pixels.");
90 FILE *fp;
91 fp = fopen(filename, "wb");
92 if (fp == nullptr)
93 throw Exception("Unable to open '" + std::string(filename) + "' for writing");
94 fprintf(fp, "P6\n");
95 fprintf(fp, "%d %d\n", width_, height_);
96 fprintf(fp, "%d\n", 255); // RGB marker
97 fwrite(&pixels_[0], 3 * width_, height_, fp);
98 fclose(fp);
99}
The exception type for ompl.
Definition Exception.h:47
void loadFile(const char *filename)
Load a .ppm file. Throw an exception in case of an error.
Definition PPM.cpp:41
void saveFile(const char *filename)
Save image data to a .ppm file. Throw an exception in case of an error.
Definition PPM.cpp:85