1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - SSP - include
3   File:     jpegdec.h
4 
5   Copyright 2007-2009 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:: 2009-07-06#$
14   $Rev: 10865 $
15   $Author: kitase_hirotake $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef TWL_SSP_JPEGDEC_H_
19 #define TWL_SSP_JPEGDEC_H_
20 
21 #include <twl/ssp/common/ssp_jpeg_type.h>
22 #include <twl/ssp/ARM9/exifdec.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include <twl/types.h>
29 
30 
31 // Define errorCode with a range of s8.
32 // The cause of "Could not process data." is either that the data format is invalid or unsupported. It is also possible that the data is incomplete.
33 //
34 typedef enum SSPJpegDecoderErrorCode
35 {
36     SSP_JPEG_DECODER_OK = 0,       // Success
37     SSP_JPEG_DECODER_ERROR_ARGUMENT = (-1),   // There is a mistake in an argument other than the 'option' argument
38     SSP_JPEG_DECODER_ERROR_WORK_ALIGN = (-2),   // pCtx is not in 4-byte alignment
39     SSP_JPEG_DECODER_ERROR_OPTION = (-3),   // There is a mistake in the 'option' argument
40     SSP_JPEG_DECODER_ERROR_SIGN = (-10),
41 
42     SSP_JPEG_DECODER_ERROR_WIDTH_HEIGHT = (-20),  // Either the width or the height exceeds the specified maximum value. The actual size is in pCtx->width and pCtx->height
43 
44     SSP_JPEG_DECODER_ERROR_EXIF_0 = (-30),   // Could not process data
45 // If decoding of a thumbnail is specified for an image for which no thumbnail exists, this error code might be returned
46 //
47 
48     SSP_JPEG_DECODER_ERROR_MARKER_COMBINATION = (-50),   // Could not process data
49 // Marker combination is incorrect.
50 // For example, this error code might be returned if the SOS marker corresponding to the SOF marker was not found.
51 //
52 
53     SSP_JPEG_DECODER_ERROR_SOI = (-60),   // Could not process data (or marker)
54     SSP_JPEG_DECODER_ERROR_SOF = (-61),
55     SSP_JPEG_DECODER_ERROR_SOF_BLOCK_ID = (-62),
56     SSP_JPEG_DECODER_ERROR_DHT = (-63),
57     SSP_JPEG_DECODER_ERROR_SOS = (-64),
58     SSP_JPEG_DECODER_ERROR_DQT = (-65),
59     SSP_JPEG_DECODER_ERROR_DRI = (-66),
60 
61     SSP_JPEG_DECODER_ERROR_UNDERRUN_0 = (-90),   // Could not process data
62     SSP_JPEG_DECODER_ERROR_UNDERRUN_1 = (-91),
63     SSP_JPEG_DECODER_ERROR_UNDERRUN_2 = (-92),
64     SSP_JPEG_DECODER_ERROR_UNDERRUN_3 = (-93),
65     SSP_JPEG_DECODER_ERROR_UNDERRUN_4 = (-94),
66     SSP_JPEG_DECODER_ERROR_UNDERRUN_5 = (-95),
67 
68     SSP_JPEG_DECODER_ERROR_RANGE_0 = (-110),  // Could not process data
69     SSP_JPEG_DECODER_ERROR_RANGE_1 = (-111),
70     SSP_JPEG_DECODER_ERROR_RANGE_2 = (-112),
71     SSP_JPEG_DECODER_ERROR_RANGE_3 = (-113),
72     SSP_JPEG_DECODER_ERROR_RANGE_4 = (-114),
73     SSP_JPEG_DECODER_ERROR_RANGE_5 = (-115),
74 
75     SSP_JPEG_DECODER_ERROR_HLB_0 = (-120),  // Could not process data
76 
77     SSP_JPEG_DECODER_ERROR_INTERNAL_CTX_SIZE = (-128)  // Internal error. Normally, this does not occur
78 } SSPJpegDecoderErrorCode;
79 
80 // For Fast versions, work memory to call a context structure is required.
81 // Initialization before and after calling the function is unnecessary. After the function completes, the library will not use it again.
82 // The context structure is large (8 KB), so do not allocate it from a stack in the same way as the local variables.
83 //
84 
85 // Context structure
86 typedef struct SSPJpegDecoderFastContext {
87     u8*     pSrc;
88     u32     inputDataSize;
89     void*   pDst;
90     u32     option;
91     u16     maxWidth;       // Maximum allowable image width (less than 65536). The argument maxWidth of the SSP_StartJpegDecoderFast function is copied here
92     u16     maxHeight;      // Maximum allowable image height (less than 65536). The argument maxHeight of the SSP_StartJpegDecoderFast function is copied here
93     u16     width;          // Actual image width
94     u16     height;         // Actual image height
95     SSPJpegDecoderErrorCode errorCode; // No error if 0
96     u8      reserved[4];
97     u32     work[0x7f8];
98 } SSPJpegDecoderFastContext;
99 
100 /*---------------------------------------------------------------------------*
101   Name:         SSP_CheckJpegDecoderSign
102 
103   Description:  Only checks a JPEG file's signature.
104 
105   Arguments:    data: JPEG file
106                 size: JPEG file size
107 
108   Returns:      TRUE if successful.
109  *---------------------------------------------------------------------------*/
110 BOOL SSP_CheckJpegDecoderSign(u8* data, u32 size);
111 
112 /*---------------------------------------------------------------------------*
113   Name:         SSP_StartJpegDecoder
114 
115   Description:  Decodes a JPEG file.
116 
117   Arguments:    data: JPEG file
118                 size: JPEG file size
119                 dst: Destination buffer for decoding (now only RGB555)
120                 width: Gives the maximum image width as an argument.
121                          When the image is decoded successfully, its width will be returned.
122                 height: Gives the maximum image height as an argument.
123                          When the image is decoded successfully, its height will be returned.
124                 option: Options (bitwise logical OR)
125                          SSP_JPEG_RGB555: Decode in the RGB555 format.
126                                 Currently, even if this is not specified, data will be decoded in the RGB555 format.
127                          SSP_JPEG_THUMBNAIL: Decode thumbnails. When this is unspecified, the main image will be decoded.
128                          SSP_JPEG_EXIF: Get only EXIF information
129 
130   Returns:      TRUE if successful.
131  *---------------------------------------------------------------------------*/
132 BOOL SSP_StartJpegDecoder(u8* data, u32 size, void* dst, s16* width, s16* height, u32 option);
133 
134 /*---------------------------------------------------------------------------*
135   Name:         SSP_StartJpegDecoderEx
136 
137   Description:  Specifies whether to use a signature and decode a JPEG file.
138 
139   Arguments:    data: JPEG file
140                 size: JPEG file size
141                 dst: Destination buffer for decoding (now only RGB555)
142                 width: Gives the maximum image width as an argument.
143                          When the image is decoded successfully, its width will be returned.
144                 height: Gives the maximum image height as an argument.
145                          When the image is decoded successfully, its height will be returned.
146                 option: Options (bitwise logical OR)
147                          SSP_JPEG_RGB555: Decode in the RGB555 format.
148                                 Currently, even if this is not specified, data will be decoded in the RGB555 format.
149                          SSP_JPEG_THUMBNAIL: Decode thumbnails. When this is unspecified, the main image will be decoded.
150                          SSP_JPEG_EXIF: Get only EXIF information
151                 sign: Specify TRUE to check the signature.
152 
153   Returns:      TRUE if successful.
154  *---------------------------------------------------------------------------*/
SSP_StartJpegDecoderEx(u8 * data,u32 size,void * dst,s16 * width,s16 * height,u32 option,BOOL sign)155 static inline BOOL SSP_StartJpegDecoderEx(u8* data, u32 size, void* dst, s16* width, s16* height, u32 option, BOOL sign)
156 {
157     BOOL result;
158     BOOL old_sign;
159 
160     old_sign = SSP_SetJpegDecoderSignMode(sign);
161     result = SSP_StartJpegDecoder(data, size, dst, width, height, option);
162     (void)SSP_SetJpegDecoderSignMode(old_sign);
163 
164     return result;
165 }
166 
167 /*---------------------------------------------------------------------------*
168   Name:         SSP_ExtractJpegDecoderExif
169 
170   Description:  Extracts only EXIF information from a JPEG file.
171                 Use the various get functions to get the actual extracted values.
172 
173   Arguments:    data: JPEG file
174                 size: JPEG file size
175 
176   Returns:      TRUE if successful.
177  *---------------------------------------------------------------------------*/
SSP_ExtractJpegDecoderExif(u8 * data,u32 size)178 static inline BOOL SSP_ExtractJpegDecoderExif(u8* data, u32 size)
179 {
180     return SSP_StartJpegDecoder(data, size, 0, 0, 0, SSP_JPEG_EXIF);
181 }
182 
183  /*---------------------------------------------------------------------------*
184   Name:         SSP_StartJpegDecoderFast
185 
186   Description:  Decodes a JPEG file (Fast version).
187 
188                 If the image size is a multiple of 8 or 16 pixels (whether 8 or 16 depends on the data format), this function can decode it with the fastest possible speed.
189 
190 
191   Arguments:    pCtx: Pointer to the context structure.
192                          Initialization before and after calling the function is unnecessary. After the function completes, the library will not use it again.
193                          The context structure is large, so do not allocate it from a stack in the same way as the local variables.
194                          Currently, you cannot call this function simultaneously from multiple strings.
195                 data: JPEG file
196                 size: JPEG file size
197                 dst: Destination buffer for decoding (now only RGB555)
198                          Regardless of the output format, 4-byte alignment is required.
199                 maxWidth: Gives the maximum image width as an argument. (Up to 65535)
200                             When the image is decoded successfully, the decoded image's width is returned to pCtx->width.
201                 maxHeight: Gives the maximum image height as an argument. (Up to 65535)
202                             When the image is decoded successfully, the decoded image's height is returned to pCtx->height.
203                 option: Options (bitwise logical OR)
204                          SSP_JPEG_RGB555: Decode in the RGB555 format.
205                                 Right now only RGB555 is available, but this specification cannot be omitted.
206                          SSP_JPEG_THUMBNAIL: Decode thumbnails. When this is unspecified, the main image will be decoded.
207 
208   Returns:      TRUE if successful.
209                 If failed, a detailed error code is stored in pCtx->errorCode.
210  *---------------------------------------------------------------------------*/
211 BOOL SSP_StartJpegDecoderFast(SSPJpegDecoderFastContext* pCtx, u8* data, u32 size, void* dst, u32 maxWidth, u32 maxHeight, u32 option);
212 
213  /*---------------------------------------------------------------------------*
214   Name:         SSP_StartJpegDecoderFastEx
215 
216   Description:  Decodes a JPEG file, specifying whether signature features are enabled (Fast version).
217 
218                 If the image size is a multiple of 8 or 16 pixels (whether 8 or 16 depends on the data format), this function can decode it with the fastest possible speed.
219 
220 
221   Arguments:    pCtx: Pointer to the context structure.
222                          Initialization before and after calling the function is unnecessary. After the function completes, the library will not use it again.
223                          The context structure is large, so do not allocate it from a stack in the same way as the local variables.
224                          Currently, you cannot call this function simultaneously from multiple strings.
225                 data: JPEG file
226                 size: JPEG file size
227                 dst: Destination buffer for decoding (now only RGB555)
228                          Regardless of the output format, 4-byte alignment is required.
229                 maxWidth: Gives the maximum image width as an argument. (Up to 65535)
230                             When the image is decoded successfully, the decoded image's width is returned to pCtx->width.
231                 maxHeight: Gives the maximum image height as an argument. (Up to 65535)
232                             When the image is decoded successfully, the decoded image's height is returned to pCtx->height.
233                 option: Options (bitwise logical OR)
234                          SSP_JPEG_RGB555: Decode in the RGB555 format.
235                                 Right now only RGB555 is available, but this specification cannot be omitted.
236                          SSP_JPEG_THUMBNAIL: Decode thumbnails. When this is unspecified, the main image will be decoded.
237                 sign: Specify TRUE to check the signature.
238 
239   Returns:      TRUE if successful.
240                 If failed, a detailed error code is stored in pCtx->errorCode.
241  *---------------------------------------------------------------------------*/
242 BOOL SSP_StartJpegDecoderFastEx(SSPJpegDecoderFastContext* pCtx, u8* data, u32 size, void* dst, u32 maxWidth, u32 maxHeight, u32 option, BOOL sign);
243 
244 #ifdef __cplusplus
245 } /* extern "C" */
246 #endif
247 
248 /* TWL_SSP_JPEGDEC_H_ */
249 #endif
250