| 1 |
/* Copyright (C) 2008 Vincent Penquerc'h. |
| 2 |
This file is part of the Kate codec library. |
| 3 |
Written by Vincent Penquerc'h. |
| 4 |
|
| 5 |
Use, distribution and reproduction of this library is governed |
| 6 |
by a BSD style source license included with this source in the |
| 7 |
file 'COPYING'. Please read these terms before distributing. */ |
| 8 |
|
| 9 |
|
| 10 |
/* adapted from example.c from the libpng distribution */ |
| 11 |
|
| 12 |
#include "config.h" |
| 13 |
|
| 14 |
#include <stdio.h> |
| 15 |
#ifdef HAVE_STDLIB_H |
| 16 |
#include <stdlib.h> |
| 17 |
#endif |
| 18 |
#ifdef HAVE_STRING_H |
| 19 |
#include <string.h> |
| 20 |
#endif |
| 21 |
#include <errno.h> |
| 22 |
#include <png.h> |
| 23 |
#include "kate/kate.h" |
| 24 |
|
| 25 |
static FILE *kd_open(const char *filename) |
| 26 |
{ |
| 27 |
FILE *f; |
| 28 |
size_t bytes; |
| 29 |
unsigned char magic[8]; |
| 30 |
|
| 31 |
if (!filename) { |
| 32 |
fprintf(stderr,"No filename specified\n"); |
| 33 |
return NULL; |
| 34 |
} |
| 35 |
|
| 36 |
f=fopen(filename,"rb"); |
| 37 |
if (!f) { |
| 38 |
fprintf(stderr,"Failed to open %s: %s\n",filename,strerror(errno)); |
| 39 |
return NULL; |
| 40 |
} |
| 41 |
|
| 42 |
bytes=fread(magic,1,8,f); |
| 43 |
if (bytes<8) { |
| 44 |
fprintf(stderr,"Failed to read signature bytes from file\n"); |
| 45 |
goto error; |
| 46 |
} |
| 47 |
if (png_sig_cmp(magic,0,8)) { |
| 48 |
fprintf(stderr,"Signature bytes do not match PNG\n"); |
| 49 |
goto error; |
| 50 |
} |
| 51 |
|
| 52 |
return f; |
| 53 |
|
| 54 |
error: |
| 55 |
fclose(f); |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
int kd_read_png8(const char *filename,int *w,int *h,int *bpp,kate_color **palette,int *ncolors,unsigned char **pixels) |
| 60 |
{ |
| 61 |
/* making f volatile to avoid warning: variable 'fp' might be |
| 62 |
clobbered by 'longjmp' or 'vfork' */ |
| 63 |
FILE * volatile f=NULL; |
| 64 |
png_structp png_ptr=NULL; |
| 65 |
png_infop info_ptr=NULL; |
| 66 |
int cdepth,ctype; |
| 67 |
int channels; |
| 68 |
int width,height; |
| 69 |
png_bytep *row_pointers=NULL; |
| 70 |
int transforms=PNG_TRANSFORM_STRIP_16; |
| 71 |
png_colorp png_palette; |
| 72 |
int x,y,n,num_palette; |
| 73 |
png_bytep trans; |
| 74 |
int num_trans; |
| 75 |
int has_trans; |
| 76 |
|
| 77 |
f=kd_open(filename); |
| 78 |
if (!f) return -1; |
| 79 |
|
| 80 |
png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); |
| 81 |
if (!png_ptr) { |
| 82 |
fprintf(stderr,"Cannot created PNG read structure\n"); |
| 83 |
goto error; |
| 84 |
} |
| 85 |
|
| 86 |
info_ptr=png_create_info_struct(png_ptr); |
| 87 |
if (!info_ptr) { |
| 88 |
fprintf(stderr,"Cannot created PNG info structure\n"); |
| 89 |
goto error; |
| 90 |
} |
| 91 |
|
| 92 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 93 |
fprintf(stderr,"PNG read failed\n"); |
| 94 |
goto error; |
| 95 |
} |
| 96 |
|
| 97 |
png_init_io(png_ptr,f); |
| 98 |
png_set_sig_bytes(png_ptr,8); |
| 99 |
png_read_png(png_ptr,info_ptr,transforms,NULL); |
| 100 |
|
| 101 |
channels=png_get_channels(png_ptr,info_ptr); |
| 102 |
if (channels!=1) { |
| 103 |
fprintf(stderr,"Channels is not 1 (%d)",channels); |
| 104 |
goto error; |
| 105 |
} |
| 106 |
|
| 107 |
ctype=png_get_color_type(png_ptr,info_ptr); |
| 108 |
if (!(ctype&PNG_COLOR_MASK_PALETTE)) { |
| 109 |
fprintf(stderr,"Image is not paletted\n"); |
| 110 |
goto error; |
| 111 |
} |
| 112 |
if (!(ctype&PNG_COLOR_MASK_COLOR)) { |
| 113 |
fprintf(stderr,"Image does not contain color information\n"); |
| 114 |
goto error; |
| 115 |
} |
| 116 |
|
| 117 |
cdepth=png_get_bit_depth(png_ptr,info_ptr); |
| 118 |
if (cdepth>8) { |
| 119 |
fprintf(stderr,"Color depth is higher than 8\n"); |
| 120 |
goto error; |
| 121 |
} |
| 122 |
|
| 123 |
width=png_get_image_width(png_ptr,info_ptr); |
| 124 |
height=png_get_image_height(png_ptr,info_ptr); |
| 125 |
|
| 126 |
if (!png_get_PLTE(png_ptr,info_ptr,&png_palette,&num_palette)) { |
| 127 |
fprintf(stderr,"Failed to retrieve palette data\n"); |
| 128 |
goto error; |
| 129 |
} |
| 130 |
|
| 131 |
/* this may or may not be there */ |
| 132 |
has_trans=(png_get_tRNS(png_ptr,info_ptr,&trans,&num_trans,NULL)&PNG_INFO_tRNS); |
| 133 |
|
| 134 |
row_pointers=png_get_rows(png_ptr,info_ptr); |
| 135 |
if (!row_pointers) { |
| 136 |
fprintf(stderr,"Failed to retrieve image data\n"); |
| 137 |
goto error; |
| 138 |
} |
| 139 |
|
| 140 |
/* allocate memory */ |
| 141 |
if (palette) { |
| 142 |
*palette=(kate_color*)kate_malloc(num_palette*sizeof(kate_color)); |
| 143 |
if (!*palette) { |
| 144 |
fprintf(stderr,"Not enough memory to allocate space for palette (%d colors)\n",num_palette); |
| 145 |
goto error; |
| 146 |
} |
| 147 |
} |
| 148 |
if (pixels) { |
| 149 |
*pixels=(unsigned char*)kate_malloc(width*height); |
| 150 |
if (!*pixels) { |
| 151 |
fprintf(stderr,"Not enough memory to allocate space for pixels (%dx%d)\n",width,height); |
| 152 |
if (palette) kate_free(*palette); |
| 153 |
goto error; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/* fill data */ |
| 158 |
if (w) *w=width; |
| 159 |
if (h) *h=height; |
| 160 |
if (bpp) *bpp=cdepth; |
| 161 |
if (palette) { |
| 162 |
for (n=0;n<num_palette;++n) { |
| 163 |
(*palette)[n].r=png_palette[n].red; |
| 164 |
(*palette)[n].g=png_palette[n].green; |
| 165 |
(*palette)[n].b=png_palette[n].blue; |
| 166 |
(*palette)[n].a=(has_trans && n<num_trans)?trans[n]:255; |
| 167 |
} |
| 168 |
} |
| 169 |
if (ncolors) *ncolors=num_palette; |
| 170 |
if (pixels) { |
| 171 |
/* the PNG_TRANSFORM_PACKING and PNG_TRANSFORM_PACKSWAP bits don't seem to do anything to my 4 bit images, |
| 172 |
so do it by hand */ |
| 173 |
int ppb=8/cdepth; |
| 174 |
int mask=(1<<cdepth)-1; |
| 175 |
n=0; |
| 176 |
for (y=0;y<height;++y) { |
| 177 |
unsigned char *row=row_pointers[y]; |
| 178 |
for (x=0;x<width;++x) { |
| 179 |
(*pixels)[n++]=(row[x/ppb]>>((ppb-1-x%ppb)*cdepth))&mask; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
png_destroy_read_struct(&png_ptr,&info_ptr,NULL); |
| 185 |
fclose(f); |
| 186 |
|
| 187 |
return 0; |
| 188 |
|
| 189 |
error: |
| 190 |
if (png_ptr) png_destroy_read_struct(&png_ptr,info_ptr?&info_ptr:NULL,NULL); |
| 191 |
if (f) fclose(f); |
| 192 |
return -1; |
| 193 |
} |
| 194 |
|
| 195 |
int kd_read_png(const char *filename,int *w,int *h,unsigned char **pixels,size_t *size) |
| 196 |
{ |
| 197 |
FILE *f; |
| 198 |
png_structp png_ptr=NULL; |
| 199 |
png_infop info_ptr=NULL; |
| 200 |
long sz; |
| 201 |
|
| 202 |
f=kd_open(filename); |
| 203 |
if (!f) return -1; |
| 204 |
|
| 205 |
png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); |
| 206 |
if (!png_ptr) { |
| 207 |
fprintf(stderr,"Cannot created PNG read structure\n"); |
| 208 |
goto error; |
| 209 |
} |
| 210 |
|
| 211 |
info_ptr=png_create_info_struct(png_ptr); |
| 212 |
if (!info_ptr) { |
| 213 |
fprintf(stderr,"Cannot created PNG info structure\n"); |
| 214 |
goto error; |
| 215 |
} |
| 216 |
|
| 217 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 218 |
fprintf(stderr,"PNG read failed\n"); |
| 219 |
goto error; |
| 220 |
} |
| 221 |
|
| 222 |
png_init_io(png_ptr,f); |
| 223 |
png_set_sig_bytes(png_ptr,8); |
| 224 |
png_read_info(png_ptr,info_ptr); |
| 225 |
|
| 226 |
if (w) *w=png_get_image_width(png_ptr,info_ptr); |
| 227 |
if (h) *h=png_get_image_height(png_ptr,info_ptr); |
| 228 |
|
| 229 |
png_destroy_read_struct(&png_ptr,&info_ptr,NULL); |
| 230 |
|
| 231 |
/* now read the whole file as a binary blob */ |
| 232 |
fseek(f,0,SEEK_END); |
| 233 |
sz=ftell(f); |
| 234 |
fseek(f,0,SEEK_SET); |
| 235 |
*pixels=malloc(sz); |
| 236 |
if (!*pixels) goto error; |
| 237 |
if ((long)fread(*pixels,1,sz,f)!=sz) { |
| 238 |
fprintf(stderr,"Failed to read %ld bytes\n",sz); |
| 239 |
free(*pixels); |
| 240 |
*pixels=NULL; |
| 241 |
goto error; |
| 242 |
} |
| 243 |
|
| 244 |
if (size) *size=sz; |
| 245 |
|
| 246 |
fclose(f); |
| 247 |
|
| 248 |
return 0; |
| 249 |
|
| 250 |
error: |
| 251 |
if (png_ptr) png_destroy_read_struct(&png_ptr,info_ptr?&info_ptr:NULL,NULL); |
| 252 |
if (f) fclose(f); |
| 253 |
return -1; |
| 254 |
} |
| 255 |
|