loadpng.cc (2957B)
1 /* Copyright 2013 Brian Swetland <swetland@frotz.net> 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 20 #include <png.h> 21 22 #include "util.h" 23 24 void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int ch, int inverty) { 25 png_structp png; 26 png_infop info; 27 png_uint_32 w, h; 28 int depth, ctype, itype, i; 29 png_byte *data = 0; 30 FILE *fp; 31 32 if ((fp = fopen(fn, "rb")) == NULL) 33 goto exit; 34 35 if (!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) 36 goto close_and_exit; 37 38 if (!(info = png_create_info_struct(png))) { 39 png_destroy_read_struct(&png, NULL, NULL); 40 goto destroy_and_exit; 41 } 42 43 if (setjmp(png->jmpbuf)) { 44 if (data) { 45 free(data); 46 data = 0; 47 } 48 goto destroy_and_exit; 49 } 50 51 png_init_io(png, fp); 52 53 png_read_info(png, info); 54 55 png_get_IHDR(png, info, &w, &h, &depth, &ctype, &itype, NULL, NULL); 56 57 if (depth < 8) 58 png_set_packing(png); 59 60 if (depth > 8) 61 png_set_strip_16(png); 62 63 if (ctype == PNG_COLOR_TYPE_PALETTE) 64 png_set_expand(png); 65 66 if ((ctype == PNG_COLOR_TYPE_GRAY) && (depth < 8)) 67 png_set_expand(png); 68 69 if (png_get_valid(png, info, PNG_INFO_tRNS)) 70 png_set_expand(png); 71 72 if (ch == 4) { 73 if ((ctype == PNG_COLOR_TYPE_RGB) || 74 (ctype == PNG_COLOR_TYPE_GRAY)) 75 png_set_add_alpha(png, 0xFF, PNG_FILLER_AFTER); 76 77 if ((ctype == PNG_COLOR_TYPE_GRAY) || 78 (ctype == PNG_COLOR_TYPE_GRAY_ALPHA)) 79 png_set_gray_to_rgb(png); 80 } else if (ch == 1) { 81 if ((ctype == PNG_COLOR_TYPE_RGB) || 82 (ctype == PNG_COLOR_TYPE_RGB_ALPHA)) 83 png_set_rgb_to_gray_fixed(png, 1, -1, -1); 84 85 png_set_strip_alpha(png); 86 } else { 87 png_error(png, "unsupported channel count"); 88 } 89 90 if (!(data = (png_byte*) malloc(w * h * ch))) 91 png_error(png, "cannot allocate image buffer"); 92 93 if (inverty) 94 for (i = h-1; i >= 0; i--) 95 png_read_row(png, data + (i * w * ch), NULL); 96 else 97 for (i = 0; i < h; i++) 98 png_read_row(png, data + (i * w * ch), NULL); 99 100 *_width = w; 101 *_height = h; 102 103 destroy_and_exit: 104 png_destroy_read_struct(&png, &info, NULL); 105 106 close_and_exit: 107 fclose(fp); 108 exit: 109 if (!data) 110 fprintf(stderr,"failed to load '%s'\n", fn); 111 return data; 112 } 113 114 void *load_png_rgba(const char *fn, unsigned *_width, unsigned *_height, int texture) { 115 return _load_png(fn, _width, _height, 4, texture); 116 } 117 118 void *load_png_gray(const char *fn, unsigned *_width, unsigned *_height, int texture) { 119 return _load_png(fn, _width, _height, 1, texture); 120 } 121