Updating to version 0.20.2
[wmaker-crm.git] / wrlib / ppm.c
blobb33169002b8a1c0f6997efafb1eb6054ef7d26bd
1 /* ppm.c - load PPM image from file
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1997 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 if (!fgets(image->data[0], w*h, file))
47 goto short_file;
48 memcpy(image->data[0], image->data[1], w*h);
49 memcpy(image->data[0], image->data[2], w*h);
50 } else {
55 return image;
57 short_file:
58 RErrorCode = RERR_BADIMAGEFILE;
59 return NULL;
63 static RImage*
64 load_pixmap(char *file_name, FILE *file, int w, int h, int max, int raw)
66 RImage *image;
67 int i;
68 char buf[3];
69 char *r, *g, *b;
71 image = RCreateImage(w, h, 0);
72 if (!image) {
73 return NULL;
75 r = image->data[0];
76 g = image->data[1];
77 b = image->data[2];
78 if (!raw) {
80 } else {
81 if (max<256) {
82 i = 0;
83 while (i < w*h) {
84 if (fread(buf, 1, 3, file)!=3)
85 goto short_file;
86 *(r++) = buf[0];
87 *(g++) = buf[1];
88 *(b++) = buf[2];
89 i++;
91 } else {
96 return image;
98 short_file:
99 RErrorCode = RERR_BADIMAGEFILE;
100 return NULL;
104 RImage*
105 RLoadPPM(RContext *context, char *file_name, int index)
107 FILE *file;
108 RImage *image = NULL;
109 char buffer[256];
110 int w, h, m;
111 int type;
113 #define GETL() if (!fgets(buffer, 255, file)) goto short_file
115 file = fopen(file_name, "r");
116 if (!file) {
117 RErrorCode = RERR_OPEN;
118 return NULL;
121 /* get signature */
122 GETL();
124 /* only accept raw pixmaps or graymaps */
125 if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
126 RErrorCode = RERR_BADFORMAT;
127 fclose(file);
128 return NULL;
131 type = buffer[1];
133 /* skip comments */
134 while (1) {
135 GETL();
137 if (buffer[0]!='#')
138 break;
141 /* get size */
142 if (sscanf(buffer, "%i %i", &w, &h)!=2)
143 goto bad_file;
146 GETL();
147 if (sscanf(buffer, "%i", &m)!=1 || m < 1)
148 goto bad_file;
150 if (type=='5')
151 image = load_graymap(file_name, file, w, h, m, type=='5');
152 else if (type=='6')
153 image = load_pixmap(file_name, file, w, h, m, type=='6');
155 fclose(file);
156 return image;
158 bad_file:
159 short_file:
160 RErrorCode = RERR_BADIMAGEFILE;
161 fclose(file);
162 return NULL;