Only cache pixmaps for docked icons
[wmaker-crm.git] / wrlib / ppm.c
blobd89d91729f9f1e2acc69aa7e2794b108fd07987f
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>
24 #include <X11/Xlib.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "wraster.h"
31 static RImage *load_graymap(FILE * file, int w, int h, int max, int raw)
33 RImage *image;
35 image = RCreateImage(w, h, 0);
36 if (!image) {
37 return NULL;
39 if (!raw) {
41 } else {
42 if (max < 256) {
43 unsigned char *ptr;
44 char *buf;
45 int x, y;
47 buf = malloc(w + 1);
48 if (!buf) {
49 return NULL;
52 ptr = image->data;
53 for (y = 0; y < h; y++) {
54 if (!fread(buf, w, 1, file)) {
55 free(buf);
56 goto short_file;
58 for (x = 0; x < w; x++) {
59 *(ptr++) = buf[x];
60 *(ptr++) = buf[x];
61 *(ptr++) = buf[x];
64 free(buf);
65 } else {
70 return image;
72 short_file:
73 RErrorCode = RERR_BADIMAGEFILE;
74 return NULL;
77 static RImage *load_pixmap(FILE * file, int w, int h, int max, int raw)
79 RImage *image;
80 int i;
81 char buf[3];
82 unsigned char *ptr;
84 image = RCreateImage(w, h, 0);
85 if (!image) {
86 return NULL;
88 ptr = image->data;
89 if (!raw) {
91 } else {
92 if (max < 256) {
93 i = 0;
94 while (i < w * h) {
95 if (fread(buf, 1, 3, file) != 3)
96 goto short_file;
97 *(ptr++) = buf[0];
98 *(ptr++) = buf[1];
99 *(ptr++) = buf[2];
100 i++;
102 } else {
107 return image;
109 short_file:
110 RErrorCode = RERR_BADIMAGEFILE;
111 return NULL;
114 RImage *RLoadPPM(char *file_name)
116 FILE *file;
117 RImage *image = NULL;
118 char buffer[256];
119 int w, h, m;
120 int type;
122 #define GETL() if (!fgets(buffer, 255, file)) goto short_file
124 file = fopen(file_name, "rb");
125 if (!file) {
126 RErrorCode = RERR_OPEN;
127 return NULL;
130 /* get signature */
131 GETL();
133 /* only accept raw pixmaps or graymaps */
134 if (buffer[0] != 'P' || (buffer[1] != '5' && buffer[1] != '6')) {
135 RErrorCode = RERR_BADFORMAT;
136 fclose(file);
137 return NULL;
140 type = buffer[1];
142 /* skip comments */
143 while (1) {
144 GETL();
146 if (buffer[0] != '#')
147 break;
150 /* get size */
151 if (sscanf(buffer, "%i %i", &w, &h) != 2 || w < 1 || h < 1)
152 goto bad_file;
154 GETL();
155 if (sscanf(buffer, "%i", &m) != 1 || m < 1)
156 goto bad_file;
158 if (type == '5')
159 image = load_graymap(file, w, h, m, type == '5');
160 else if (type == '6')
161 image = load_pixmap(file, w, h, m, type == '6');
163 fclose(file);
164 return image;
166 bad_file:
167 short_file:
168 RErrorCode = RERR_BADIMAGEFILE;
169 fclose(file);
170 return NULL;