ppm.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2015, University of Colorado, Boulder
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Univ of CO, Boulder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /*
00036  * PPM Image Format Reader
00037  * Code adapted from http://stackoverflow.com/questions/2693631/read-ppm-file-and-store-it-in-an-array-coded-with-c
00038  * User: rpf
00039  * 8/1/2012
00040  */
00041 
00042 #include <ompl_visual_tools/utilities/ppm.h>
00043 
00044 namespace ompl_visual_tools
00045 {
00046 
00047 // *************************************************************************************************
00048 // Reading Function
00049 // *************************************************************************************************
00050 PPMImage *readPPM(const char *filename)
00051 {
00052   char buff[16];
00053   PPMImage *img;
00054   FILE *fp;
00055   int c, rgb_comp_color;
00056 
00057   //open PPM file for reading
00058   fp = fopen(filename, "rb");
00059   if (!fp) {
00060     fprintf(stderr, "Unable to open file '%s'\n", filename);
00061     exit(1);
00062   }
00063 
00064   //read image format
00065   if (!fgets(buff, sizeof(buff), fp)) {
00066     perror(filename);
00067     exit(1);
00068   }
00069 
00070   //check the image format
00071   if (buff[0] != 'P' || buff[1] != '6') {
00072     fprintf(stderr, "Invalid image format (must be 'P6')\n");
00073     exit(1);
00074   }
00075 
00076   //alloc memory form image
00077   img = (PPMImage *)malloc(sizeof(PPMImage));
00078   if (!img) {
00079     fprintf(stderr, "Unable to allocate memory\n");
00080     exit(1);
00081   }
00082 
00083   //check for comments
00084   c = getc(fp);
00085   while (c == '#') {
00086     while (getc(fp) != '\n') ;
00087     c = getc(fp);
00088   }
00089 
00090   ungetc(c, fp);
00091   //read image size information
00092   if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
00093     fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
00094     exit(1);
00095   }
00096 
00097   //read rgb component
00098   if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
00099     fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
00100     exit(1);
00101   }
00102 
00103   //check rgb component depth
00104   if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
00105     fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
00106     exit(1);
00107   }
00108 
00109   while (fgetc(fp) != '\n') ;
00110   //memory allocation for pixel data
00111   img->data = (PPMPixel*)malloc(img->x * img->y * sizeof(PPMPixel));
00112 
00113   if (!img) {
00114     fprintf(stderr, "Unable to allocate memory\n");
00115     exit(1);
00116   }
00117 
00118   //read pixel data from file
00119   if ( fread( img->data, 3 * img->x, img->y, fp) != img->y) {
00120     fprintf(stderr, "Error loading image '%s'\n", filename);
00121     exit(1);
00122   }
00123 
00124   // Close the file pointer
00125   fclose(fp);
00126   
00127   return img;
00128 }
00129 
00130 } // namespace


ompl_visual_tools
Author(s): Dave Coleman
autogenerated on Thu Jun 6 2019 21:13:31