123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "readpng.h"
- ulg width, height;
- int bit_depth, color_type, channels;
- uch *image_data = NULL;
- FILE *saved_infile;
- void readpng_version_info()
- {
- fprintf(stderr, " Compiled without libpng, zlib or PBMPLUS/NetPBM.\n");
- }
- int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight)
- {
- static uch ppmline[256];
- int maxval;
- saved_infile = infile;
- fgets(ppmline, 256, infile);
- if (ppmline[0] != 'P' || ppmline[1] != '6') {
- fprintf(stderr, "ERROR: not a PPM file\n");
- return 1;
- }
-
- if (ppmline[1] == '6') {
- color_type = 2;
- channels = 3;
- } else if (ppmline[1] == '8') {
- color_type = 6;
- channels = 4;
- } else {
- color_type = 0;
- channels = 1;
- }
- do {
- fgets(ppmline, 256, infile);
- } while (ppmline[0] == '#');
- sscanf(ppmline, "%lu %lu", &width, &height);
- do {
- fgets(ppmline, 256, infile);
- } while (ppmline[0] == '#');
- sscanf(ppmline, "%d", &maxval);
- if (maxval != 255) {
- fprintf(stderr, "ERROR: maxval = %d\n", maxval);
- return 2;
- }
- bit_depth = 8;
- *pWidth = width;
- *pHeight = height;
- return 0;
- }
- int readpng_get_bgcolor(uch *red, uch *green, uch *blue)
- {
- return 1;
- }
- uch *readpng_get_image(double display_exponent, int *pChannels, ulg *pRowbytes)
- {
- ulg rowbytes;
-
-
- *pRowbytes = rowbytes = channels*width;
- *pChannels = channels;
- Trace((stderr, "readpng_get_image: rowbytes = %ld, height = %ld\n", rowbytes, height));
-
- if (height > ((size_t)(-1))/rowbytes) {
- fprintf(stderr, PROGNAME ": image_data buffer would be too large\n",
- return NULL;
- }
- if ((image_data = (uch *)malloc(rowbytes*height)) == NULL) {
- return NULL;
- }
-
- if (fread(image_data, 1L, rowbytes*height, saved_infile) <
- rowbytes*height) {
- free (image_data);
- image_data = NULL;
- return NULL;
- }
- return image_data;
- }
- void readpng_cleanup(int free_image_data)
- {
- if (free_image_data && image_data) {
- free(image_data);
- image_data = NULL;
- }
- }
|