1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <png.h> |
---|
5 | #include "proto.h" |
---|
6 | |
---|
7 | #ifdef __64BIT__ |
---|
8 | typedef int g2int; |
---|
9 | #else |
---|
10 | typedef long g2int; |
---|
11 | #endif |
---|
12 | |
---|
13 | struct png_stream { |
---|
14 | unsigned char *stream_ptr; /* location to write PNG stream */ |
---|
15 | g2int stream_len; /* number of bytes written */ |
---|
16 | }; |
---|
17 | typedef struct png_stream png_stream; |
---|
18 | |
---|
19 | void user_write_data(png_structp ,png_bytep , png_uint_32 ); |
---|
20 | void user_flush_data(png_structp ); |
---|
21 | |
---|
22 | void user_write_data(png_structp png_ptr,png_bytep data, png_uint_32 length) |
---|
23 | /* |
---|
24 | Custom write function used to that libpng will write |
---|
25 | to memory location instead of a file on disk |
---|
26 | */ |
---|
27 | { |
---|
28 | unsigned char *ptr; |
---|
29 | g2int offset; |
---|
30 | png_stream *mem; |
---|
31 | |
---|
32 | mem=(png_stream *)png_get_io_ptr(png_ptr); |
---|
33 | ptr=mem->stream_ptr; |
---|
34 | offset=mem->stream_len; |
---|
35 | /* printf("SAGwr %ld %ld %x\n",offset,length,ptr); */ |
---|
36 | /*for (j=offset,k=0;k<length;j++,k++) ptr[j]=data[k];*/ |
---|
37 | memcpy(ptr+offset,data,length); |
---|
38 | mem->stream_len += length; |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | void user_flush_data(png_structp png_ptr) |
---|
43 | /* |
---|
44 | Dummy Custom flush function |
---|
45 | */ |
---|
46 | { |
---|
47 | int *do_nothing=NULL; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | int ENC_PNG(char *data,g2int *width,g2int *height,g2int *nbits,char *pngbuf) |
---|
52 | { |
---|
53 | |
---|
54 | int color_type; |
---|
55 | g2int j,bytes,pnglen,bit_depth; |
---|
56 | png_structp png_ptr; |
---|
57 | png_infop info_ptr; |
---|
58 | // png_bytep *row_pointers[*height]; |
---|
59 | png_bytep **row_pointers; |
---|
60 | png_stream write_io_ptr; |
---|
61 | |
---|
62 | /* create and initialize png_structs */ |
---|
63 | |
---|
64 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, |
---|
65 | NULL, NULL); |
---|
66 | if (!png_ptr) |
---|
67 | return (-1); |
---|
68 | |
---|
69 | info_ptr = png_create_info_struct(png_ptr); |
---|
70 | if (!info_ptr) |
---|
71 | { |
---|
72 | png_destroy_write_struct(&png_ptr,(png_infopp)NULL); |
---|
73 | return (-2); |
---|
74 | } |
---|
75 | |
---|
76 | /* Set Error callback */ |
---|
77 | |
---|
78 | if (setjmp(png_jmpbuf(png_ptr))) |
---|
79 | { |
---|
80 | png_destroy_write_struct(&png_ptr, &info_ptr); |
---|
81 | return (-3); |
---|
82 | } |
---|
83 | |
---|
84 | /* Initialize info for writing PNG stream to memory */ |
---|
85 | |
---|
86 | write_io_ptr.stream_ptr=(png_voidp)pngbuf; |
---|
87 | write_io_ptr.stream_len=0; |
---|
88 | |
---|
89 | /* Set new custom write functions */ |
---|
90 | |
---|
91 | png_set_write_fn(png_ptr,(voidp)&write_io_ptr,(png_rw_ptr)user_write_data, |
---|
92 | (png_flush_ptr)user_flush_data); |
---|
93 | /* png_init_io(png_ptr, fptr); */ |
---|
94 | /* png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); */ |
---|
95 | |
---|
96 | /* Set the image size, colortype, filter type, etc... */ |
---|
97 | |
---|
98 | /* printf("SAGTsettingIHDR %d %d %d\n",*width,*height,bit_depth); */ |
---|
99 | bit_depth=*nbits; |
---|
100 | color_type=PNG_COLOR_TYPE_GRAY; |
---|
101 | if (*nbits == 24 ) { |
---|
102 | bit_depth=8; |
---|
103 | color_type=PNG_COLOR_TYPE_RGB; |
---|
104 | } |
---|
105 | else if (*nbits == 32 ) { |
---|
106 | bit_depth=8; |
---|
107 | color_type=PNG_COLOR_TYPE_RGB_ALPHA; |
---|
108 | } |
---|
109 | png_set_IHDR(png_ptr, info_ptr, *width, *height, |
---|
110 | bit_depth, color_type, PNG_INTERLACE_NONE, |
---|
111 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
---|
112 | |
---|
113 | /* Put image data into the PNG info structure */ |
---|
114 | |
---|
115 | /*bytes=bit_depth/8;*/ |
---|
116 | bytes=*nbits/8; |
---|
117 | row_pointers=malloc((*height)*sizeof(png_bytep)); |
---|
118 | for (j=0;j<*height;j++) row_pointers[j]=(png_bytep *)(data+(j*(*width)*bytes)); |
---|
119 | png_set_rows(png_ptr, info_ptr, (png_bytepp)row_pointers); |
---|
120 | |
---|
121 | /* Do the PNG encoding, and write out PNG stream */ |
---|
122 | |
---|
123 | png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); |
---|
124 | |
---|
125 | /* Clean up */ |
---|
126 | |
---|
127 | png_destroy_write_struct(&png_ptr, &info_ptr); |
---|
128 | free(row_pointers); |
---|
129 | pnglen=write_io_ptr.stream_len; |
---|
130 | return pnglen; |
---|
131 | |
---|
132 | } |
---|
133 | |
---|