2 // "$Id: Fl_PNM_Image.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Fl_PNM_Image routines.
6 // Copyright 1997-2010 by Easy Software Products.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
30 // Fl_PNM_Image::Fl_PNM_Image() - Load a PNM image...
34 // Include necessary header files...
38 #include <FL/Fl_PNM_Image.H>
41 #include <FL/fl_utf8.h>
46 // 'Fl_PNM_Image::Fl_PNM_Image()' - Load a PNM image...
50 The constructor loads the named PNM image.
51 <P>The inherited destructor free all memory and server resources that are used by the image.
54 Fl_PNM_Image::Fl_PNM_Image(const char *name
) // I - File to read
55 : Fl_RGB_Image(0,0,0) {
56 FILE *fp
; // File pointer
57 int x
, y
; // Looping vars
58 char line
[1024], // Input line
59 *lineptr
; // Pointer in line
60 uchar
*ptr
, // Pointer to pixel values
61 byte
, // Byte from file
63 int format
, // Format of PNM file
65 maxval
; // Maximum pixel value
68 if ((fp
= fl_fopen(name
, "rb")) == NULL
) return;
71 // Read the file header in the format:
83 lineptr
= fgets(line
, sizeof(line
), fp
);
86 Fl::error("Early end-of-file in PNM file \"%s\"!", name
);
92 format
= atoi(lineptr
);
93 while (isdigit(*lineptr
)) lineptr
++;
95 if (format
== 7) lineptr
= (char *)"";
97 while (lineptr
!= NULL
&& w() == 0) {
98 if (*lineptr
== '\0' || *lineptr
== '#') {
99 lineptr
= fgets(line
, sizeof(line
), fp
);
100 } else if (isdigit(*lineptr
)) {
101 w(strtol(lineptr
, &lineptr
, 10));
105 while (lineptr
!= NULL
&& h() == 0) {
106 if (*lineptr
== '\0' || *lineptr
== '#') {
107 lineptr
= fgets(line
, sizeof(line
), fp
);
108 } else if (isdigit(*lineptr
)) {
109 h(strtol(lineptr
, &lineptr
, 10));
113 if (format
!= 1 && format
!= 4) {
116 while (lineptr
!= NULL
&& maxval
== 0) {
117 if (*lineptr
== '\0' || *lineptr
== '#') {
118 lineptr
= fgets(line
, sizeof(line
), fp
);
119 } else if (isdigit(*lineptr
)) {
120 maxval
= strtol(lineptr
, &lineptr
, 10);
125 // Allocate memory...
126 if (format
== 1 || format
== 2 || format
== 4 || format
== 5) d(1);
129 // printf("%s = %dx%dx%d\n", name, w(), h(), d());
131 array
= new uchar
[w() * h() * d()];
134 // Read the image file...
135 for (y
= 0; y
< h(); y
++) {
136 ptr
= (uchar
*)array
+ y
* w() * d();
141 for (x
= w(); x
> 0; x
--)
142 if (fscanf(fp
, "%d", &val
) == 1) *ptr
++ = (uchar
)(255 * val
/ maxval
);
146 for (x
= w(); x
> 0; x
--) {
147 if (fscanf(fp
, "%d", &val
) == 1) *ptr
++ = (uchar
)(255 * val
/ maxval
);
148 if (fscanf(fp
, "%d", &val
) == 1) *ptr
++ = (uchar
)(255 * val
/ maxval
);
149 if (fscanf(fp
, "%d", &val
) == 1) *ptr
++ = (uchar
)(255 * val
/ maxval
);
154 for (x
= w(), byte
= (uchar
)getc(fp
), bit
= 128; x
> 0; x
--) {
155 if (byte
& bit
) *ptr
++ = 255;
158 if (bit
> 1) bit
>>= 1;
161 byte
= (uchar
)getc(fp
);
169 if (fread(ptr
, w(), d(), fp
)) { /* ignored */ }
171 for (x
= d() * w(); x
> 0; x
--) {
172 val
= (uchar
)getc(fp
);
173 val
= (val
<<8)|(uchar
)getc(fp
);
174 *ptr
++ = (255*val
)/maxval
;
179 case 7 : /* XV 3:3:2 thumbnail format */
180 for (x
= w(); x
> 0; x
--) {
181 byte
= (uchar
)getc(fp
);
183 *ptr
++ = (uchar
)(255 * ((byte
>> 5) & 7) / 7);
184 *ptr
++ = (uchar
)(255 * ((byte
>> 2) & 7) / 7);
185 *ptr
++ = (uchar
)(255 * (byte
& 3) / 3);
196 // End of "$Id: Fl_PNM_Image.cxx 7903 2010-11-28 21:06:39Z matt $".