Trivial cleanup
[wmaker-crm.git] / wrlib / ppm.c
blobe13ca5a0405064ad44a8326deb9ccb15bd341134
1 /* ppm.c - load PPM image from file
3 * Raster graphics library
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 unsigned char *ptr;
47 char *buf;
48 int x, y;
50 buf = malloc(w+1);
51 if (!buf) {
52 return NULL;
55 ptr = image->data;
56 for (y = 0; y < h; y++) {
57 if (!fread(buf, w, 1, file)) {
58 free(buf);
59 goto short_file;
61 for (x = 0; x < w; x++) {
62 *(ptr++) = buf[x];
63 *(ptr++) = buf[x];
64 *(ptr++) = buf[x];
67 free(buf);
68 } else {
73 return image;
75 short_file:
76 RErrorCode = RERR_BADIMAGEFILE;
77 return NULL;
81 static RImage*
82 load_pixmap(char *file_name, FILE *file, int w, int h, int max, int raw)
84 RImage *image;
85 int i;
86 char buf[3];
87 unsigned char *ptr;
89 image = RCreateImage(w, h, 0);
90 if (!image) {
91 return NULL;
93 ptr = image->data;
94 if (!raw) {
96 } else {
97 if (max<256) {
98 i = 0;
99 while (i < w*h) {
100 if (fread(buf, 1, 3, file)!=3)
101 goto short_file;
102 *(ptr++) = buf[0];
103 *(ptr++) = buf[1];
104 *(ptr++) = buf[2];
105 i++;
107 } else {
112 return image;
114 short_file:
115 RErrorCode = RERR_BADIMAGEFILE;
116 return NULL;
120 RImage*
121 RLoadPPM(RContext *context, char *file_name, int index)
123 FILE *file;
124 RImage *image = NULL;
125 char buffer[256];
126 int w, h, m;
127 int type;
129 #define GETL() if (!fgets(buffer, 255, file)) goto short_file
131 file = fopen(file_name, "rb");
132 if (!file) {
133 RErrorCode = RERR_OPEN;
134 return NULL;
137 /* get signature */
138 GETL();
140 /* only accept raw pixmaps or graymaps */
141 if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
142 RErrorCode = RERR_BADFORMAT;
143 fclose(file);
144 return NULL;
147 type = buffer[1];
149 /* skip comments */
150 while (1) {
151 GETL();
153 if (buffer[0]!='#')
154 break;
157 /* get size */
158 if (sscanf(buffer, "%i %i", &w, &h)!=2 || w < 1 || h < 1)
159 goto bad_file;
162 GETL();
163 if (sscanf(buffer, "%i", &m)!=1 || m < 1)
164 goto bad_file;
166 if (type=='5')
167 image = load_graymap(file_name, file, w, h, m, type=='5');
168 else if (type=='6')
169 image = load_pixmap(file_name, file, w, h, m, type=='6');
171 fclose(file);
172 return image;
174 bad_file:
175 short_file:
176 RErrorCode = RERR_BADIMAGEFILE;
177 fclose(file);
178 return NULL;