New function set_icon_image_from_database
[wmaker-crm.git] / wrlib / ppm.c
blobe4bd758408900c091e45114732f41a0be8b24db8
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"
32 static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)
34 RImage *image;
36 image = RCreateImage(w, h, 0);
37 if (!image) {
38 return NULL;
40 if (!raw) {
42 } else {
43 if (max < 256) {
44 unsigned char *ptr;
45 char *buf;
46 int x, y;
48 buf = malloc(w + 1);
49 if (!buf) {
50 return NULL;
53 ptr = image->data;
54 for (y = 0; y < h; y++) {
55 if (!fread(buf, w, 1, file)) {
56 free(buf);
57 goto short_file;
59 for (x = 0; x < w; x++) {
60 *(ptr++) = buf[x];
61 *(ptr++) = buf[x];
62 *(ptr++) = buf[x];
65 free(buf);
66 } else {
71 return image;
73 short_file:
74 RErrorCode = RERR_BADIMAGEFILE;
75 return NULL;
78 static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
80 RImage *image;
81 int i;
82 char buf[3];
83 unsigned char *ptr;
85 image = RCreateImage(w, h, 0);
86 if (!image) {
87 return NULL;
89 ptr = image->data;
90 if (!raw) {
92 } else {
93 if (max < 256) {
94 i = 0;
95 while (i < w * h) {
96 if (fread(buf, 1, 3, file) != 3)
97 goto short_file;
98 *(ptr++) = buf[0];
99 *(ptr++) = buf[1];
100 *(ptr++) = buf[2];
101 i++;
103 } else {
108 return image;
110 short_file:
111 RErrorCode = RERR_BADIMAGEFILE;
112 return NULL;
115 RImage *RLoadPPM(char *file_name)
117 FILE *file;
118 RImage *image = NULL;
119 char buffer[256];
120 int w, h, m;
121 int type;
123 #define GETL() if (!fgets(buffer, 255, file)) goto short_file
125 file = fopen(file_name, "rb");
126 if (!file) {
127 RErrorCode = RERR_OPEN;
128 return NULL;
131 /* get signature */
132 GETL();
134 /* only accept raw pixmaps or graymaps */
135 if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
136 RErrorCode = RERR_BADFORMAT;
137 fclose(file);
138 return NULL;
141 type = buffer[1];
143 /* skip comments */
144 while (1) {
145 GETL();
147 if (buffer[0] != '#')
148 break;
151 /* get size */
152 if (sscanf(buffer, "%i %i", &w, &h) != 2 || w < 1 || h < 1)
153 goto bad_file;
155 GETL();
156 if (sscanf(buffer, "%i", &m) != 1 || m < 1)
157 goto bad_file;
159 if (type == '5')
160 image = load_graymap(file, w, h, m, type == '5');
161 else if (type == '6')
162 image = load_pixmap(file, w, h, m, type == '6');
164 fclose(file);
165 return image;
167 bad_file:
168 short_file:
169 RErrorCode = RERR_BADIMAGEFILE;
170 fclose(file);
171 return NULL;