mirror of
https://github.com/gryf/wmaker.git
synced 2025-12-19 04:20:27 +01:00
wrlib: Improved NETPBM support, file format detection
1) according to that checks i was able to see that some netpbm support type are missing (exactly: ascii graymap (PGM files) and pixmap (PPM) and ascii/binary bitmap (PBM)) See the link below for more details. http://en.wikipedia.org/wiki/Netpbm_format Signed-off-by: Carlos R. Mafra <crmafra@gmail.com>
This commit is contained in:
committed by
Carlos R. Mafra
parent
aed4d78e87
commit
5c96c69cb5
@@ -78,8 +78,8 @@ char **RSupportedFileFormats(void)
|
|||||||
|
|
||||||
/* built-in */
|
/* built-in */
|
||||||
tmp[i++] = "XPM";
|
tmp[i++] = "XPM";
|
||||||
/* built-in */
|
/* built-in PNM here refers to anymap format: PPM, PGM, PBM */
|
||||||
tmp[i++] = "PPM";
|
tmp[i++] = "PNM";
|
||||||
#ifdef USE_TIFF
|
#ifdef USE_TIFF
|
||||||
tmp[i++] = "TIFF";
|
tmp[i++] = "TIFF";
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
274
wrlib/load_ppm.c
274
wrlib/load_ppm.c
@@ -3,6 +3,7 @@
|
|||||||
* Raster graphics library
|
* Raster graphics library
|
||||||
*
|
*
|
||||||
* Copyright (c) 1997-2003 Alfredo K. Kojima
|
* Copyright (c) 1997-2003 Alfredo K. Kojima
|
||||||
|
* Copyright (c) 2014 Window Maker Team
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Library General Public
|
* modify it under the terms of the GNU Library General Public
|
||||||
@@ -26,79 +27,248 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#include "wraster.h"
|
#include "wraster.h"
|
||||||
#include "imgformat.h"
|
#include "imgformat.h"
|
||||||
|
|
||||||
static RImage *load_graymap(FILE *file, int w, int h, int max, int raw)
|
/* fileio.c - routines to read elements based on Netpbm
|
||||||
|
**
|
||||||
|
** Copyright (C) 1988 by Jef Poskanzer.
|
||||||
|
*/
|
||||||
|
|
||||||
|
char pm_getc(FILE * const fileP)
|
||||||
|
{
|
||||||
|
int ich;
|
||||||
|
char ch;
|
||||||
|
|
||||||
|
ich = getc(fileP);
|
||||||
|
if (ich == EOF)
|
||||||
|
fprintf(stderr, "EOF / read error reading a byte\n");
|
||||||
|
ch = (char)ich;
|
||||||
|
|
||||||
|
if (ch == '#') {
|
||||||
|
do {
|
||||||
|
ich = getc(fileP);
|
||||||
|
if (ich == EOF)
|
||||||
|
fprintf(stderr, "EOF / read error reading a byte\n");
|
||||||
|
ch = (char)ich;
|
||||||
|
} while (ch != '\n' && ch != '\r');
|
||||||
|
}
|
||||||
|
return ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char pm_getrawbyte(FILE * const file)
|
||||||
|
{
|
||||||
|
int iby;
|
||||||
|
|
||||||
|
iby = getc(file);
|
||||||
|
if (iby == EOF)
|
||||||
|
fprintf(stderr, "EOF / read error reading a one-byte sample\n");
|
||||||
|
return (unsigned char)iby;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pm_getuint(FILE * const ifP)
|
||||||
|
{
|
||||||
|
char ch;
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
do {
|
||||||
|
ch = pm_getc(ifP);
|
||||||
|
} while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
|
||||||
|
|
||||||
|
if (ch < '0' || ch > '9')
|
||||||
|
fprintf(stderr, "junk in file where an unsigned integer should be\n");
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
do {
|
||||||
|
unsigned int const digitVal = ch - '0';
|
||||||
|
|
||||||
|
if (i > INT_MAX / 10) {
|
||||||
|
fprintf(stderr, "ASCII decimal integer in file is too large to be processed\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
i *= 10;
|
||||||
|
|
||||||
|
if (i > INT_MAX - digitVal) {
|
||||||
|
fprintf(stderr, "ASCII decimal integer in file is too large to be processed\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += digitVal;
|
||||||
|
|
||||||
|
ch = pm_getc(ifP);
|
||||||
|
} while (ch >= '0' && ch <= '9');
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
/******************************************************************************************/
|
||||||
|
|
||||||
|
/* PGM: support for portable graymap ascii and binary encoding */
|
||||||
|
static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)
|
||||||
{
|
{
|
||||||
RImage *image;
|
RImage *image;
|
||||||
unsigned char *ptr;
|
unsigned char *ptr;
|
||||||
char *buf;
|
|
||||||
int x, y;
|
int x, y;
|
||||||
|
|
||||||
image = RCreateImage(w, h, 0);
|
image = RCreateImage(w, h, 0);
|
||||||
if (!image)
|
if (!image)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!raw)
|
if (raw != '2' && raw != '5')
|
||||||
return image;
|
return image;
|
||||||
|
|
||||||
if (max < 256) {
|
if (max < 256) {
|
||||||
buf = malloc(w + 1);
|
|
||||||
if (!buf)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
ptr = image->data;
|
ptr = image->data;
|
||||||
for (y = 0; y < h; y++) {
|
if (raw == '2') {
|
||||||
if (!fread(buf, w, 1, file)) {
|
int val;
|
||||||
free(buf);
|
for (y = 0; y < h; y++) {
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
for (x = 0; x < w; x++) {
|
||||||
return NULL;
|
val = pm_getuint(file);
|
||||||
}
|
|
||||||
|
|
||||||
for (x = 0; x < w; x++) {
|
if (val > max || val < 0) {
|
||||||
*(ptr++) = buf[x];
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
*(ptr++) = buf[x];
|
return NULL;
|
||||||
*(ptr++) = buf[x];
|
}
|
||||||
|
|
||||||
|
val = val * 255 / max;
|
||||||
|
*(ptr++) = val;
|
||||||
|
*(ptr++) = val;
|
||||||
|
*(ptr++) = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (raw == '5') {
|
||||||
|
char *buf;
|
||||||
|
buf = malloc(w + 1);
|
||||||
|
if (!buf)
|
||||||
|
return NULL;
|
||||||
|
for (y = 0; y < h; y++) {
|
||||||
|
if (!fread(buf, w, 1, file)) {
|
||||||
|
free(buf);
|
||||||
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (x = 0; x < w; x++) {
|
||||||
|
*(ptr++) = buf[x];
|
||||||
|
*(ptr++) = buf[x];
|
||||||
|
*(ptr++) = buf[x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
static RImage *load_pixmap(FILE *file, int w, int h, int max, int raw)
|
/* PPM: support for portable pixmap ascii and binary encoding */
|
||||||
|
static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
|
||||||
{
|
{
|
||||||
RImage *image;
|
RImage *image;
|
||||||
int i;
|
unsigned char *ptr;
|
||||||
char buf[3];
|
int i = 0;
|
||||||
|
|
||||||
|
image = RCreateImage(w, h, 0);
|
||||||
|
if (!image)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (raw != '3' && raw != '6')
|
||||||
|
return image;
|
||||||
|
|
||||||
|
ptr = image->data;
|
||||||
|
if (max < 256) {
|
||||||
|
if (raw == '3') {
|
||||||
|
int x, y, val;
|
||||||
|
for (y = 0; y < h; y++) {
|
||||||
|
for (x = 0; x < w; x++) {
|
||||||
|
for (i = 0; i < 3; i++) {
|
||||||
|
val = pm_getuint(file);
|
||||||
|
|
||||||
|
if (val > max || val < 0) {
|
||||||
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
val = val * 255 / max;
|
||||||
|
*(ptr++) = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (raw == '6') {
|
||||||
|
char buf[3];
|
||||||
|
while (i < w * h) {
|
||||||
|
if (fread(buf, 1, 3, file) != 3) {
|
||||||
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*(ptr++) = buf[0];
|
||||||
|
*(ptr++) = buf[1];
|
||||||
|
*(ptr++) = buf[2];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* PBM: support for portable bitmap ascii and binary encoding */
|
||||||
|
static RImage *load_bitmap(FILE * file, int w, int h, int max, int raw)
|
||||||
|
{
|
||||||
|
RImage *image;
|
||||||
|
int val;
|
||||||
unsigned char *ptr;
|
unsigned char *ptr;
|
||||||
|
|
||||||
image = RCreateImage(w, h, 0);
|
image = RCreateImage(w, h, 0);
|
||||||
if (!image)
|
if (!image)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!raw)
|
if (raw != '1' && raw != '4')
|
||||||
return image;
|
return image;
|
||||||
|
|
||||||
ptr = image->data;
|
ptr = image->data;
|
||||||
if (max < 256) {
|
if (raw == '1') {
|
||||||
i = 0;
|
int i = 0;
|
||||||
while (i < w * h) {
|
while (i < w * h) {
|
||||||
if (fread(buf, 1, 3, file) != 3) {
|
val = pm_getuint(file);
|
||||||
|
|
||||||
|
if (val > max || val < 0) {
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
*(ptr++) = buf[0];
|
val = (val == 0) ? 255 : 0;
|
||||||
*(ptr++) = buf[1];
|
*(ptr++) = val;
|
||||||
*(ptr++) = buf[2];
|
*(ptr++) = val;
|
||||||
|
*(ptr++) = val;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
if (raw == '4') {
|
||||||
|
unsigned char buf;
|
||||||
|
int bitshift;
|
||||||
|
int x, y;
|
||||||
|
|
||||||
|
for (y = 0; y < h; y++) {
|
||||||
|
bitshift = -1;
|
||||||
|
for (x = 0; x < w; x++) {
|
||||||
|
if (bitshift == -1) {
|
||||||
|
buf = pm_getrawbyte(file);
|
||||||
|
bitshift = 7;
|
||||||
|
}
|
||||||
|
val = (buf >> bitshift) & 1;
|
||||||
|
val = (val == 0) ? 255 : 0;
|
||||||
|
--bitshift;
|
||||||
|
*(ptr++) = val;
|
||||||
|
*(ptr++) = val;
|
||||||
|
*(ptr++) = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,8 +293,8 @@ RImage *RLoadPPM(const char *file_name)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* only accept raw pixmaps or graymaps */
|
/* accept bitmaps, pixmaps or graymaps */
|
||||||
if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
|
if (buffer[0] != 'P' || (buffer[1] < '1' && buffer[1] > '6')) {
|
||||||
RErrorCode = RERR_BADFORMAT;
|
RErrorCode = RERR_BADFORMAT;
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -152,24 +322,36 @@ RImage *RLoadPPM(const char *file_name)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fgets(buffer, 255, file)) {
|
if (type != '1' && type != '4') {
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
if (!fgets(buffer, 255, file)) {
|
||||||
fclose(file);
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
return NULL;
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
/* get max value */
|
||||||
|
if (sscanf(buffer, "%i", &m) != 1 || m < 1) {
|
||||||
|
/* Short file */
|
||||||
|
RErrorCode = RERR_BADIMAGEFILE;
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
m = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sscanf(buffer, "%i", &m) != 1 || m < 1) {
|
/* check portable bitmap type, ascii = 1 and binary = 4 */
|
||||||
/* Short file */
|
if (type == '1' || type == '4')
|
||||||
RErrorCode = RERR_BADIMAGEFILE;
|
image = load_bitmap(file, w, h, m, type);
|
||||||
fclose(file);
|
else {
|
||||||
return NULL;
|
/* check portable graymap type, ascii = 2 and binary = 5 */
|
||||||
|
if (type == '2' || type == '5')
|
||||||
|
image = load_graymap(file, w, h, m, type);
|
||||||
|
else
|
||||||
|
/* check portable pixmap type, ascii = 3 and binary = 6 */
|
||||||
|
if (type == '3' || type == '6')
|
||||||
|
image = load_pixmap(file, w, h, m, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == '5')
|
|
||||||
image = load_graymap(file, w, h, m, type == '5');
|
|
||||||
else if (type == '6')
|
|
||||||
image = load_pixmap(file, w, h, m, type == '6');
|
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user