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

Fl_PNM_Image.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Fl_PNM_Image routines.
5 //
6 // Copyright 1997-2010 by Easy Software Products.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 // Contents:
19 
20 //
21 // Fl_PNM_Image::Fl_PNM_Image() - Load a PNM image...
22 //
23 
24 //
25 // Include necessary header files...
26 //
27 
28 #include <FL/Fl.H>
29 #include <FL/Fl_PNM_Image.H>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <FL/fl_utf8.h>
33 #include "flstring.h"
34 
35 
36 //
37 // 'Fl_PNM_Image::Fl_PNM_Image()' - Load a PNM image...
38 //
39 
40 
52 Fl_PNM_Image::Fl_PNM_Image(const char *name) // I - File to read
53  : Fl_RGB_Image(0,0,0) {
54  FILE *fp; // File pointer
55  int x, y; // Looping vars
56  char line[1024], // Input line
57  *lineptr; // Pointer in line
58  uchar *ptr, // Pointer to pixel values
59  byte, // Byte from file
60  bit; // Bit in pixel
61  int format, // Format of PNM file
62  val, // Pixel value
63  maxval; // Maximum pixel value
64 
65 
66  if ((fp = fl_fopen(name, "rb")) == NULL) {
68  return;
69  }
70 
71  //
72  // Read the file header in the format:
73  //
74  // Pformat
75  // # comment1
76  // # comment2
77  // ...
78  // # commentN
79  // width
80  // height
81  // max sample
82  //
83 
84  lineptr = fgets(line, sizeof(line), fp);
85  if (!lineptr) {
86  fclose(fp);
87  Fl::error("Early end-of-file in PNM file \"%s\"!", name);
89  return;
90  }
91 
92  lineptr ++;
93 
94  format = atoi(lineptr);
95  while (isdigit(*lineptr)) lineptr ++;
96 
97  if (format == 7) lineptr = (char *)"";
98 
99  while (lineptr != NULL && w() == 0) {
100  if (*lineptr == '\0' || *lineptr == '#') {
101  lineptr = fgets(line, sizeof(line), fp);
102  } else if (isdigit(*lineptr)) {
103  w(strtol(lineptr, &lineptr, 10));
104  } else lineptr ++;
105  }
106 
107  while (lineptr != NULL && h() == 0) {
108  if (*lineptr == '\0' || *lineptr == '#') {
109  lineptr = fgets(line, sizeof(line), fp);
110  } else if (isdigit(*lineptr)) {
111  h(strtol(lineptr, &lineptr, 10));
112  } else lineptr ++;
113  }
114 
115  if (format != 1 && format != 4) {
116  maxval = 0;
117 
118  while (lineptr != NULL && maxval == 0) {
119  if (*lineptr == '\0' || *lineptr == '#') {
120  lineptr = fgets(line, sizeof(line), fp);
121  } else if (isdigit(*lineptr)) {
122  maxval = strtol(lineptr, &lineptr, 10);
123  } else lineptr ++;
124  }
125  } else maxval = 1;
126 
127  // Allocate memory...
128  if (format == 1 || format == 2 || format == 4 || format == 5) d(1);
129  else d(3);
130 
131 // printf("%s = %dx%dx%d\n", name, w(), h(), d());
132 
133  if (((size_t)w()) * h() * d() > max_size() ) {
134  Fl::warning("PNM file \"%s\" is too large!\n", name);
135  fclose(fp);
136  w(0); h(0); d(0); ld(ERR_FORMAT);
137  return;
138  }
139  array = new uchar[w() * h() * d()];
140  alloc_array = 1;
141 
142  // Read the image file...
143  for (y = 0; y < h(); y ++) {
144  ptr = (uchar *)array + y * w() * d();
145 
146  switch (format) {
147  case 1 :
148  for (x = w(); x > 0; x --)
149  if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * (1-val));
150  break;
151 
152  case 2 :
153  for (x = w(); x > 0; x --)
154  if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
155  break;
156 
157  case 3 :
158  for (x = w(); x > 0; x --) {
159  if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
160  if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
161  if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
162  }
163  break;
164 
165  case 4 :
166  for (x = w(), byte = (uchar)getc(fp), bit = 128; x > 0; x --) {
167  if ((byte & bit) == 0) *ptr++ = 255; // 0 bit for white pixel
168  else *ptr++ = 0; // 1 bit for black pixel
169 
170  if (bit > 1) bit >>= 1;
171  else {
172  bit = 128;
173  if (x > 1) byte = (uchar)getc(fp);
174  }
175  }
176  break;
177 
178  case 5 :
179  case 6 :
180  if (maxval < 256) {
181  if (fread(ptr, w(), d(), fp)) { /* ignored */ }
182  } else {
183  for (x = d() * w(); x > 0; x --) {
184  val = (uchar)getc(fp);
185  val = (val<<8)|(uchar)getc(fp);
186  *ptr++ = (255*val)/maxval;
187  }
188  }
189  break;
190 
191  case 7 : /* XV 3:3:2 thumbnail format */
192  for (x = w(); x > 0; x --) {
193  byte = (uchar)getc(fp);
194 
195  *ptr++ = (uchar)(255 * ((byte >> 5) & 7) / 7);
196  *ptr++ = (uchar)(255 * ((byte >> 2) & 7) / 7);
197  *ptr++ = (uchar)(255 * (byte & 3) / 3);
198  }
199  break;
200  }
201  }
202 
203  fclose(fp);
204 }
205 
206 
207 //
208 // End of "$Id$".
209 //
Fl.H
Fl_PNM_Image::Fl_PNM_Image
Fl_PNM_Image(const char *filename)
Definition: Fl_PNM_Image.cxx:52
Fl_Image::d
int d() const
Definition: Fl_Image.H:121
NULL
#define NULL
Definition: forms.H:34
fl_fopen
FILE * fl_fopen(const char *f, const char *mode)
Definition: fl_utf8.cxx:498
Fl_Image::ld
int ld() const
Definition: Fl_Image.H:126
Fl_RGB_Image::alloc_array
int alloc_array
Definition: Fl_Image.H:215
fl_utf8.h
header for Unicode and UTF-8 character handling
Fl_Image::ERR_FORMAT
static const int ERR_FORMAT
Definition: Fl_Image.H:60
Fl_Image::h
int h() const
Definition: Fl_Image.H:115
Fl_RGB_Image::array
const uchar * array
Definition: Fl_Image.H:212
x
int x
Definition: test.c:73
Fl_RGB_Image::max_size
static size_t max_size()
Definition: Fl_Image.H:255
y
int y
Definition: test.c:74
Fl_Image::w
int w() const
Definition: Fl_Image.H:111
Fl::warning
static void(* warning)(const char *,...)
Definition: Fl.H:498
flstring.h
Fl::error
static void(* error)(const char *,...)
Definition: Fl.H:513
Fl_Image::ERR_FILE_ACCESS
static const int ERR_FILE_ACCESS
Definition: Fl_Image.H:59
uchar
unsigned char uchar
Definition: fl_types.h:30
name
static const char * name
Definition: Fl_arg.cxx:53
Fl_RGB_Image
Definition: Fl_Image.H:202
Fl_PNM_Image.H