root/as3/PNG/src/alchemy/fileformats/png/png.gg

リビジョン 3382, 6.0 kB (コミッタ: dsk, コミット時期: 2 年 前)

PNG ファーストコミット

Line 
1 {
2 #include <stdio.h>
3 #include "png.h"
4
5 #define GRAY_SIZE       1
6 #define GRAY_ALPHA_SIZE 2
7 #define RGB_SIZE        3
8 #define RGB_ALPHA_SIZE  4
9
10 static int readba(void *cookie, char *dst, int size)
11 {
12         return AS3_ByteArray_readBytes(dst, (AS3_Val)cookie, size);
13 }
14
15 static int writeba(void *cookie, const char *src, int size)
16 {
17         return AS3_ByteArray_writeBytes((AS3_Val)cookie, (char *)src, size);
18 }
19
20 static fpos_t seekba(void *cookie, fpos_t offs, int whence)
21 {
22         return AS3_ByteArray_seek((AS3_Val)cookie, offs, whence);
23 }
24
25 static int closeba(void *cookie)
26 {
27         AS3_Val zero = AS3_Int(0);
28         AS3_SetS((AS3_Val)cookie, "position", zero);
29         AS3_Release(zero);
30         return 0;
31 }
32 }
33
34 import flash.utils.ByteArray;
35
36 public function encodeSync(dest:ByteArray, source:ByteArray, width:int, height:int, colorType:uint,
37                            filterType:uint, compressionLevel:int):void
38 {
39         closeba(source);
40        
41         int sourceRowSize = RGB_ALPHA_SIZE * width;
42         int destRowSize;
43         switch (colorType)
44         {
45                 case PNG_COLOR_TYPE_GRAY:       destRowSize = GRAY_SIZE * width;       break;
46                 case PNG_COLOR_TYPE_GRAY_ALPHA: destRowSize = GRAY_ALPHA_SIZE * width; break;
47                 case PNG_COLOR_TYPE_RGB:        destRowSize = RGB_SIZE * width;        break;
48                 case PNG_COLOR_TYPE_RGB_ALPHA:  destRowSize = RGB_ALPHA_SIZE * width;  break;
49         }
50        
51         FILE *file = funopen((void *)dest, readba, writeba, seekba, closeba);
52         if (!file)
53         {
54         }
55         png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
56         if (!png)
57         {
58                 fclose(file);
59         }
60         png_infop info = png_create_info_struct(png);
61         if (!info)
62         {
63                 png_destroy_write_struct(&png, (png_infopp)NULL);
64                 fclose(file);
65         }
66         if (setjmp(png_jmpbuf(png)))
67         {
68                 png_destroy_write_struct(&png, (png_infopp)NULL);
69                 fclose(file);
70         }
71        
72         png_init_io(png, file);
73         png_set_filter(png, PNG_FILTER_TYPE_DEFAULT, filterType);
74         png_set_compression_level(png, compressionLevel);
75         png_set_compression_buffer_size(png, (1 + destRowSize) * height);
76        
77         png_set_IHDR(png, info, width, height, 8,
78                      colorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
79         png_write_info(png, info);
80        
81         int y, x;
82         unsigned char *sourceRow = (unsigned char *)malloc(sourceRowSize);
83         png_byte *destRow = (png_byte *)malloc(destRowSize);
84         switch (colorType)
85         {
86                 case PNG_COLOR_TYPE_RGB:
87                         for (y = 0; y < height; y ++)
88                         {
89                                 AS3_ByteArray_readBytes(sourceRow, source, sourceRowSize);
90                                 for (x = 0; x < width; x ++)
91                                 {
92                                         *(unsigned int *)(destRow+3*x) = *(unsigned int *)(sourceRow+1+4*x);
93                                 }
94                                 png_write_row(png, destRow);
95                         }
96                         break;
97                
98                 case PNG_COLOR_TYPE_RGB_ALPHA:
99                         for (y = 0; y < height; y ++)
100                         {
101                                 AS3_ByteArray_readBytes(sourceRow, source, sourceRowSize);
102                                 for (x = 0; x < width; x ++)
103                                 {
104                                         *(unsigned int *)(destRow+4*x) = *(unsigned int *)(sourceRow+1+4*x);
105                                         *(destRow+3+4*x) = *(png_byte *)(sourceRow+4*x);
106                                 }
107                                 png_write_row(png, destRow);
108                         }
109                         break;
110         }
111         free(sourceRow);
112         free(destRow);
113        
114         png_write_end(png, info);
115        
116         png_destroy_write_struct(&png, &info);
117         fclose(file);
118 }
119
120 async public function encodeAsync(open:Function, progress:Function, complete:Function, rowSegments:int,
121                                   dest:ByteArray, source:ByteArray, width:int, height:int,
122                                   colorType:uint, filterType:uint, compressionLevel:int):void
123 {
124         AS3_Call(open, NULL, NULL);
125        
126         closeba(source);
127        
128         int sourceRowSize = RGB_ALPHA_SIZE * width;
129         int destRowSize;
130         switch (colorType)
131         {
132                 case PNG_COLOR_TYPE_GRAY:       destRowSize = GRAY_SIZE * width;       break;
133                 case PNG_COLOR_TYPE_GRAY_ALPHA: destRowSize = GRAY_ALPHA_SIZE * width; break;
134                 case PNG_COLOR_TYPE_RGB:        destRowSize = RGB_SIZE * width;        break;
135                 case PNG_COLOR_TYPE_RGB_ALPHA:  destRowSize = RGB_ALPHA_SIZE * width;  break;
136         }
137        
138         FILE *file = funopen((void *)dest, readba, writeba, seekba, closeba);
139         if (!file)
140         {
141         }
142         png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
143         if (!png)
144         {
145                 fclose(file);
146         }
147         png_infop info = png_create_info_struct(png);
148         if (!info)
149         {
150                 png_destroy_write_struct(&png, (png_infopp)NULL);
151                 fclose(file);
152         }
153         if (setjmp(png_jmpbuf(png)))
154         {
155                 png_destroy_write_struct(&png, (png_infopp)NULL);
156                 fclose(file);
157         }
158        
159         png_init_io(png, file);
160         png_set_filter(png, PNG_FILTER_TYPE_DEFAULT, filterType);
161         png_set_compression_level(png, compressionLevel);
162         png_set_compression_buffer_size(png, (1 + destRowSize) * height);
163        
164         png_set_IHDR(png, info, width, height, 8,
165                      colorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
166         png_write_info(png, info);
167        
168         int y, x;
169         unsigned char *sourceRow = (unsigned char *)malloc(sourceRowSize);
170         png_byte *destRow = (png_byte *)malloc(destRowSize);
171         switch (colorType)
172         {
173                 case PNG_COLOR_TYPE_RGB:
174                         AS3_CallT(progress, NULL, "IntType, IntType", 0, destRowSize * height);
175                         for (y = 0; y < height; y ++)
176                         {
177                                 AS3_ByteArray_readBytes(sourceRow, source, sourceRowSize);
178                                 for (x = 0; x < width; x ++)
179                                 {
180                                         *(unsigned int *)(destRow+3*x) = *(unsigned int *)(sourceRow+1+4*x);
181                                 }
182                                 png_write_row(png, destRow);
183                                
184                                 if (y % rowSegments == 0 || y == height - 1)
185                                 {
186                                         AS3_CallT(progress, NULL, "IntType, IntType", destRowSize * (y + 1), destRowSize * height);
187                                         flyield();
188                                 }
189                         }
190                         break;
191                
192                 case PNG_COLOR_TYPE_RGB_ALPHA:
193                         AS3_CallT(progress, NULL, "IntType, IntType", 0, destRowSize * height);
194                         for (y = 0; y < height; y ++)
195                         {
196                                 AS3_ByteArray_readBytes(sourceRow, source, sourceRowSize);
197                                 for (x = 0; x < width; x ++)
198                                 {
199                                         *(unsigned int *)(destRow+4*x) = *(unsigned int *)(sourceRow+1+4*x);
200                                         *(destRow+3+4*x) = *(png_byte *)(sourceRow+4*x);
201                                 }
202                                 png_write_row(png, destRow);
203                                
204                                 if (y % rowSegments == 0 || y == height - 1)
205                                 {
206                                         AS3_CallT(progress, NULL, "IntType, IntType", destRowSize * (y + 1), destRowSize * height);
207                                         flyield();
208                                 }
209                         }
210                         break;
211         }
212         free(sourceRow);
213         free(destRow);
214        
215         png_write_end(png, info);
216        
217         png_destroy_write_struct(&png, &info);
218         fclose(file);
219        
220         AS3_Call(complete, NULL, NULL);
221 }
222
223
224
225
226
227
228
229
230
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。