2001-11-24 21:07:58 +03:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
2001-11-24 21:07:58 +03:00
|
|
|
//
|
|
|
|
// Fl_PNM_Image routines.
|
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 1997-2010 by Easy Software Products.
|
2001-11-24 21:07:58 +03:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
2001-11-24 21:07:58 +03:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// http://www.fltk.org/COPYING.php
|
2001-11-24 21:07:58 +03:00
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
2001-11-24 21:07:58 +03:00
|
|
|
//
|
|
|
|
// Contents:
|
2008-09-14 22:19:41 +04:00
|
|
|
|
2001-11-24 21:07:58 +03:00
|
|
|
//
|
2001-12-11 19:03:13 +03:00
|
|
|
// Fl_PNM_Image::Fl_PNM_Image() - Load a PNM image...
|
2001-11-24 21:07:58 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Include necessary header files...
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_PNM_Image.H>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-09-19 21:40:20 +04:00
|
|
|
#include <FL/fl_utf8.h>
|
2002-04-11 15:52:43 +04:00
|
|
|
#include "flstring.h"
|
2001-11-24 21:07:58 +03:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// 'Fl_PNM_Image::Fl_PNM_Image()' - Load a PNM image...
|
|
|
|
//
|
|
|
|
|
2008-09-14 22:19:41 +04:00
|
|
|
/**
|
|
|
|
The constructor loads the named PNM image.
|
|
|
|
<P>The inherited destructor free all memory and server resources that are used by the image.
|
|
|
|
*/
|
|
|
|
|
2001-11-24 21:07:58 +03:00
|
|
|
Fl_PNM_Image::Fl_PNM_Image(const char *name) // I - File to read
|
|
|
|
: Fl_RGB_Image(0,0,0) {
|
|
|
|
FILE *fp; // File pointer
|
|
|
|
int x, y; // Looping vars
|
|
|
|
char line[1024], // Input line
|
|
|
|
*lineptr; // Pointer in line
|
|
|
|
uchar *ptr, // Pointer to pixel values
|
|
|
|
byte, // Byte from file
|
|
|
|
bit; // Bit in pixel
|
|
|
|
int format, // Format of PNM file
|
|
|
|
val, // Pixel value
|
|
|
|
maxval; // Maximum pixel value
|
|
|
|
|
|
|
|
|
2008-09-11 03:56:49 +04:00
|
|
|
if ((fp = fl_fopen(name, "rb")) == NULL) return;
|
2001-11-28 21:09:08 +03:00
|
|
|
|
2001-11-24 21:07:58 +03:00
|
|
|
//
|
|
|
|
// Read the file header in the format:
|
|
|
|
//
|
|
|
|
// Pformat
|
|
|
|
// # comment1
|
|
|
|
// # comment2
|
|
|
|
// ...
|
|
|
|
// # commentN
|
|
|
|
// width
|
|
|
|
// height
|
|
|
|
// max sample
|
|
|
|
//
|
|
|
|
|
|
|
|
lineptr = fgets(line, sizeof(line), fp);
|
2001-12-11 19:03:13 +03:00
|
|
|
if (!lineptr) {
|
|
|
|
fclose(fp);
|
2001-12-19 21:15:34 +03:00
|
|
|
Fl::error("Early end-of-file in PNM file \"%s\"!", name);
|
2001-12-11 19:03:13 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-11-24 21:07:58 +03:00
|
|
|
lineptr ++;
|
|
|
|
|
|
|
|
format = atoi(lineptr);
|
|
|
|
while (isdigit(*lineptr)) lineptr ++;
|
|
|
|
|
2005-04-14 16:29:51 +04:00
|
|
|
if (format == 7) lineptr = (char *)"";
|
2002-09-18 00:27:19 +04:00
|
|
|
|
2001-11-24 21:07:58 +03:00
|
|
|
while (lineptr != NULL && w() == 0) {
|
|
|
|
if (*lineptr == '\0' || *lineptr == '#') {
|
|
|
|
lineptr = fgets(line, sizeof(line), fp);
|
|
|
|
} else if (isdigit(*lineptr)) {
|
2002-05-25 17:38:25 +04:00
|
|
|
w(strtol(lineptr, &lineptr, 10));
|
2001-11-24 21:07:58 +03:00
|
|
|
} else lineptr ++;
|
|
|
|
}
|
|
|
|
|
2002-05-25 17:38:25 +04:00
|
|
|
while (lineptr != NULL && h() == 0) {
|
2001-11-24 21:07:58 +03:00
|
|
|
if (*lineptr == '\0' || *lineptr == '#') {
|
|
|
|
lineptr = fgets(line, sizeof(line), fp);
|
|
|
|
} else if (isdigit(*lineptr)) {
|
2002-05-25 17:38:25 +04:00
|
|
|
h(strtol(lineptr, &lineptr, 10));
|
2001-11-24 21:07:58 +03:00
|
|
|
} else lineptr ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format != 1 && format != 4) {
|
|
|
|
maxval = 0;
|
|
|
|
|
|
|
|
while (lineptr != NULL && maxval == 0) {
|
|
|
|
if (*lineptr == '\0' || *lineptr == '#') {
|
|
|
|
lineptr = fgets(line, sizeof(line), fp);
|
|
|
|
} else if (isdigit(*lineptr)) {
|
2002-05-25 17:38:25 +04:00
|
|
|
maxval = strtol(lineptr, &lineptr, 10);
|
2001-11-24 21:07:58 +03:00
|
|
|
} else lineptr ++;
|
|
|
|
}
|
|
|
|
} else maxval = 1;
|
|
|
|
|
|
|
|
// Allocate memory...
|
|
|
|
if (format == 1 || format == 2 || format == 4 || format == 5) d(1);
|
|
|
|
else d(3);
|
|
|
|
|
2002-05-28 01:16:47 +04:00
|
|
|
// printf("%s = %dx%dx%d\n", name, w(), h(), d());
|
|
|
|
|
2012-11-09 20:02:08 +04:00
|
|
|
if (((size_t)w()) * h() * d() > max_size() ) {
|
|
|
|
Fl::warning("PNM file \"%s\" is too large!\n", name);
|
|
|
|
fclose(fp);
|
|
|
|
return;
|
|
|
|
}
|
2002-10-11 06:30:20 +04:00
|
|
|
array = new uchar[w() * h() * d()];
|
|
|
|
alloc_array = 1;
|
2001-11-24 21:07:58 +03:00
|
|
|
|
|
|
|
// Read the image file...
|
|
|
|
for (y = 0; y < h(); y ++) {
|
|
|
|
ptr = (uchar *)array + y * w() * d();
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case 1 :
|
|
|
|
case 2 :
|
|
|
|
for (x = w(); x > 0; x --)
|
2002-11-19 19:37:36 +03:00
|
|
|
if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
|
2001-11-24 21:07:58 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3 :
|
|
|
|
for (x = w(); x > 0; x --) {
|
2002-11-19 19:37:36 +03:00
|
|
|
if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
|
|
|
|
if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
|
|
|
|
if (fscanf(fp, "%d", &val) == 1) *ptr++ = (uchar)(255 * val / maxval);
|
2001-11-24 21:07:58 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4 :
|
2002-11-19 19:37:36 +03:00
|
|
|
for (x = w(), byte = (uchar)getc(fp), bit = 128; x > 0; x --) {
|
2001-11-24 21:07:58 +03:00
|
|
|
if (byte & bit) *ptr++ = 255;
|
|
|
|
else *ptr++ = 0;
|
|
|
|
|
|
|
|
if (bit > 1) bit >>= 1;
|
|
|
|
else {
|
|
|
|
bit = 128;
|
2002-11-19 19:37:36 +03:00
|
|
|
byte = (uchar)getc(fp);
|
2001-11-24 21:07:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2007-12-20 19:32:56 +03:00
|
|
|
|
2001-11-24 21:07:58 +03:00
|
|
|
case 5 :
|
|
|
|
case 6 :
|
2007-12-20 19:32:56 +03:00
|
|
|
if (maxval < 256) {
|
2010-10-29 01:47:01 +04:00
|
|
|
if (fread(ptr, w(), d(), fp)) { /* ignored */ }
|
2007-12-20 19:32:56 +03:00
|
|
|
} else {
|
|
|
|
for (x = d() * w(); x > 0; x --) {
|
|
|
|
val = (uchar)getc(fp);
|
|
|
|
val = (val<<8)|(uchar)getc(fp);
|
|
|
|
*ptr++ = (255*val)/maxval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-09-18 00:27:19 +04:00
|
|
|
case 7 : /* XV 3:3:2 thumbnail format */
|
2007-12-20 19:32:56 +03:00
|
|
|
for (x = w(); x > 0; x --) {
|
|
|
|
byte = (uchar)getc(fp);
|
|
|
|
|
|
|
|
*ptr++ = (uchar)(255 * ((byte >> 5) & 7) / 7);
|
|
|
|
*ptr++ = (uchar)(255 * ((byte >> 2) & 7) / 7);
|
|
|
|
*ptr++ = (uchar)(255 * (byte & 3) / 3);
|
|
|
|
}
|
|
|
|
break;
|
2001-11-24 21:07:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
2001-11-24 21:07:58 +03:00
|
|
|
//
|