[go: up one dir, main page]

File: kpng.c

package info (click to toggle)
libkate 0.3.7-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,484 kB
  • ctags: 4,083
  • sloc: ansic: 10,002; sh: 8,853; yacc: 2,250; python: 721; lex: 362; makefile: 239
file content (253 lines) | stat: -rw-r--r-- 6,169 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* Copyright (C) 2008 Vincent Penquerc'h.
   This file is part of the Kate codec library.
   Written by Vincent Penquerc'h.

   Use, distribution and reproduction of this library is governed
   by a BSD style source license included with this source in the
   file 'COPYING'. Please read these terms before distributing. */


/* adapted from example.c from the libpng distribution */

#include "config.h"

#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <errno.h>
#include <png.h>
#include "kate/kate.h"

static FILE *kd_open(const char *filename)
{
  FILE *f;
  size_t bytes;
  unsigned char magic[8];

  if (!filename) {
    fprintf(stderr,"No filename specified\n");
    return NULL;
  }

  f=fopen(filename,"rb");
  if (!f) {
    fprintf(stderr,"Failed to open %s: %s\n",filename,strerror(errno));
    return NULL;
  }

  bytes=fread(magic,1,8,f);
  if (bytes<8) {
    fprintf(stderr,"Failed to read signature bytes from file\n");
    goto error;
  }
  if (png_sig_cmp(magic,0,8)) {
    fprintf(stderr,"Signature bytes do not match PNG\n");
    goto error;
  }

  return f;

error:
  fclose(f);
  return NULL;
}

int kd_read_png8(const char *filename,int *w,int *h,int *bpp,kate_color **palette,int *ncolors,unsigned char **pixels)
{
  FILE *f=NULL;
  png_structp png_ptr=NULL;
  png_infop info_ptr=NULL;
  int cdepth,ctype;
  int channels;
  int width,height;
  png_bytep *row_pointers=NULL;
  int transforms=PNG_TRANSFORM_STRIP_16;
  png_colorp png_palette;
  int x,y,n,num_palette;
  png_bytep trans;
  int num_trans;
  int has_trans;

  f=kd_open(filename);
  if (!f) return -1;

  png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
  if (!png_ptr) {
    fprintf(stderr,"Cannot created PNG read structure\n");
    goto error;
  }

  info_ptr=png_create_info_struct(png_ptr);
  if (!info_ptr) {
    fprintf(stderr,"Cannot created PNG info structure\n");
    goto error;
  }

  if (setjmp(png_jmpbuf(png_ptr))) {
    fprintf(stderr,"PNG read failed\n");
    goto error;
  }

  png_init_io(png_ptr,f);
  png_set_sig_bytes(png_ptr,8);
  png_read_png(png_ptr,info_ptr,transforms,NULL);

  channels=png_get_channels(png_ptr,info_ptr);
  if (channels!=1) {
    fprintf(stderr,"Channels is not 1 (%d)",channels);
    goto error;
  }

  ctype=png_get_color_type(png_ptr,info_ptr);
  if (!(ctype&PNG_COLOR_MASK_PALETTE)) {
    fprintf(stderr,"Image is not paletted\n");
    goto error;
  }
  if (!(ctype&PNG_COLOR_MASK_COLOR)) {
    fprintf(stderr,"Image does not contain color information\n");
    goto error;
  }

  cdepth=png_get_bit_depth(png_ptr,info_ptr);
  if (cdepth>8) {
    fprintf(stderr,"Color depth is higher than 8\n");
    goto error;
  }

  width=png_get_image_width(png_ptr,info_ptr);
  height=png_get_image_height(png_ptr,info_ptr);

  if (!png_get_PLTE(png_ptr,info_ptr,&png_palette,&num_palette)) {
    fprintf(stderr,"Failed to retrieve palette data\n");
    goto error;
  }

  /* this may or may not be there */
  has_trans=(png_get_tRNS(png_ptr,info_ptr,&trans,&num_trans,NULL)&PNG_INFO_tRNS);

  row_pointers=png_get_rows(png_ptr,info_ptr);
  if (!row_pointers) {
    fprintf(stderr,"Failed to retrieve image data\n");
    goto error;
  }

  /* allocate memory */
  if (palette) {
    *palette=(kate_color*)kate_malloc(num_palette*sizeof(kate_color));
    if (!*palette) {
      fprintf(stderr,"Not enough memory to allocate space for palette (%d colors)\n",num_palette);
      goto error;
    }
  }
  if (pixels) {
    *pixels=(unsigned char*)kate_malloc(width*height);
    if (!*pixels) {
      fprintf(stderr,"Not enough memory to allocate space for pixels (%dx%d)\n",width,height);
      if (palette) kate_free(*palette);
      goto error;
    }
  }

  /* fill data */
  if (w) *w=width;
  if (h) *h=height;
  if (bpp) *bpp=cdepth;
  if (palette) {
    for (n=0;n<num_palette;++n) {
      (*palette)[n].r=png_palette[n].red;
      (*palette)[n].g=png_palette[n].green;
      (*palette)[n].b=png_palette[n].blue;
      (*palette)[n].a=(has_trans && n<num_trans)?trans[n]:255;
    }
  }
  if (ncolors) *ncolors=num_palette;
  if (pixels) {
    /* the PNG_TRANSFORM_PACKING and PNG_TRANSFORM_PACKSWAP bits don't seem to do anything to my 4 bit images,
       so do it by hand */
    int ppb=8/cdepth;
    int mask=(1<<cdepth)-1;
    n=0;
    for (y=0;y<height;++y) {
      unsigned char *row=row_pointers[y];
      for (x=0;x<width;++x) {
        (*pixels)[n++]=(row[x/ppb]>>((ppb-1-x%ppb)*cdepth))&mask;
      }
    }
  }

  png_destroy_read_struct(&png_ptr,&info_ptr,png_infopp_NULL);
  fclose(f);

  return 0;

error:
  if (png_ptr) png_destroy_read_struct(&png_ptr,info_ptr?&info_ptr:png_infopp_NULL,png_infopp_NULL);
  if (f) fclose(f);
  return -1;
}

