1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #if defined __CELLOS_LV2__ |
---|
16 | |
---|
17 | #include <cmath> |
---|
18 | |
---|
19 | #include <cell/sysmodule.h> |
---|
20 | #include <cell/codec/pngdec.h> |
---|
21 | |
---|
22 | #include "core.h" |
---|
23 | #include "../../image/image-private.h" |
---|
24 | |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | namespace lol |
---|
28 | { |
---|
29 | |
---|
30 | /* |
---|
31 | * Image implementation class |
---|
32 | */ |
---|
33 | |
---|
34 | DECLARE_IMAGE_LOADER(Ps3ImageData, 100) |
---|
35 | { |
---|
36 | public: |
---|
37 | virtual bool Open(char const *); |
---|
38 | virtual bool Close(); |
---|
39 | |
---|
40 | virtual void *GetData() const; |
---|
41 | |
---|
42 | private: |
---|
43 | static void* Malloc(uint32_t size, void* data) { return malloc(size); }; |
---|
44 | static int32_t Free(void* ptr, void* data) { free(ptr); return 0; }; |
---|
45 | uint8_t *pixels; |
---|
46 | }; |
---|
47 | |
---|
48 | /* |
---|
49 | * Public Image class |
---|
50 | */ |
---|
51 | |
---|
52 | bool Ps3ImageData::Open(char const *path) |
---|
53 | { |
---|
54 | int32_t err; |
---|
55 | |
---|
56 | /* Initialise decoding library */ |
---|
57 | CellPngDecMainHandle hmain; |
---|
58 | |
---|
59 | err = cellSysmoduleLoadModule(CELL_SYSMODULE_FS); |
---|
60 | if (err != CELL_OK) |
---|
61 | { |
---|
62 | #if !LOL_RELEASE |
---|
63 | Log::Error("could not open Fs sysmodule\n"); |
---|
64 | #endif |
---|
65 | return false; |
---|
66 | } |
---|
67 | |
---|
68 | err = cellSysmoduleLoadModule(CELL_SYSMODULE_PNGDEC); |
---|
69 | if (err != CELL_OK) |
---|
70 | { |
---|
71 | #if !LOL_RELEASE |
---|
72 | Log::Error("could not open PngDec sysmodule\n"); |
---|
73 | #endif |
---|
74 | return false; |
---|
75 | } |
---|
76 | |
---|
77 | CellPngDecThreadInParam in_param; |
---|
78 | in_param.spuThreadEnable = CELL_PNGDEC_SPU_THREAD_ENABLE; |
---|
79 | in_param.ppuThreadPriority = 1000; |
---|
80 | in_param.spuThreadPriority = 200; |
---|
81 | in_param.cbCtrlMallocFunc = ImageData::Malloc; |
---|
82 | in_param.cbCtrlMallocArg = NULL; |
---|
83 | in_param.cbCtrlFreeFunc = ImageData::Free; |
---|
84 | in_param.cbCtrlFreeArg = NULL; |
---|
85 | CellPngDecThreadOutParam out_param; |
---|
86 | err = cellPngDecCreate(&hmain, &in_param, &out_param); |
---|
87 | if (err != CELL_OK) |
---|
88 | { |
---|
89 | #if !LOL_RELEASE |
---|
90 | Log::Error("could not create PngDec library\n"); |
---|
91 | #endif |
---|
92 | return false; |
---|
93 | } |
---|
94 | |
---|
95 | /* Create decoder */ |
---|
96 | CellPngDecSubHandle hsub; |
---|
97 | |
---|
98 | char file[1024]; |
---|
99 | sprintf(file, "/app_home/c:/Users/s.hocevar/lolengine/%s", path); |
---|
100 | |
---|
101 | CellPngDecSrc dec_src; |
---|
102 | dec_src.srcSelect = CELL_PNGDEC_FILE; |
---|
103 | dec_src.fileName = file; |
---|
104 | dec_src.fileOffset = 0; |
---|
105 | dec_src.fileSize = 0; |
---|
106 | dec_src.streamPtr = NULL; |
---|
107 | dec_src.streamSize = 0; |
---|
108 | dec_src.spuThreadEnable = CELL_PNGDEC_SPU_THREAD_ENABLE; |
---|
109 | CellPngDecOpnInfo open_info; |
---|
110 | err = cellPngDecOpen(hmain, &hsub, &dec_src, &open_info); |
---|
111 | if (err != CELL_OK) |
---|
112 | { |
---|
113 | #if !LOL_RELEASE |
---|
114 | Log::Error("could not open %s for decoding\n", file); |
---|
115 | #endif |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | CellPngDecInfo info; |
---|
120 | err = cellPngDecReadHeader(hmain, hsub, &info); |
---|
121 | if (err != CELL_OK) |
---|
122 | { |
---|
123 | #if !LOL_RELEASE |
---|
124 | Log::Error("could not read image header\n"); |
---|
125 | #endif |
---|
126 | return false; |
---|
127 | } |
---|
128 | |
---|
129 | CellPngDecInParam in_dec_param; |
---|
130 | in_dec_param.commandPtr = NULL; |
---|
131 | in_dec_param.outputMode = CELL_PNGDEC_TOP_TO_BOTTOM; |
---|
132 | in_dec_param.outputColorSpace = CELL_PNGDEC_RGBA; |
---|
133 | in_dec_param.outputBitDepth = 8; |
---|
134 | in_dec_param.outputPackFlag = CELL_PNGDEC_1BYTE_PER_1PIXEL; |
---|
135 | in_dec_param.outputAlphaSelect = CELL_PNGDEC_STREAM_ALPHA; |
---|
136 | in_dec_param.outputColorAlpha = 0xff; |
---|
137 | CellPngDecOutParam out_dec_param; |
---|
138 | err = cellPngDecSetParameter(hmain, hsub, &in_dec_param, &out_dec_param); |
---|
139 | if (err != CELL_OK) |
---|
140 | { |
---|
141 | #if !LOL_RELEASE |
---|
142 | Log::Error("could not configure PngDec decoder\n"); |
---|
143 | #endif |
---|
144 | return false; |
---|
145 | } |
---|
146 | |
---|
147 | /* Decode image */ |
---|
148 | size = ivec2(info.imageWidth, info.imageHeight); |
---|
149 | format = FORMAT_RGBA; |
---|
150 | pixels = (uint8_t *)malloc(info.imageWidth * 4 * info.imageHeight); |
---|
151 | CellPngDecDataCtrlParam data_ctrl_param; |
---|
152 | data_ctrl_param.outputBytesPerLine = info.imageWidth * 4; |
---|
153 | CellPngDecDataOutInfo data_out_info; |
---|
154 | err = cellPngDecDecodeData(hmain, hsub, pixels, |
---|
155 | &data_ctrl_param, &data_out_info); |
---|
156 | if (err != CELL_OK) |
---|
157 | { |
---|
158 | #if !LOL_RELEASE |
---|
159 | Log::Error("could not run PngDec decoder\n"); |
---|
160 | #endif |
---|
161 | return false; |
---|
162 | } |
---|
163 | |
---|
164 | /* Close decoder */ |
---|
165 | err = cellPngDecClose(hmain, hsub); |
---|
166 | if (err != CELL_OK) |
---|
167 | { |
---|
168 | #if !LOL_RELEASE |
---|
169 | Log::Error("could not close PngDec decoder\n"); |
---|
170 | #endif |
---|
171 | return false; |
---|
172 | } |
---|
173 | |
---|
174 | /* Deinitialise library */ |
---|
175 | err = cellPngDecDestroy(hmain); |
---|
176 | if (err != CELL_OK) |
---|
177 | { |
---|
178 | #if !LOL_RELEASE |
---|
179 | Log::Error("could not destroy PngDec decoder\n"); |
---|
180 | #endif |
---|
181 | return false; |
---|
182 | } |
---|
183 | err = cellSysmoduleUnloadModule(CELL_SYSMODULE_PNGDEC); |
---|
184 | err = cellSysmoduleUnloadModule(CELL_SYSMODULE_FS); |
---|
185 | |
---|
186 | return true; |
---|
187 | } |
---|
188 | |
---|
189 | bool Ps3ImageData::Close() |
---|
190 | { |
---|
191 | free(pixels); |
---|
192 | |
---|
193 | return true; |
---|
194 | } |
---|
195 | |
---|
196 | void * Ps3ImageData::GetData() const |
---|
197 | { |
---|
198 | return pixels; |
---|
199 | } |
---|
200 | |
---|
201 | } /* namespace lol */ |
---|
202 | |
---|
203 | #endif /* defined __CELLOS_LV2__ */ |
---|
204 | |
---|