wmaker: Created an array to hold the maximize menu entries
[wmaker-crm.git] / wrlib / load_ppm.c
blob49c10dbb01341743807ed08afadb30e228a13ac3
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;
36 unsigned char *ptr;
37 char *buf;
38 int x, y;
40 image = RCreateImage(w, h, 0);
41 if (!image)
42 return NULL;
44 if (!raw)
45 return image;
47 if (max < 256) {
48 buf = malloc(w + 1);
49 if (!buf)
50 return NULL;
52 ptr = image->data;
53 for (y = 0; y < h; y++) {
54 if (!fread(buf, w, 1, file)) {
55 free(buf);
56 RErrorCode = RERR_BADIMAGEFILE;
57 return NULL;
60 for (x = 0; x < w; x++) {
61 *(ptr++) = buf[x];
62 *(ptr++) = buf[x];
63 *(ptr++) = buf[x];
66 free(buf);
69 return image;
72 static RImage *load_pixmap(FILE *file, int w, int h, int max, int raw)
74 RImage *image;
75 int i;
76 char buf[3];
77 unsigned char *ptr;
79 image = RCreateImage(w, h, 0);
80 if (!image)
81 return NULL;
83 if (!raw)
84 return image;
86 ptr = image->data;
87 if (max < 256) {
88 i = 0;
89 while (i < w * h) {
90 if (fread(buf, 1, 3, file) != 3) {
91 RErrorCode = RERR_BADIMAGEFILE;
92 return NULL;
95 *(ptr++) = buf[0];
96 *(ptr++) = buf[1];
97 *(ptr++) = buf[2];
98 i++;
102 return image;
105 RImage *RLoadPPM(const char *file_name)
107 FILE *file;
108 RImage *image = NULL;
109 char buffer[256];
110 int w, h, m;
111 int type;
113 file = fopen(file_name, "rb");
114 if (!file) {
115 RErrorCode = RERR_OPEN;
116 return NULL;
119 /* get signature */
120 if (!fgets(buffer, 255, file)) {
121 RErrorCode = RERR_BADIMAGEFILE;
122 fclose(file);
123 return NULL;
126 /* only accept raw pixmaps or graymaps */
127 if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
128 RErrorCode = RERR_BADFORMAT;
129 fclose(file);
130 return NULL;
133 type = buffer[1];
135 /* skip comments */
136 while (1) {
137 if (!fgets(buffer, 255, file)) {
138 RErrorCode = RERR_BADIMAGEFILE;
139 fclose(file);
140 return NULL;
143 if (buffer[0] != '#')
144 break;
147 /* get size */
148 if (sscanf(buffer, "%i %i", &w, &h) != 2 || w < 1 || h < 1) {
149 /* Short file */
150 RErrorCode = RERR_BADIMAGEFILE;
151 fclose(file);
152 return NULL;
155 if (!fgets(buffer, 255, file)) {
156 RErrorCode = RERR_BADIMAGEFILE;
157 fclose(file);
158 return NULL;
161 if (sscanf(buffer, "%i", &m) != 1 || m < 1) {
162 /* Short file */
163 RErrorCode = RERR_BADIMAGEFILE;
164 fclose(file);
165 return NULL;
168 if (type == '5')
169 image = load_graymap(file, w, h, m, type == '5');
170 else if (type == '6')
171 image = load_pixmap(file, w, h, m, type == '6');
173 fclose(file);
174 return image;