int kd_read_png(const char *filename,int *w,int *h,unsigned char **pixels,size_t *size)
{
  FILE *f;
  png_structp png_ptr=NULL;
  png_infop info_ptr=NULL;
  long sz;

  f=kd_open(filename);
  if (!f) return -1;

  png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
  if (!png_ptr) {
    fprintf(stderr,"Cannot created PNG read structure\n");
    goto error;
  }

  info_ptr=png_create_info_struct(png_ptr);
  if (!info_ptr) {
    fprintf(stderr,"Cannot created PNG info structure\n");
    goto error;
  }

  if (setjmp(png_jmpbuf(png_ptr))) {
    fprintf(stderr,"PNG read failed\n");
    goto error;
  }

  png_init_io(png_ptr,f);
  png_set_sig_bytes(png_ptr,8);
  png_read_info(png_ptr,info_ptr);

  if (w) *w=png_get_image_width(png_ptr,info_ptr);
  if (h) *h=png_get_image_height(png_ptr,info_ptr);

  png_destroy_read_struct(&png_ptr,&info_ptr,png_infopp_NULL);

  /* now read the whole file as a binary blob */
  fseek(f,0,SEEK_END);
  sz=ftell(f);
  fseek(f,0,SEEK_SET);
  *pixels=malloc(sz);
  if (!*pixels) goto error;
  if ((long)fread(*pixels,1,sz,f)!=sz) {
    fprintf(stderr,"Failed to read %ld bytes\n",sz);
    free(*pixels);
    *pixels=NULL;
    goto error;
  }

  if (size) *size=sz;

  fclose(f);

  return 0;

error:
  if (png_ptr) png_destroy_read_struct(&png_ptr,info_ptr?&info_ptr:png_infopp_NULL,png_infopp_NULL);
  if (f) fclose(f);
  return -1;
}