source: trunk/src/image/codec/ps3-image.cpp @ 1513

Last change on this file since 1513 was 1513, checked in by sam, 7 years ago

core: replace usage of sin() or std::sin() with lol::sin() where appropriate.

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