fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_PNM_Image.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_PNM_Image.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Fl_PNM_Image routines.
00005 //
00006 // Copyright 1997-2010 by Easy Software Products.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 // Contents:
00028 
00029 //
00030 //   Fl_PNM_Image::Fl_PNM_Image() - Load a PNM image...
00031 //
00032 
00033 //
00034 // Include necessary header files...
00035 //
00036 
00037 #include <FL/Fl.H>
00038 #include <FL/Fl_PNM_Image.H>
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <FL/fl_utf8.h>
00042 #include "flstring.h"
00043 
00044 
00045 //
00046 // 'Fl_PNM_Image::Fl_PNM_Image()' - Load a PNM image...
00047 //
00048 
00054 Fl_PNM_Image::Fl_PNM_Image(const char *name)    // I - File to read
00055   : Fl_RGB_Image(0,0,0) {
00056   FILE          *fp;            // File pointer
00057   int           x, y;           // Looping vars
00058   char          line[1024],     // Input line
00059                 *lineptr;       // Pointer in line
00060   uchar         *ptr,           // Pointer to pixel values
00061                 byte,           // Byte from file
00062                 bit;            // Bit in pixel
00063   int           format,         // Format of PNM file
00064                 val,            // Pixel value
00065                 maxval;         // Maximum pixel value
00066 
00067 
00068   if ((fp = fl_fopen(name, "rb")) == NULL) return;
00069 
00070   //
00071   // Read the file header in the format:
00072   //
00073   //   Pformat
00074   //   # comment1
00075   //   # comment2
00076   //   ...
00077   //   # commentN
00078   //   width
00079   //   height
00080   //   max sample
00081   //
00082 
00083   lineptr = fgets(line, sizeof(line), fp);
00084   if (!lineptr) {
00085     fclose(fp);
00086     Fl::error("Early end-of-file in PNM file \"%s\"!", name);
00087     return;
00088   }
00089 
00090   lineptr ++;
00091 
00092   format = atoi(lineptr);
00093   while (isdigit(*lineptr)) lineptr ++;
00094 
00095   if (format == 7) lineptr = (char *)"";
00096 
00097   while (lineptr != NULL && w() == 0) {
00098     if (*lineptr == '\0' || *lineptr == '#') {
00099       lineptr = fgets(line, sizeof(line), fp);
00100     } else if (isdigit(*lineptr)) {
00101       w(strtol(lineptr, &lineptr, 10));
00102     } else lineptr ++;
00103   }
00104 
00105   while (lineptr != NULL && h() == 0) {
00106     if (*lineptr == '\0' || *lineptr == '#') {
00107       lineptr = fgets(line, sizeof(line), fp);
00108     } else if (isdigit(*lineptr)) {
00109       h(strtol(lineptr, &lineptr, 10));
00110     } else lineptr ++;
00111   }
00112 
00113   if (format != 1 && format != 4) {
00114     maxval = 0;
00115 
00116     while (lineptr != NULL && maxval == 0) {
00117       if (*lineptr == '\0' || *lineptr == '#') {
00118         lineptr = fgets(line, sizeof(line), fp);
00119       } else if (isdigit(*lineptr)) {
00120         maxval = strtol(lineptr, &lineptr, 10);
00121       } else lineptr ++;
00122     }
00123   } else maxval = 1;
00124 
00125   // Allocate memory...
00126   if (format == 1 || format == 2 || format == 4 || format == 5) d(1);
00127   else d(3);
00128 
00129 //  printf("%s = %dx%dx%d\n", name, w(), h(), d());
00130 
00131   array       = new uchar[w() * h() * d()];
00132   alloc_array = 1;
00133 
00134   // Read the image file...
00135   for (y = 0; y < h(); y ++) {
00136     ptr = (uchar *)array + y * w() * d();
00137 
00138     switch (format) {
00139       case 1 :
00140       case 2 :
00141           for (x = w(); x > 0; x --)
00142             if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
00143           break;
00144 
00145       case 3 :
00146           for (x = w(); x > 0; x --) {
00147             if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
00148             if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
00149             if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
00150           }
00151           break;
00152 
00153       case 4 :
00154           for (x = w(), byte = (uchar)getc(fp), bit = 128; x > 0; x --) {
00155             if (byte & bit) *ptr++ = 255;
00156             else *ptr++ = 0;
00157 
00158             if (bit > 1) bit >>= 1;
00159             else {
00160               bit  = 128;
00161               byte = (uchar)getc(fp);
00162             }
00163           }
00164           break;
00165           
00166       case 5 :
00167       case 6 :
00168         if (maxval < 256) {
00169           if (fread(ptr, w(), d(), fp)) { /* ignored */ }
00170         } else {
00171           for (x = d() * w(); x > 0; x --) {
00172             val = (uchar)getc(fp);
00173             val = (val<<8)|(uchar)getc(fp);
00174             *ptr++ = (255*val)/maxval;
00175           }
00176         }
00177         break;
00178         
00179       case 7 : /* XV 3:3:2 thumbnail format */
00180         for (x = w(); x > 0; x --) {
00181           byte = (uchar)getc(fp);
00182           
00183           *ptr++ = (uchar)(255 * ((byte >> 5) & 7) / 7);
00184           *ptr++ = (uchar)(255 * ((byte >> 2) & 7) / 7);
00185           *ptr++ = (uchar)(255 * (byte & 3) / 3);
00186         }
00187         break;
00188     }
00189   }
00190 
00191   fclose(fp);
00192 }
00193 
00194 
00195 //
00196 // End of "$Id: Fl_PNM_Image.cxx 7903 2010-11-28 21:06:39Z matt $".
00197 //