-
Notifications
You must be signed in to change notification settings - Fork 3
/
png_dec.c
252 lines (200 loc) · 6.9 KB
/
png_dec.c
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
#include "png_dec.h"
#include "mem.h"
#include "inc/vsh_exports.h"
#ifdef DEBUG
#include "network.h"
#endif
//#include "misc.h" // for buzzer() only
static uint32_t png_w = 0, png_h = 0;
static int32_t create_decoder(png_dec_info *dec_ctx);
static int32_t open_png(png_dec_info *dec_ctx, const char *file_path);
static int32_t set_dec_param(png_dec_info *dec_ctx);
static int32_t decode_png_stream(png_dec_info *dec_ctx, void *buf);
static void* cb_malloc(uint32_t size, void *cb_malloc_arg);
static int32_t cb_free(void *ptr, void *cb_free_arg);
/***********************************************************************
* create png decoder
***********************************************************************/
static int32_t create_decoder(png_dec_info *dec_ctx)
{
int32_t ret = 0;
CellPngDecThreadInParam in;
CellPngDecThreadOutParam out;
// set params
dec_ctx->cb_arg.mallocCallCounts = 0;
dec_ctx->cb_arg.freeCallCounts = 0;
in.spuThreadEnable = CELL_PNGDEC_SPU_THREAD_DISABLE; // ppu only
in.ppuThreadPriority = 512;
in.spuThreadPriority = 200;
in.cbCtrlMallocFunc = cb_malloc;
in.cbCtrlMallocArg = &dec_ctx->cb_arg;
in.cbCtrlFreeFunc = cb_free;
in.cbCtrlFreeArg = &dec_ctx->cb_arg;
// create png decoder
ret = PngDecCreate(&dec_ctx->main_h, &in, &out);
#ifdef DEBUG
dbg_printf("PngDecCreate\tret %d\n", ret);
#endif
return ret;
}
/***********************************************************************
* open png stream
***********************************************************************/
static int32_t open_png(png_dec_info *dec_ctx, const char *file_path)
{
int32_t ret = 0;
CellPngDecSrc src;
CellPngDecOpnInfo info;
// set stream source
src.srcSelect = CELL_PNGDEC_FILE; // source is file
src.fileName = file_path; // path to source file
src.fileOffset = 0;
src.fileSize = 0;
src.streamPtr = NULL;
src.streamSize = 0;
// spu thread disable
src.spuThreadEnable = CELL_PNGDEC_SPU_THREAD_DISABLE;
// open stream
ret = PngDecOpen(dec_ctx->main_h, &dec_ctx->sub_h, &src, &info);
#ifdef DEBUG
dbg_printf("PngDecOpen %s \tret %d\n", file_path, ret);
#endif
return ret;
}
/***********************************************************************
* set decode parameter
***********************************************************************/
static int32_t set_dec_param(png_dec_info *dec_ctx)
{
int32_t ret = 0;
CellPngDecInfo info;
CellPngDecInParam in;
CellPngDecOutParam out;
// read png header
PngDecReadHeader(dec_ctx->main_h, dec_ctx->sub_h, &info);
png_w = info.imageWidth;
png_h = info.imageHeight;
#ifdef DEBUG
dbg_printf("\nimageWidth: %d\n"
"imageHeight: %d\n"
"numComponents: 0x%08X\n"
"colorSpace: 0x%08X\n"
"bitDepth: 0x%08X\n"
"interlaceMethod: 0x%08X\n"
"chunkInformation: 0x%08X\n\n",
info.imageWidth, info.imageHeight, info.numComponents,
info.colorSpace, info.bitDepth, info.interlaceMethod, info.chunkInformation);
#endif
// set decoder parameter
in.commandPtr = NULL;
in.outputMode = CELL_PNGDEC_TOP_TO_BOTTOM;
in.outputColorSpace = CELL_PNGDEC_ARGB; // ps3 framebuffer is ARGB
in.outputBitDepth = 8;
in.outputPackFlag = CELL_PNGDEC_1BYTE_PER_1PIXEL;
if((info.colorSpace == CELL_PNGDEC_GRAYSCALE_ALPHA) ||
(info.colorSpace == CELL_PNGDEC_RGBA) ||
(info.chunkInformation & 0x10))
{
in.outputAlphaSelect = CELL_PNGDEC_STREAM_ALPHA;
}
else
{
in.outputAlphaSelect = CELL_PNGDEC_FIX_ALPHA;
}
in.outputColorAlpha = 0xFF;
ret = PngDecSetParameter(dec_ctx->main_h, dec_ctx->sub_h, &in, &out);
#ifdef DEBUG
dbg_printf("PngDecSetParameter\tret %d\n", ret);
#endif
return ret;
}
/***********************************************************************
* decode png stream
***********************************************************************/
static int32_t decode_png_stream(png_dec_info *dec_ctx, void *buf)
{
int32_t ret = 0;
uint8_t *out;
CellPngDecDataCtrlParam param;
CellPngDecDataOutInfo info;
param.outputBytesPerLine = png_w * 4;
out = (void*)buf;
memset(out, 0, (png_w * png_h * 4));
// decode png...
ret = PngDecDecodeData(dec_ctx->main_h, dec_ctx->sub_h, out, ¶m, &info);
#ifdef DEBUG
dbg_printf("PngDecDecodeData\tret %d\n", ret);
#endif
return ret;
}
/***********************************************************************
* malloc callback
***********************************************************************/
static void *cb_malloc(uint32_t size, void *cb_malloc_arg)
{
Cb_Arg *arg;
arg = (Cb_Arg *)cb_malloc_arg;
arg->mallocCallCounts++;
void *ret = malloc(size);
#ifdef DEBUG
dbg_printf("arg->mallocCallCounts\t%.2d, size %d,\tret %p\n", arg->mallocCallCounts, size, ret);
#endif
return ret;
}
/***********************************************************************
* free callback
***********************************************************************/
static int32_t cb_free(void *ptr, void *cb_free_arg)
{
Cb_Arg *arg;
arg = (Cb_Arg *)cb_free_arg;
arg->freeCallCounts++;
#ifdef DEBUG
dbg_printf("arg->freeCallCounts\t%.2d, ptr %p\n", arg->freeCallCounts, ptr);
#endif
if(ptr) free(ptr);
return 0;
}
/***********************************************************************
* decode png file
* const char *file_path = path to png file e.g. "/dev_hdd0/test.png"
***********************************************************************/
Buffer load_png(const char *file_path)
{
png_dec_info dec_ctx; // decryption handles
void *buf_addr = NULL; // buffer for decoded png data
int32_t ret = 0;
Buffer tmp;
memset(&tmp, 0, sizeof(Buffer));
// create png decoder
ret = create_decoder(&dec_ctx);
// open png stream
open_png(&dec_ctx, file_path);
// set decode parameter
set_dec_param(&dec_ctx);
// alloc target buffer
buf_addr = mem_alloc(png_w * png_h * 4);
#ifdef DEBUG
dbg_printf("buf_addr %p\n", buf_addr);
#endif
if(buf_addr)
{
// decode png stream, into target buffer
decode_png_stream(&dec_ctx, buf_addr); // check return !
// store png values
tmp.addr = (uint32_t*)buf_addr;
tmp.w = png_w;
tmp.h = png_h;
}
// close png stream
ret = PngDecClose(dec_ctx.main_h, dec_ctx.sub_h);
#ifdef DEBUG
dbg_printf("PngDecClose\tret %d\n", ret);
#endif
// destroy png decoder
ret = PngDecDestroy(dec_ctx.main_h);
#ifdef DEBUG
dbg_printf("PngDecDestroy\tret %d\n", ret);
#endif
return tmp;
}