Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / png / contrib / gregbook / readppm.c
blobbe9a56d95fd2a350df9b1593d7af62279646cb52
1 /*---------------------------------------------------------------------------
3 rpng - simple PNG display program readppm.c
5 ---------------------------------------------------------------------------
7 This is a special-purpose replacement for readpng.c that allows binary
8 PPM files to be used in place of PNG images.
10 ---------------------------------------------------------------------------
12 Copyright (c) 1998-2007 Greg Roelofs. All rights reserved.
14 This software is provided "as is," without warranty of any kind,
15 express or implied. In no event shall the author or contributors
16 be held liable for any damages arising in any way from the use of
17 this software.
19 The contents of this file are DUAL-LICENSED. You may modify and/or
20 redistribute this software according to the terms of one of the
21 following two licenses (at your option):
24 LICENSE 1 ("BSD-like with advertising clause"):
26 Permission is granted to anyone to use this software for any purpose,
27 including commercial applications, and to alter it and redistribute
28 it freely, subject to the following restrictions:
30 1. Redistributions of source code must retain the above copyright
31 notice, disclaimer, and this list of conditions.
32 2. Redistributions in binary form must reproduce the above copyright
33 notice, disclaimer, and this list of conditions in the documenta-
34 tion and/or other materials provided with the distribution.
35 3. All advertising materials mentioning features or use of this
36 software must display the following acknowledgment:
38 This product includes software developed by Greg Roelofs
39 and contributors for the book, "PNG: The Definitive Guide,"
40 published by O'Reilly and Associates.
43 LICENSE 2 (GNU GPL v2 or later):
45 This program is free software; you can redistribute it and/or modify
46 it under the terms of the GNU General Public License as published by
47 the Free Software Foundation; either version 2 of the License, or
48 (at your option) any later version.
50 This program is distributed in the hope that it will be useful,
51 but WITHOUT ANY WARRANTY; without even the implied warranty of
52 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 GNU General Public License for more details.
55 You should have received a copy of the GNU General Public License
56 along with this program; if not, write to the Free Software Foundation,
57 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
59 ---------------------------------------------------------------------------*/
61 #include <stdio.h>
62 #include <stdlib.h>
64 #include "readpng.h" /* typedefs, common macros, public prototypes */
67 ulg width, height;
68 int bit_depth, color_type, channels;
69 uch *image_data = NULL;
70 FILE *saved_infile;
73 void readpng_version_info()
75 fprintf(stderr, " Compiled without libpng, zlib or PBMPLUS/NetPBM.\n");
79 /* return value = 0 for success, 1 for bad sig, 2 for bad IHDR, 4 for no mem */
81 int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight)
83 static uch ppmline[256];
84 int maxval;
87 saved_infile = infile;
89 fgets(ppmline, 256, infile);
90 if (ppmline[0] != 'P' || ppmline[1] != '6') {
91 fprintf(stderr, "ERROR: not a PPM file\n");
92 return 1;
94 /* possible color types: P5 = grayscale (0), P6 = RGB (2), P8 = RGBA (6) */
95 if (ppmline[1] == '6') {
96 color_type = 2;
97 channels = 3;
98 } else if (ppmline[1] == '8') {
99 color_type = 6;
100 channels = 4;
101 } else /* if (ppmline[1] == '5') */ {
102 color_type = 0;
103 channels = 1;
106 do {
107 fgets(ppmline, 256, infile);
108 } while (ppmline[0] == '#');
109 sscanf(ppmline, "%lu %lu", &width, &height);
111 do {
112 fgets(ppmline, 256, infile);
113 } while (ppmline[0] == '#');
114 sscanf(ppmline, "%d", &maxval);
115 if (maxval != 255) {
116 fprintf(stderr, "ERROR: maxval = %d\n", maxval);
117 return 2;
119 bit_depth = 8;
121 *pWidth = width;
122 *pHeight = height;
124 return 0;
130 /* returns 0 if succeeds, 1 if fails due to no bKGD chunk, 2 if libpng error;
131 * scales values to 8-bit if necessary */
133 int readpng_get_bgcolor(uch *red, uch *green, uch *blue)
135 return 1;
141 /* display_exponent == LUT_exponent * CRT_exponent */
143 uch *readpng_get_image(double display_exponent, int *pChannels, ulg *pRowbytes)
145 ulg rowbytes;
148 /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
149 * transparency chunks to full alpha channel; strip 16-bit-per-sample
150 * images to 8 bits per sample; and convert grayscale to RGB[A] */
152 /* GRR WARNING: grayscale needs to be expanded and channels reset! */
154 *pRowbytes = rowbytes = channels*width;
155 *pChannels = channels;
157 if ((image_data = (uch *)malloc(rowbytes*height)) == NULL) {
158 return NULL;
161 Trace((stderr, "readpng_get_image: rowbytes = %ld, height = %ld\n", rowbytes, height));
164 /* now we can go ahead and just read the whole image */
166 fread(image_data, 1L, rowbytes*height, saved_infile);
169 return image_data;
173 void readpng_cleanup(int free_image_data)
175 if (free_image_data && image_data) {
176 free(image_data);
177 image_data = NULL;