wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / wrlib / ppm.c
blob63b7bcf9f57a4c4ae52f119b016dbc811abd430b
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., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include <config.h>
25 #include <X11/Xlib.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "wraster.h"
31 #include "imgformat.h"
33 static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)
35 RImage *image;
37 image = RCreateImage(w, h, 0);
38 if (!image) {
39 return NULL;
41 if (!raw) {
43 } else {
44 if (max < 256) {
45 unsigned char *ptr;
46 char *buf;
47 int x, y;
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;
79 static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
81 RImage *image;
82 int i;
83 char buf[3];
84 unsigned char *ptr;
86 image = RCreateImage(w, h, 0);
87 if (!image) {
88 return NULL;
90 ptr = image->data;
91 if (!raw) {
93 } else {
94 if (max < 256) {
95 i = 0;
96 while (i < w * h) {
97 if (fread(buf, 1, 3, file) != 3)
98 goto short_file;
99 *(ptr++) = buf[0];
100 *(ptr++) = buf[1];
101 *(ptr++) = buf[2];
102 i++;
104 } else {
109 return image;
111 short_file:
112 RErrorCode = RERR_BADIMAGEFILE;
113 return NULL;
116 RImage *RLoadPPM(const char *file_name)
118 FILE *file;
119 RImage *image = NULL;
120 char buffer[256];
121 int w, h, m;
122 int type;
124 #define GETL() if (!fgets(buffer, 255, file)) goto short_file
126 file = fopen(file_name, "rb");
127 if (!file) {
128 RErrorCode = RERR_OPEN;
129 return NULL;
132 /* get signature */
133 GETL();
135 /* only accept raw pixmaps or graymaps */
136 if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
137 RErrorCode = RERR_BADFORMAT;
138 fclose(file);
139 return NULL;
142 type = buffer[1];
144 /* skip comments */
145 while (1) {
146 GETL();
148 if (buffer[0] != '#')
149 break;
152 /* get size */
153 if (sscanf(buffer, "%i %i", &w, &h) != 2 || w < 1 || h < 1)
154 goto bad_file;
156 GETL();
157 if (sscanf(buffer, "%i", &m) != 1 || m < 1)
158 goto bad_file;
160 if (type == '5')
161 image = load_graymap(file, w, h, m, type == '5');
162 else if (type == '6')
163 image = load_pixmap(file, w, h, m, type == '6');
165 fclose(file);
166 return image;
168 bad_file:
169 short_file:
170 RErrorCode = RERR_BADIMAGEFILE;
171 fclose(file);
172 return NULL;