1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include "jpeg_cmodel_def.h"
82 #include "apiJPEG.h"
83 #include "jpeg_cmodel.h"
84 #include "jpeg_cmodel_io.h"
85
86 #define INTERNAL_MEM_POOL_SIZE (13*1024*1024)
87 #define READ_BUFF_SIZE (384*1024 + 128)
88
89 // TGA_WRITER enable -> output file as RGB TGA file, else output to frame buffer directly
90 //-------------------------------------------------------------------------------------------------
91 // Defines
92 //-------------------------------------------------------------------------------------------------
93 static JPEG_FILE_FileSystem_t input_stream;
94 static PJPEG_FILE_FileSystem_t Pinput_stream = &input_stream;
95
96 static JPEG_FILE_TGAFileSystem_t tga_stream;
97 static PJPEG_FILE_TGAFileSystem_t Ptga_stream = &tga_stream;
98
99 static const char *Psrc_filename;
100 static const char *Pdst_filename;
101
JPEG_FillReadBuff(MS_U32 BufAddr,MS_U32 BufLength)102 static MS_S32 JPEG_FillReadBuff(MS_U32 BufAddr, MS_U32 BufLength)
103 {
104 MS_S32 bytes_read = 0;
105 MS_U32 buf_left = 0;
106 MS_BOOL bIsEOF = FALSE;
107
108 printf("verJPD_FillHdrFunc, bytes_read = %lu\n", BufAddr);
109 do
110 {
111 bytes_read = Pinput_stream->read((MS_U8 *)(BufAddr + buf_left), BufLength - buf_left, &bIsEOF, Pinput_stream);
112
113 if (bytes_read < 0)
114 {
115 return bytes_read;
116 }
117
118 buf_left += bytes_read;
119 } while(( buf_left < BufLength ) && ( !bIsEOF ));
120 MApi_JPEG_UpdateReadInfo(buf_left, bIsEOF);
121 return buf_left;
122 }
123
124
JPEGInit(void)125 BOOL JPEGInit( void )
126 {
127 printf("Main::JPEGInit\n");
128 Psrc_filename = NULL;
129 Pdst_filename = NULL;
130
131 Pinput_stream->constructor = JPEG_FILE_constructor;
132 Pinput_stream->destructor = JPEG_FILE_destructor;
133 Pinput_stream->open = JPEG_FILE_open;
134 Pinput_stream->close = JPEG_FILE_close;
135 Pinput_stream->read = JPEG_FILE_read;
136 Pinput_stream->get_error_status = JPEG_FILE_get_error_status;
137 Pinput_stream->reset = JPEG_FILE_reset;
138 Pinput_stream->get_size = JPEG_FILE_get_size;
139
140 Ptga_stream->constructor = JPEG_FILE_tga_constructor;
141 Ptga_stream->destructor = JPEG_FILE_tga_destructor;
142 Ptga_stream->open = JPEG_FILE_tga_open;
143 Ptga_stream->close = JPEG_FILE_tga_close;
144 Ptga_stream->writeline = JPEG_FILE_tga_writeline;
145
146 MApi_JPEG_SetDbgLevel(E_JPEG_DEBUG_ALL);
147
148 return TRUE;
149 }
150
main(int arg_c,char * arg_v[])151 int main( int arg_c, char *arg_v[] )
152 {
153 U8 *Pbuf = NULL;
154 int lines_decoded = 0;
155
156 BOOL status;
157 BOOL bIsEOF;
158
159 U8 *memory_pool;
160 U8 *read_buffer;
161 JPEG_InitParam init_param;
162
163 // set memory for JPEG buffer using
164 memory_pool = malloc(INTERNAL_MEM_POOL_SIZE*sizeof(U8));
165
166 if ( !memory_pool )
167 {
168 printf( "Error: Out of memory!\n" );
169 }
170
171
172 read_buffer = malloc((READ_BUFF_SIZE + 128)*sizeof(U8));
173
174 if ( !read_buffer )
175 {
176 printf( "Error: Out of memory!\n" );
177 }
178
179
180 init_param.u32MRCBufAddr = (U32)read_buffer;
181 init_param.u32MRCBufSize = READ_BUFF_SIZE;
182 init_param.u32MWCBufAddr = 0;
183 init_param.u32MWCBufSize = 0;
184 init_param.u32InternalBufAddr = (U32)memory_pool;
185 init_param.u32InternalBufSize = INTERNAL_MEM_POOL_SIZE;
186 init_param.bInitMem = TRUE;
187 // MApi_JPEG_SetInitParameter(&init_param);
188
189 printf( "JPEG to TGA file conversion example. Compiled %s %s\n", __DATE__, __TIME__ );
190 printf( "Copyright (C) 1994-2000 Rich Geldreich\n" );
191
192 if ( arg_c != 4 )
193 {
194 printf( "Usage: jpg2tga <source_file> <dest_file> <type>\n" );
195 printf( "Outputs greyscale and truecolor 24-bit TGA files.\n" );
196 return ( EXIT_FAILURE );
197 }
198
199 JPEGInit();
200
201 Psrc_filename = arg_v[1];
202 Pdst_filename = arg_v[2];
203
204 printf( "Source file: \"%s\"\n", Psrc_filename );
205 printf( "Destination file: \"%s\"\n", Pdst_filename );
206
207
208 //Pjpeg_decoder_file_stream Pinput_stream = new jpeg_decoder_file_stream();
209 Pinput_stream->constructor( Pinput_stream );
210
211 if ( Pinput_stream->open( Psrc_filename, Pinput_stream ) )
212 {
213 //delete Pinput_stream;
214 Pinput_stream->destructor( Pinput_stream );
215 printf( "Error: Unable to open file \"%s\" for reading!\n", Psrc_filename );
216 return ( EXIT_FAILURE );
217 }
218
219 init_param.u32DecByteRead = Pinput_stream->read(read_buffer, READ_BUFF_SIZE - 128, &bIsEOF, Pinput_stream);
220 init_param.bEOF = bIsEOF;
221 init_param.pFillHdrFunc = (JPEG_FillHdrFunc)JPEG_FillReadBuff;
222
223 //Pjpeg_decoder Pd = new jpeg_decoder(Pinput_stream, use_mmx);
224 if(strcmp(arg_v[3], "1")==0)
225 {
226 //MApi_JPEG_Constructor( Pinput_stream, E_JPEG_TYPE_MAIN);
227 init_param.u8DecodeType = E_JPEG_TYPE_MAIN;
228 MApi_JPEG_Init(&init_param);
229 }
230 else
231 {
232 //MApi_JPEG_Constructor( Pinput_stream, E_JPEG_TYPE_THUMBNAIL);
233 init_param.u8DecodeType = E_JPEG_TYPE_THUMBNAIL;
234 MApi_JPEG_Init(&init_param);
235 }
236
237 if ( MApi_JPEG_GetErrorCode() != E_JPEG_NO_ERROR )
238 {
239 printf( "Error: Decoder failed! Error status: %i\n", MApi_JPEG_GetErrorCode() );
240
241 // Always be sure to delete the input stream object _after_
242 // the decoder is deleted. Reason: the decoder object calls the input
243 // stream's detach() method.
244 //delete Pd;
245 MApi_JPEG_Exit();
246 //delete Pinput_stream;
247 Pinput_stream->destructor( Pinput_stream );
248
249 return ( EXIT_FAILURE );
250 }
251
252 if(MApi_JPEG_DecodeHdr()==E_JPEG_FAILED)
253 {
254 printf("Decode Header error\n");
255 // Always be sure to delete the input stream object _after_
256 // the decoder is deleted. Reason: the decoder object calls the input
257 // stream's detach() method.
258 //delete Pd;
259 MApi_JPEG_Exit();
260 //delete Pinput_stream;
261 Pinput_stream->destructor( Pinput_stream );
262
263 return ( EXIT_FAILURE );
264 }
265
266 #ifdef NO_OUTPUT
267 Ptga_stream = NULL;
268 #else
269 //Pdst = new tga_writer();
270
271 Ptga_stream->constructor( Ptga_stream );
272
273 status = Ptga_stream->open( Pdst_filename, MApi_JPEG_GetWidth(), MApi_JPEG_GetHeight(),
274 ( JPEG_CMODEL_get_num_components() == 1 ) ? E_TGA_IMAGE_TYPE_GREY : E_TGA_IMAGE_TYPE_BGR, Ptga_stream );
275
276 if ( status )
277 {
278 //delete Pd;
279 MApi_JPEG_Exit();
280 //delete Pinput_stream;
281 Pinput_stream->destructor( Pinput_stream );
282 //delete Pdst;
283 Ptga_stream->destructor( Ptga_stream );
284
285 printf( "Error: Unable to open file \"%s\" for writing!\n", Pdst_filename );
286
287 return ( EXIT_FAILURE );
288 }
289 #endif
290
291 printf( "Width: %i\n", MApi_JPEG_GetWidth() );
292 printf( "Height: %i\n", MApi_JPEG_GetHeight() );
293 printf( "Components: %i\n", JPEG_CMODEL_get_num_components() );
294
295 #if 0
296 if ( msAPI_JPEG_begin() )
297 {
298 printf( "Error: Decoder failed! Error status: %i\n", msAPI_JPEG_get_error_code() );
299
300 //delete Pd;
301 msAPI_JPEG_Finalize();
302 //delete Pinput_stream;
303 Pinput_stream->destructor( Pinput_stream );
304 //delete Pdst;
305 Ptga_stream->destructor( Ptga_stream );
306 remove( Pdst_filename );
307
308 return ( EXIT_FAILURE );
309 }
310 #endif
311
312 JPEG_CMODEL_DumpTables();
313
314 if ( JPEG_CMODEL_get_num_components() == 3 )
315 {
316 Pbuf = ( U8 * )malloc( MApi_JPEG_GetWidth() * 3 );
317 if ( !Pbuf )
318 {
319 printf( "Error: Out of memory!\n" );
320
321 //delete Pd;
322 MApi_JPEG_Exit();
323 //delete Pinput_stream;
324 Pinput_stream->destructor( Pinput_stream );
325 //delete Pdst;
326 Ptga_stream->destructor( Ptga_stream );
327 remove( Pdst_filename );
328
329 return ( EXIT_FAILURE );
330 }
331 }
332
333 if(MApi_JPEG_IsProgressive())
334 {
335 printf("Do RLE\n");
336 printf("==========\n");
337 }
338
339 for ( ; ; )
340 {
341 void *Pscan_line_ofs;
342 U32 scan_line_len;
343 BOOL status;
344
345 if (E_JPEG_OKAY == JPEG_CMODEL_decode( &Pscan_line_ofs, &scan_line_len ))
346 {
347 break;
348 }
349
350 lines_decoded++;
351
352 #ifndef NO_OUTPUT
353 if ( JPEG_CMODEL_get_num_components() == 3 )
354 {
355 U8 *Psb = ( U8 * )Pscan_line_ofs;
356 U8 *Pdb = Pbuf;
357 int src_bpp = JPEG_CMODEL_get_bytes_per_pixel();
358 int x;
359
360 for ( x = MApi_JPEG_GetWidth(); x > 0; x--, Psb += src_bpp, Pdb += 3 )
361 {
362 Pdb[0] = Psb[2];
363 Pdb[1] = Psb[1];
364 Pdb[2] = Psb[0];
365 }
366
367 status = Ptga_stream->writeline( Pbuf, Ptga_stream );
368 }
369 else
370 {
371 status = Ptga_stream->writeline( Pscan_line_ofs, Ptga_stream );
372 }
373
374 if ( status )
375 {
376 printf( "Error: Unable to write to file \"%s\"!\n", Pdst_filename );
377
378 free( Pbuf );
379 //delete Pd;
380 MApi_JPEG_Exit();
381 //delete Pinput_stream;
382 Pinput_stream->destructor( Pinput_stream );
383 //delete Pdst;
384 Ptga_stream->destructor( Ptga_stream );
385 remove( Pdst_filename );
386
387 return ( EXIT_FAILURE );
388 }
389 #endif
390 }
391
392 free( Pbuf );
393
394 if(MApi_JPEG_IsProgressive())
395 {
396 printf("==========\n");
397 printf("RLE end\n");
398 }
399
400 #ifndef NO_OUTPUT
401 if ( Ptga_stream->close( Ptga_stream ) )
402 {
403 printf( "Error: Unable to write to file \"%s\"!\n", Pdst_filename );
404
405 //delete Pd;
406 MApi_JPEG_Exit();
407 //delete Pinput_stream;
408 Pinput_stream->destructor( Pinput_stream );
409 //delete Pdst;
410 Ptga_stream->destructor( Ptga_stream );
411 remove( Pdst_filename );
412
413 return ( EXIT_FAILURE );
414 }
415
416 //delete Pdst;
417 Ptga_stream->destructor( Ptga_stream );
418 #endif
419
420 if (E_JPEG_NO_ERROR != MApi_JPEG_GetErrorCode())
421 {
422 printf( "Error: Decoder failed! Error status: %i\n", MApi_JPEG_GetErrorCode() );
423
424 //delete Pd;
425 MApi_JPEG_Exit();
426 //delete Pinput_stream;
427 Pinput_stream->destructor( Pinput_stream );
428 //delete Pdst;
429 Ptga_stream->destructor( Ptga_stream );
430
431 return ( EXIT_FAILURE );
432 }
433
434 //msAPI_JPEG_Dump_Tbl();
435
436 printf( "Lines decoded: %i\n", lines_decoded );
437 printf( "Input file size: %ld\n", Pinput_stream->get_size( Pinput_stream ) );
438 printf( "Input bytes actually read: %ld\n", JPEG_CMODEL_get_total_bytes_read() );
439
440 //delete Pd;
441 MApi_JPEG_Exit();
442 //delete Pinput_stream;
443 Pinput_stream->destructor( Pinput_stream );
444
445 return ( EXIT_SUCCESS );
446 }
447 //------------------------------------------------------------------------------
448
449
450