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.
31 static RImage
*load_graymap(FILE * file
, int w
, int h
, int max
, int raw
)
35 image
= RCreateImage(w
, h
, 0);
53 for (y
= 0; y
< h
; y
++) {
54 if (!fread(buf
, w
, 1, file
)) {
58 for (x
= 0; x
< w
; x
++) {
73 RErrorCode
= RERR_BADIMAGEFILE
;
77 static RImage
*load_pixmap(FILE * file
, int w
, int h
, int max
, int raw
)
84 image
= RCreateImage(w
, h
, 0);
95 if (fread(buf
, 1, 3, file
) != 3)
110 RErrorCode
= RERR_BADIMAGEFILE
;
114 RImage
*RLoadPPM(char *file_name
)
117 RImage
*image
= NULL
;
122 #define GETL() if (!fgets(buffer, 255, file)) goto short_file
124 file
= fopen(file_name
, "rb");
126 RErrorCode
= RERR_OPEN
;
133 /* only accept raw pixmaps or graymaps */
134 if (buffer
[0] != 'P' || (buffer
[1] != '5' && buffer
[1] != '6')) {
135 RErrorCode
= RERR_BADFORMAT
;
146 if (buffer
[0] != '#')
151 if (sscanf(buffer
, "%i %i", &w
, &h
) != 2 || w
< 1 || h
< 1)
155 if (sscanf(buffer
, "%i", &m
) != 1 || m
< 1)
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');
168 RErrorCode
= RERR_BADIMAGEFILE
;