Files
wmaker/wrlib/ppm.c
T

180 lines
3.7 KiB
C
Raw Normal View History

1998-09-29 22:36:29 +00:00
/* ppm.c - load PPM image from file
2004-10-12 21:28:27 +00:00
*
2003-01-16 23:30:45 +00:00
* Raster graphics library
2004-10-12 21:28:27 +00:00
*
2003-01-16 23:30:45 +00:00
* Copyright (c) 1997-2003 Alfredo K. Kojima
1998-09-29 22:36:29 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
2004-10-12 21:28:27 +00:00
*
1998-09-29 22:36:29 +00:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
2004-10-12 21:28:27 +00:00
*
1998-09-29 22:36:29 +00:00
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
#include <X11/Xlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "wraster.h"
static RImage*
load_graymap(char *file_name, FILE *file, int w, int h, int max, int raw)
{
RImage *image;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
image = RCreateImage(w, h, 0);
if (!image) {
2004-10-12 21:28:27 +00:00
return NULL;
1998-09-29 22:36:29 +00:00
}
if (!raw) {
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
} else {
2004-10-12 21:28:27 +00:00
if (max<256) {
int x, y;
char *buf, *ptr;
buf = malloc(w+1);
if (!buf) {
return NULL;
}
ptr = image->data;
for (y = 0; y < h; y++) {
if (!fread(buf, w, 1, file)) {
2004-10-12 21:28:27 +00:00
free(buf);
goto short_file;
}
for (x = 0; x < w; x++) {
*(ptr++) = buf[x];
*(ptr++) = buf[x];
*(ptr++) = buf[x];
}
}
free(buf);
} else {
}
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
return image;
2004-10-12 21:28:27 +00:00
short_file:
1998-10-21 14:43:47 +00:00
RErrorCode = RERR_BADIMAGEFILE;
1998-09-29 22:36:29 +00:00
return NULL;
}
static RImage*
load_pixmap(char *file_name, FILE *file, int w, int h, int max, int raw)
{
RImage *image;
int i;
char buf[3];
char *ptr;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
image = RCreateImage(w, h, 0);
if (!image) {
2004-10-12 21:28:27 +00:00
return NULL;
1998-09-29 22:36:29 +00:00
}
ptr = image->data;
1998-09-29 22:36:29 +00:00
if (!raw) {
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
} else {
2004-10-12 21:28:27 +00:00
if (max<256) {
i = 0;
while (i < w*h) {
if (fread(buf, 1, 3, file)!=3)
goto short_file;
*(ptr++) = buf[0];
*(ptr++) = buf[1];
*(ptr++) = buf[2];
i++;
}
} else {
}
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
return image;
2004-10-12 21:28:27 +00:00
short_file:
1998-10-21 14:43:47 +00:00
RErrorCode = RERR_BADIMAGEFILE;
1998-09-29 22:36:29 +00:00
return NULL;
}
RImage*
RLoadPPM(RContext *context, char *file_name, int index)
{
FILE *file;
RImage *image = NULL;
char buffer[256];
int w, h, m;
int type;
#define GETL() if (!fgets(buffer, 255, file)) goto short_file
2004-10-12 21:28:27 +00:00
file = fopen(file_name, "rb");
1998-09-29 22:36:29 +00:00
if (!file) {
2004-10-12 21:28:27 +00:00
RErrorCode = RERR_OPEN;
return NULL;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
/* get signature */
GETL();
/* only accept raw pixmaps or graymaps */
if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
2004-10-12 21:28:27 +00:00
RErrorCode = RERR_BADFORMAT;
fclose(file);
return NULL;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
type = buffer[1];
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
/* skip comments */
while (1) {
2004-10-12 21:28:27 +00:00
GETL();
if (buffer[0]!='#')
break;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
/* get size */
2002-10-25 03:43:57 +00:00
if (sscanf(buffer, "%i %i", &w, &h)!=2 || w < 1 || h < 1)
2004-10-12 21:28:27 +00:00
goto bad_file;
1998-09-29 22:36:29 +00:00
GETL();
if (sscanf(buffer, "%i", &m)!=1 || m < 1)
2004-10-12 21:28:27 +00:00
goto bad_file;
1998-09-29 22:36:29 +00:00
if (type=='5')
2004-10-12 21:28:27 +00:00
image = load_graymap(file_name, file, w, h, m, type=='5');
1998-09-29 22:36:29 +00:00
else if (type=='6')
2004-10-12 21:28:27 +00:00
image = load_pixmap(file_name, file, w, h, m, type=='6');
1998-09-29 22:36:29 +00:00
fclose(file);
return image;
2004-10-12 21:28:27 +00:00
bad_file:
short_file:
1998-10-21 14:43:47 +00:00
RErrorCode = RERR_BADIMAGEFILE;
1998-09-29 22:36:29 +00:00
fclose(file);
return NULL;
}