Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / ppm.c
1 /* ppm.c - load PPM image from file
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 *
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.
11 *
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.
16 *
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.
20 */
21
22 #include <config.h>
23
24 #include <X11/Xlib.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "wraster.h"
30
31 static RImage *load_graymap(char *file_name, FILE * file, int w, int h, int max, int raw)
32 {
33 RImage *image;
34
35 image = RCreateImage(w, h, 0);
36 if (!image) {
37 return NULL;
38 }
39 if (!raw) {
40
41 } else {
42 if (max < 256) {
43 unsigned char *ptr;
44 char *buf;
45 int x, y;
46
47 buf = malloc(w + 1);
48 if (!buf) {
49 return NULL;
50 }
51
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;
57 }
58 for (x = 0; x < w; x++) {
59 *(ptr++) = buf[x];
60 *(ptr++) = buf[x];
61 *(ptr++) = buf[x];
62 }
63 }
64 free(buf);
65 } else {
66
67 }
68 }
69
70 return image;
71
72 short_file:
73 RErrorCode = RERR_BADIMAGEFILE;
74 return NULL;
75 }
76
77 static RImage *load_pixmap(char *file_name, FILE * file, int w, int h, int max, int raw)
78 {
79 RImage *image;
80 int i;
81 char buf[3];
82 unsigned char *ptr;
83
84 image = RCreateImage(w, h, 0);
85 if (!image) {
86 return NULL;
87 }
88 ptr = image->data;
89 if (!raw) {
90
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++;
101 }
102 } else {
103
104 }
105 }
106
107 return image;
108
109 short_file:
110 RErrorCode = RERR_BADIMAGEFILE;
111 return NULL;
112 }
113
114 RImage *RLoadPPM(RContext * context, char *file_name, int index)
115 {
116 FILE *file;
117 RImage *image = NULL;
118 char buffer[256];
119 int w, h, m;
120 int type;
121
122 #define GETL() if (!fgets(buffer, 255, file)) goto short_file
123
124 file = fopen(file_name, "rb");
125 if (!file) {
126 RErrorCode = RERR_OPEN;
127 return NULL;
128 }
129
130 /* get signature */
131 GETL();
132
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;
138 }
139
140 type = buffer[1];
141
142 /* skip comments */
143 while (1) {
144 GETL();
145
146 if (buffer[0] != '#')
147 break;
148 }
149
150 /* get size */
151 if (sscanf(buffer, "%i %i", &w, &h) != 2 || w < 1 || h < 1)
152 goto bad_file;
153
154 GETL();
155 if (sscanf(buffer, "%i", &m) != 1 || m < 1)
156 goto bad_file;
157
158 if (type == '5')
159 image = load_graymap(file_name, file, w, h, m, type == '5');
160 else if (type == '6')
161 image = load_pixmap(file_name, file, w, h, m, type == '6');
162
163 fclose(file);
164 return image;
165
166 bad_file:
167 short_file:
168 RErrorCode = RERR_BADIMAGEFILE;
169 fclose(file);
170 return NULL;
171 }