1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - SSP - demos - jpegDecoder
3 File: main.c
4
5 Copyright 2007-2008 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Date:: 2008-11-20#$
14 $Rev: 9365 $
15 $Author: kitase_hirotake $
16 *---------------------------------------------------------------------------*/
17 #ifdef SDK_TWL
18 #include <twl.h>
19 #else
20 #include <nitro.h>
21 #endif
22
23 static u8* OutputBuffer;
24
25 static u8 WriteBuffer[923648] ATTRIBUTE_ALIGN(4);
26 static u32 WriteBufferSize;
27
28 #include <twl/ssp/ARM9/jpegdec.h>
29 #include <twl/ssp/ARM9/exifdec.h>
30
31 #include "DEMO.h"
32
33 /*---------------------------------------------------------------------------*
34 Name: MyAlloc
35
36 Description: OS_Alloc wrapper function.
37 *---------------------------------------------------------------------------*/
MyAlloc(u32 size)38 static void* MyAlloc(u32 size)
39 {
40 void* ptr;
41
42 ptr = OS_Alloc(size);
43
44 return ptr;
45 }
46
47 /*---------------------------------------------------------------------------*
48 Name: MyFree
49
50 Description: OS_Free wrapper function.
51 *---------------------------------------------------------------------------*/
MyFree(void * ptr)52 static void MyFree(void* ptr)
53 {
54 OS_Free(ptr);
55 }
56
57 /*---------------------------------------------------------------------------*
58 Name: InitializeAllocateSystem
59
60 Description: Initializes the memory allocation system within the main memory arena.
61
62 Arguments: None.
63
64 Returns: None.
65 *---------------------------------------------------------------------------*/
InitializeAllocateSystem(void)66 static void InitializeAllocateSystem(void)
67 {
68 void *tempLo;
69 OSHeapHandle hh;
70
71 tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
72 OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
73 hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
74 if (hh < 0)
75 {
76 OS_Panic("ARM9: Fail to create heap...\n");
77 }
78 hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
79 }
80
81
82 /*---------------------------------------------------------------------------*/
83 /* Functions */
84
85 /*---------------------------------------------------------------------------*
86 Name: VBlankIntr
87
88 Description: VBlankIntr
89
90 Arguments: None.
91
92 Returns: None.
93 *---------------------------------------------------------------------------*/
VBlankIntr(void)94 static void VBlankIntr(void)
95 {
96 OS_SetIrqCheckFlag(OS_IE_V_BLANK);
97 }
98
99 /*---------------------------------------------------------------------------*
100 Name: TwlMain / NitroMain
101
102 Description: Main.
103
104 Arguments: None.
105
106 Returns: None.
107 *---------------------------------------------------------------------------*/
108 #ifdef SDK_TWL
TwlMain(void)109 void TwlMain(void)
110 #else
111 void NitroMain(void)
112 #endif
113 {
114 /* OS initialization */
115 OS_Init();
116 OS_InitTick();
117 OS_InitAlarm();
118
119 OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
120 (void)OS_EnableIrqMask(OS_IE_V_BLANK);
121
122 (void)OS_EnableIrq();
123 (void)GX_VBlankIntr(TRUE);
124
125 (void)OS_EnableInterrupts();
126
127 DEMOInitCommon();
128 DEMOInitVRAM();
129 DEMOInitDisplayBitmap();
130 DEMOHookConsole();
131 DEMOStartDisplay();
132
133 // SD initialization
134 FS_Init( FS_DMA_NOT_USE );
135
136 InitializeAllocateSystem();
137
138 OS_TPrintf("\n");
139 OS_TPrintf("===================================\n");
140 OS_TPrintf(" SSP JpegDecoder Test\n");
141 OS_TPrintf("===================================\n");
142
143 {
144 s16 decode_file_width, decode_file_height;
145
146 u8 *data;
147 u32 length;
148 FSFile file[1];
149
150
151 FS_InitFile(file);
152 if( !FS_OpenFileEx(file, "rom:/decode.jpg", FS_FILEMODE_R) )
153 {
154 OS_Panic("failed FS_OpenFileEx");
155 }
156 length = FS_GetFileLength(file);
157 data = MyAlloc(length);
158 if(data == 0)
159 {
160 OS_Panic("Cannot allocate memory ... data!");
161 }
162 if( FS_ReadFile(file, data, (s32)length) == -1)
163 {
164 OS_Panic("failed FS_ReadFile");
165 }
166
167 {
168 OSTick jpegDecode_begin,jpegDecode_end;
169 BOOL result;
170
171 OS_TPrintf("Start Decode ... length = %d\n", length);
172
173 jpegDecode_begin = OS_GetTick();
174
175 decode_file_width = 256;
176 decode_file_height = 192;
177 OutputBuffer = MyAlloc(decode_file_width * decode_file_height * sizeof(u16));
178
179 result = SSP_StartJpegDecoder(data, length, OutputBuffer, &decode_file_width, &decode_file_height, SSP_JPEG_RGB555);
180
181 jpegDecode_end = OS_GetTick();
182
183 if(result == 0)
184 OS_Panic("Failed Cpu Decode\n");
185
186 OS_TPrintf("[TIMER] MAIN SSP_StartJpegDecoder time[usec] = %d\n", OS_TicksToMicroSeconds(jpegDecode_end - jpegDecode_begin));
187 OS_TPrintf("[DEBUG] MAIN decode_file_width = %d : decode_file_height = %d\n", decode_file_width, decode_file_height);
188
189 {
190 int i,j;
191 float i_step,j_step;
192 int i_count,j_count;
193 s16 t_width = decode_file_width;
194
195 j_step = (float)decode_file_width / 256;
196 i_step = (float)decode_file_height / 192;
197
198 i_count = j_count = 0;
199 for(i = 0; i < 192; i++)
200 {
201 for(j = 0; j < 256; j++)
202 {
203 *((u16*)((int)G2_GetBG3ScrPtr() + i*256*2 + j*2)) = *((u16*)((u32)OutputBuffer + (u32)(i_count * decode_file_width*2) + (u32)(j_count*2)));
204 j_count = (int)((double)j*j_step);
205 }
206 i_count=(int)((double)i*i_step);
207 }
208 }
209
210 // Try to display the date and time
211 {
212 u8 dateTime[20];
213
214 (void)SSP_GetJpegDecoderDateTime(dateTime);
215 OS_TPrintf("DateTimeOriginal = %s\n", dateTime);
216 }
217 {
218 char software[30];
219 u32 initialCode;
220
221 (void)SSP_GetJpegDecoderSoftware(software);
222 // The Exif software tag contains a little-endian game code
223 MI_CpuCopy8(software, &initialCode, 4);
224 OS_TPrintf("InitialCode = %c%c%c%c\n", initialCode>>24, initialCode>>16, initialCode>>8, initialCode);
225 }
226 // Get the MakerNote address and size
227 {
228 u8 *makerNoteAddr = SSP_GetJpegDecoderMakerNoteAddrEx(SSP_MAKERNOTE_PHOTO);
229 u16 makerNoteSize = SSP_GetJpegDecoderMakerNoteSizeEx(SSP_MAKERNOTE_PHOTO);
230 u8 *makerNoteUserAddr = SSP_GetJpegDecoderMakerNoteAddrEx(SSP_MAKERNOTE_USER);
231 u16 makerNoteUserSize = SSP_GetJpegDecoderMakerNoteSizeEx(SSP_MAKERNOTE_USER);
232
233 OS_TPrintf("MakerNoteAddr = 0x%X\n", makerNoteAddr);
234 OS_TPrintf("MakerNoteSize = 0x%X\n", makerNoteSize);
235 OS_TPrintf("MakerNoteUserAddr = 0x%X\n", makerNoteUserAddr);
236 OS_TPrintf("MakerNoteUserSize = 0x%X\n", makerNoteUserSize);
237 }
238 MyFree(OutputBuffer);
239
240 jpegDecode_begin = OS_GetTick();
241
242 decode_file_width = SSP_JPEG_THUMBNAIL_WIDTH;
243 decode_file_height = SSP_JPEG_THUMBNAIL_HEIGHT;
244 OutputBuffer = MyAlloc(decode_file_width * decode_file_height * sizeof(u16));
245
246 result = SSP_StartJpegDecoder(data, length, OutputBuffer, &decode_file_width, &decode_file_height, SSP_JPEG_THUMBNAIL | SSP_JPEG_RGB555);
247
248 jpegDecode_end = OS_GetTick();
249
250 if(result == 0)
251 OS_Panic("Failed Cpu Decode\n");
252
253 OS_TPrintf("[TIMER] Thumbnail SSP_StartJpegDecoder time[usec] = %d\n", OS_TicksToMicroSeconds(jpegDecode_end - jpegDecode_begin));
254 OS_TPrintf("[DEBUG] Thumbnail decode_file_width = %d : decode_file_height = %d\n", decode_file_width, decode_file_height);
255
256
257 // Try to display the date and time
258 {
259 u8 dateTime[20];
260
261 (void)SSP_GetJpegDecoderDateTime(dateTime);
262 OS_TPrintf("DateTimeOriginal = %s\n", dateTime);
263 }
264 {
265 char software[30];
266 u32 initialCode;
267
268 (void)SSP_GetJpegDecoderSoftware(software);
269 // The Exif software tag contains a little-endian game code
270 MI_CpuCopy8(software, &initialCode, 4);
271 OS_TPrintf("InitialCode = %c%c%c%c\n", initialCode>>24, initialCode>>16, initialCode>>8, initialCode);
272 }
273 // Get the MakerNote address and size
274 {
275 u8 *makerNoteAddr = SSP_GetJpegDecoderMakerNoteAddrEx(SSP_MAKERNOTE_PHOTO);
276 u16 makerNoteSize = SSP_GetJpegDecoderMakerNoteSizeEx(SSP_MAKERNOTE_PHOTO);
277 u8 *makerNoteUserAddr = SSP_GetJpegDecoderMakerNoteAddrEx(SSP_MAKERNOTE_USER);
278 u16 makerNoteUserSize = SSP_GetJpegDecoderMakerNoteSizeEx(SSP_MAKERNOTE_USER);
279
280 OS_TPrintf("MakerNoteAddr = 0x%X\n", makerNoteAddr);
281 OS_TPrintf("MakerNoteSize = 0x%X\n", makerNoteSize);
282 OS_TPrintf("MakerNoteUserAddr = 0x%X\n", makerNoteUserAddr);
283 OS_TPrintf("MakerNoteUserSize = 0x%X\n", makerNoteUserSize);
284 }
285 MyFree(OutputBuffer);
286 }
287 MyFree(data);
288 }
289 OS_Terminate();
290 }
291
292