themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / Fl_PNM_Image.cxx
blob569d175369281f71f434d76150e2b40e16fe40c6
1 //
2 // "$Id: Fl_PNM_Image.cxx 7903 2010-11-28 21:06:39Z matt $"
3 //
4 // Fl_PNM_Image routines.
5 //
6 // Copyright 1997-2010 by Easy Software Products.
7 //
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
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
27 // Contents:
30 // Fl_PNM_Image::Fl_PNM_Image() - Load a PNM image...
34 // Include necessary header files...
37 #include <FL/Fl.H>
38 #include <FL/Fl_PNM_Image.H>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <FL/fl_utf8.h>
42 #include "flstring.h"
46 // 'Fl_PNM_Image::Fl_PNM_Image()' - Load a PNM image...
49 /**
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
62 bit; // Bit in pixel
63 int format, // Format of PNM file
64 val, // Pixel value
65 maxval; // Maximum pixel value
68 if ((fp = fl_fopen(name, "rb")) == NULL) return;
71 // Read the file header in the format:
73 // Pformat
74 // # comment1
75 // # comment2
76 // ...
77 // # commentN
78 // width
79 // height
80 // max sample
83 lineptr = fgets(line, sizeof(line), fp);
84 if (!lineptr) {
85 fclose(fp);
86 Fl::error("Early end-of-file in PNM file \"%s\"!", name);
87 return;
90 lineptr ++;
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));
102 } else lineptr ++;
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));
110 } else lineptr ++;
113 if (format != 1 && format != 4) {
114 maxval = 0;
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);
121 } else lineptr ++;
123 } else maxval = 1;
125 // Allocate memory...
126 if (format == 1 || format == 2 || format == 4 || format == 5) d(1);
127 else d(3);
129 // printf("%s = %dx%dx%d\n", name, w(), h(), d());
131 array = new uchar[w() * h() * d()];
132 alloc_array = 1;
134 // Read the image file...
135 for (y = 0; y < h(); y ++) {
136 ptr = (uchar *)array + y * w() * d();
138 switch (format) {
139 case 1 :
140 case 2 :
141 for (x = w(); x > 0; x --)
142 if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
143 break;
145 case 3 :
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);
151 break;
153 case 4 :
154 for (x = w(), byte = (uchar)getc(fp), bit = 128; x > 0; x --) {
155 if (byte & bit) *ptr++ = 255;
156 else *ptr++ = 0;
158 if (bit > 1) bit >>= 1;
159 else {
160 bit = 128;
161 byte = (uchar)getc(fp);
164 break;
166 case 5 :
167 case 6 :
168 if (maxval < 256) {
169 if (fread(ptr, w(), d(), fp)) { /* ignored */ }
170 } else {
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;
177 break;
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);
187 break;
191 fclose(fp);
196 // End of "$Id: Fl_PNM_Image.cxx 7903 2010-11-28 21:06:39Z matt $".