- Fixed crashing bug in menu.c
[wmaker-crm.git] / wrlib / ppm.c
blob2ab1016aefd28dc19ef666a0ddfcf20965081199
1 /* ppm.c - load PPM image from file
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <config.h>
25 #include <X11/Xlib.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "wraster.h"
33 static RImage*
34 load_graymap(char *file_name, FILE *file, int w, int h, int max, int raw)
36 RImage *image;
38 image = RCreateImage(w, h, 0);
39 if (!image) {
40 return NULL;
42 if (!raw) {
44 } else {
45 if (max<256) {
46 int x, y;
47 char *buf, *ptr;
49 buf = malloc(w+1);
50 if (!buf) {
51 return NULL;
54 ptr = image->data;
55 for (y = 0; y < h; y++) {
56 if (!fread(buf, w, 1, file)) {
57 free(buf);
58 goto short_file;
60 for (x = 0; x < w; x++) {
61 *(ptr++) = buf[x];
62 *(ptr++) = buf[x];
63 *(ptr++) = buf[x];
66 free(buf);
67 } else {
72 return image;
74 short_file:
75 RErrorCode = RERR_BADIMAGEFILE;
76 return NULL;
80 static RImage*
81 load_pixmap(char *file_name, FILE *file, int w, int h, int max, int raw)
83 RImage *image;
84 int i;
85 char buf[3];
86 char *ptr;
88 image = RCreateImage(w, h, 0);
89 if (!image) {
90 return NULL;
92 ptr = image->data;
93 if (!raw) {
95 } else {
96 if (max<256) {
97 i = 0;
98 while (i < w*h) {
99 if (fread(buf, 1, 3, file)!=3)
100 goto short_file;
101 *(ptr++) = buf[0];
102 *(ptr++) = buf[1];
103 *(ptr++) = buf[2];
104 i++;
106 } else {
111 return image;
113 short_file:
114 RErrorCode = RERR_BADIMAGEFILE;
115 return NULL;
119 RImage*
120 RLoadPPM(RContext *context, char *file_name, int index)
122 FILE *file;
123 RImage *image = NULL;
124 char buffer[256];
125 int w, h, m;
126 int type;
128 #define GETL() if (!fgets(buffer, 255, file)) goto short_file
130 file = fopen(file_name, "rb");
131 if (!file) {
132 RErrorCode = RERR_OPEN;
133 return NULL;
136 /* get signature */
137 GETL();
139 /* only accept raw pixmaps or graymaps */
140 if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
141 RErrorCode = RERR_BADFORMAT;
142 fclose(file);
143 return NULL;
146 type = buffer[1];
148 /* skip comments */
149 while (1) {
150 GETL();
152 if (buffer[0]!='#')
153 break;
156 /* get size */
157 if (sscanf(buffer, "%i %i", &w, &h)!=2 || w < 1 || h < 1)
158 goto bad_file;
161 GETL();
162 if (sscanf(buffer, "%i", &m)!=1 || m < 1)
163 goto bad_file;
165 if (type=='5')
166 image = load_graymap(file_name, file, w, h, m, type=='5');
167 else if (type=='6')
168 image = load_pixmap(file_name, file, w, h, m, type=='6');
170 fclose(file);
171 return image;
173 bad_file:
174 short_file:
175 RErrorCode = RERR_BADIMAGEFILE;
176 fclose(file);
177 return NULL;