Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / pdfium / core / src / fxcodec / codec / fx_codec_jpeg.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4  
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../../include/fxcodec/fx_codec.h"
8 #include "../../../include/fxge/fx_dib.h"
9 #include "codec_int.h"
10 extern "C" {
11     static void _JpegScanSOI(const FX_BYTE*& src_buf, FX_DWORD& src_size)
12     {
13         if (src_size == 0) {
14             return;
15         }
16         FX_DWORD offset = 0;
17         while (offset < src_size - 1) {
18             if (src_buf[offset] == 0xff && src_buf[offset + 1] == 0xd8) {
19                 src_buf += offset;
20                 src_size -= offset;
21                 return;
22             }
23             offset ++;
24         }
25     }
26 };
27 extern "C" {
28 #undef FAR
29 #include "../../fx_jpeglib.h"
30 }
31 extern "C" {
32     static void _src_do_nothing(struct jpeg_decompress_struct* cinfo) {}
33 };
34 extern "C" {
35     static void _error_fatal(j_common_ptr cinfo)
36     {
37         longjmp(*(jmp_buf*)cinfo->client_data, -1);
38     }
39 };
40 extern "C" {
41     static void _src_skip_data(struct jpeg_decompress_struct* cinfo, long num)
42     {
43         if (num > (long)cinfo->src->bytes_in_buffer) {
44             _error_fatal((j_common_ptr)cinfo);
45         }
46         cinfo->src->next_input_byte += num;
47         cinfo->src->bytes_in_buffer -= num;
48     }
49 };
50 extern "C" {
51     static boolean _src_fill_buffer(j_decompress_ptr cinfo)
52     {
53         return 0;
54     }
55 };
56 extern "C" {
57     static boolean _src_resync(j_decompress_ptr cinfo, int desired)
58     {
59         return 0;
60     }
61 };
62 extern "C" {
63     static void _error_do_nothing(j_common_ptr cinfo) {}
64 };
65 extern "C" {
66     static void _error_do_nothing1(j_common_ptr cinfo, int) {}
67 };
68 extern "C" {
69     static void _error_do_nothing2(j_common_ptr cinfo, char*) {}
70 };
71 #define JPEG_MARKER_EXIF                (JPEG_APP0 + 1)
72 #define JPEG_MARKER_ICC                 (JPEG_APP0 + 2)
73 #define JPEG_MARKER_AUTHORTIME  (JPEG_APP0 + 3)
74 #define JPEG_MARKER_MAXSIZE     0xFFFF
75 #define JPEG_OVERHEAD_LEN       14
76 static  FX_BOOL _JpegEmbedIccProfile(j_compress_ptr cinfo, FX_LPCBYTE icc_buf_ptr, FX_DWORD icc_length)
77 {
78     if(icc_buf_ptr == NULL || icc_length == 0) {
79         return FALSE;
80     }
81     FX_DWORD icc_segment_size = (JPEG_MARKER_MAXSIZE - 2 - JPEG_OVERHEAD_LEN);
82     FX_DWORD icc_segment_num = (icc_length / icc_segment_size) + 1;
83     if (icc_segment_num > 255)  {
84         return FALSE;
85     }
86     FX_DWORD icc_data_length = JPEG_OVERHEAD_LEN + (icc_segment_num > 1 ? icc_segment_size : icc_length);
87     FX_LPBYTE icc_data = FX_Alloc(FX_BYTE, icc_data_length);
88     if (icc_data == NULL) {
89         return FALSE;
90     }
91     FXSYS_memcpy32(icc_data, "\x49\x43\x43\x5f\x50\x52\x4f\x46\x49\x4c\x45\x00", 12);
92     icc_data[13] = (FX_BYTE)icc_segment_num;
93     for (FX_BYTE i = 0; i < (icc_segment_num - 1); i++) {
94         icc_data[12] = i + 1;
95         FXSYS_memcpy32(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + i * icc_segment_size, icc_segment_size);
96         jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, icc_data_length);
97     }
98     icc_data[12] = (FX_BYTE)icc_segment_num;
99     FX_DWORD icc_size = (icc_segment_num - 1) * icc_segment_size;
100     FXSYS_memcpy32(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + icc_size, icc_length - icc_size);
101     jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, JPEG_OVERHEAD_LEN + icc_length - icc_size);
102     FX_Free(icc_data);
103     return TRUE;
104 }
105 extern "C" {
106     static void _dest_do_nothing(j_compress_ptr cinfo) {}
107 };
108 extern "C" {
109     static boolean _dest_empty(j_compress_ptr cinfo)
110     {
111         return FALSE;
112     }
113 };
114 #define JPEG_BLOCK_SIZE 1048576
115 static void _JpegEncode(const CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality, FX_LPCBYTE icc_buf, FX_DWORD icc_length)
116 {
117     struct jpeg_compress_struct cinfo;
118     struct jpeg_error_mgr jerr;
119     jerr.error_exit = _error_do_nothing;
120     jerr.emit_message = _error_do_nothing1;
121     jerr.output_message = _error_do_nothing;
122     jerr.format_message = _error_do_nothing2;
123     jerr.reset_error_mgr = _error_do_nothing;
124     cinfo.err = &jerr;
125     jpeg_create_compress(&cinfo);
126     int Bpp = pSource->GetBPP() / 8;
127     int nComponents = Bpp >= 3 ? (pSource->IsCmykImage() ? 4 : 3) : 1;
128     int pitch = pSource->GetPitch();
129     int width = pSource->GetWidth();
130     int height = pSource->GetHeight();
131     FX_DWORD dest_buf_length = width * height * nComponents + 1024 + (icc_length ? (icc_length + 255 * 18) : 0);
132     dest_buf = FX_Alloc(FX_BYTE, dest_buf_length);
133     while (dest_buf == NULL) {
134         dest_buf_length >>= 1;
135         dest_buf = FX_Alloc(FX_BYTE, dest_buf_length);
136     }
137     struct jpeg_destination_mgr dest;
138     dest.init_destination = _dest_do_nothing;
139     dest.term_destination = _dest_do_nothing;
140     dest.empty_output_buffer = _dest_empty;
141     dest.next_output_byte = dest_buf;
142     dest.free_in_buffer = dest_buf_length;
143     cinfo.dest = &dest;
144     cinfo.image_width = width;
145     cinfo.image_height = height;
146     cinfo.input_components = nComponents;
147     if (nComponents == 1) {
148         cinfo.in_color_space = JCS_GRAYSCALE;
149     } else if (nComponents == 3) {
150         cinfo.in_color_space = JCS_RGB;
151     } else {
152         cinfo.in_color_space = JCS_CMYK;
153     }
154     FX_LPBYTE line_buf = NULL;
155     if (nComponents > 1) {
156         line_buf = FX_Alloc(FX_BYTE, width * nComponents);
157         if (line_buf == NULL) {
158             return;
159         }
160     }
161     jpeg_set_defaults(&cinfo);
162     if(quality != 75) {
163         jpeg_set_quality(&cinfo, quality, TRUE);
164     }
165     jpeg_start_compress(&cinfo, TRUE);
166     _JpegEmbedIccProfile(&cinfo, icc_buf, icc_length);
167     JSAMPROW row_pointer[1];
168     JDIMENSION row;
169     while (cinfo.next_scanline < cinfo.image_height) {
170         FX_LPCBYTE src_scan = pSource->GetScanline(cinfo.next_scanline);
171         if (nComponents > 1) {
172             FX_LPBYTE dest_scan = line_buf;
173             if (nComponents == 3) {
174                 for (int i = 0; i < width; i ++) {
175                     dest_scan[0] = src_scan[2];
176                     dest_scan[1] = src_scan[1];
177                     dest_scan[2] = src_scan[0];
178                     dest_scan += 3;
179                     src_scan += Bpp;
180                 }
181             } else {
182                 for (int i = 0; i < pitch; i ++) {
183                     *dest_scan++ = ~*src_scan++;
184                 }
185             }
186             row_pointer[0] = line_buf;
187         } else {
188             row_pointer[0] = (FX_LPBYTE)src_scan;
189         }
190         row = cinfo.next_scanline;
191         jpeg_write_scanlines(&cinfo, row_pointer, 1);
192         if (cinfo.next_scanline == row) {
193             dest_buf = FX_Realloc(FX_BYTE, dest_buf, dest_buf_length + JPEG_BLOCK_SIZE);
194             if (dest_buf == NULL) {
195                 FX_Free(line_buf);
196                 return;
197             }
198             dest.next_output_byte = dest_buf + dest_buf_length - dest.free_in_buffer;
199             dest_buf_length += JPEG_BLOCK_SIZE;
200             dest.free_in_buffer += JPEG_BLOCK_SIZE;
201         }
202     }
203     jpeg_finish_compress(&cinfo);
204     jpeg_destroy_compress(&cinfo);
205     if (line_buf) {
206         FX_Free(line_buf);
207     }
208     dest_size = dest_buf_length - (FX_STRSIZE)dest.free_in_buffer;
209 }
210 static FX_BOOL _JpegLoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height,
211                              int& num_components, int& bits_per_components, FX_BOOL& color_transform,
212                              FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length)
213 {
214     _JpegScanSOI(src_buf, src_size);
215     struct jpeg_decompress_struct cinfo;
216     struct jpeg_error_mgr jerr;
217     jerr.error_exit = _error_fatal;
218     jerr.emit_message = _error_do_nothing1;
219     jerr.output_message = _error_do_nothing;
220     jerr.format_message = _error_do_nothing2;
221     jerr.reset_error_mgr = _error_do_nothing;
222     jerr.trace_level = 0;
223     cinfo.err = &jerr;
224     jmp_buf mark;
225     cinfo.client_data = &mark;
226     if (setjmp(mark) == -1) {
227         return FALSE;
228     }
229     jpeg_create_decompress(&cinfo);
230     struct jpeg_source_mgr src;
231     src.init_source = _src_do_nothing;
232     src.term_source = _src_do_nothing;
233     src.skip_input_data = _src_skip_data;
234     src.fill_input_buffer = _src_fill_buffer;
235     src.resync_to_restart = _src_resync;
236     src.bytes_in_buffer = src_size;
237     src.next_input_byte = src_buf;
238     cinfo.src = &src;
239     if (setjmp(mark) == -1) {
240         jpeg_destroy_decompress(&cinfo);
241         return FALSE;
242     }
243     if(icc_buf_ptr && icc_length) {
244         jpeg_save_markers(&cinfo, JPEG_MARKER_ICC, JPEG_MARKER_MAXSIZE);
245     }
246     int ret = jpeg_read_header(&cinfo, TRUE);
247     if (ret != JPEG_HEADER_OK) {
248         jpeg_destroy_decompress(&cinfo);
249         return FALSE;
250     }
251     width = cinfo.image_width;
252     height = cinfo.image_height;
253     num_components = cinfo.num_components;
254     color_transform = cinfo.jpeg_color_space == JCS_YCbCr || cinfo.jpeg_color_space == JCS_YCCK;
255     bits_per_components = cinfo.data_precision;
256     if(icc_buf_ptr != NULL) {
257         *icc_buf_ptr = NULL;
258     }
259     if(icc_length != NULL) {
260         *icc_length = 0;
261     }
262     jpeg_destroy_decompress(&cinfo);
263     return TRUE;
264 }
265 class CCodec_JpegDecoder : public CCodec_ScanlineDecoder
266 {
267 public:
268     CCodec_JpegDecoder();
269     ~CCodec_JpegDecoder();
270     FX_BOOL                             Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, int nComps,
271                                FX_BOOL ColorTransform, IFX_JpegProvider* pJP);
272     virtual void                Destroy()
273     {
274         delete this;
275     }
276     virtual void                v_DownScale(int dest_width, int dest_height);
277     virtual FX_BOOL             v_Rewind();
278     virtual FX_LPBYTE   v_GetNextLine();
279     virtual FX_DWORD    GetSrcOffset();
280     jmp_buf             m_JmpBuf;
281     struct jpeg_decompress_struct cinfo;
282     struct jpeg_error_mgr jerr;
283     struct jpeg_source_mgr src;
284     FX_LPCBYTE  m_SrcBuf;
285     FX_DWORD    m_SrcSize;
286     FX_LPBYTE   m_pScanlineBuf;
287     FX_BOOL             InitDecode();
288     FX_BOOL             m_bInited, m_bStarted, m_bJpegTransform;
289 protected:
290     IFX_JpegProvider*   m_pExtProvider;
291     void*                               m_pExtContext;
292     FX_DWORD                    m_nDefaultScaleDenom;
293 };
294 CCodec_JpegDecoder::CCodec_JpegDecoder()
295 {
296     m_pScanlineBuf = NULL;
297     m_DownScale = 1;
298     m_bStarted = FALSE;
299     m_bInited = FALSE;
300     m_pExtProvider = NULL;
301     m_pExtContext = NULL;
302     FXSYS_memset32(&cinfo, 0, sizeof(cinfo));
303     FXSYS_memset32(&jerr, 0, sizeof(jerr));
304     FXSYS_memset32(&src, 0, sizeof(src));
305     m_nDefaultScaleDenom = 1;
306 }
307 CCodec_JpegDecoder::~CCodec_JpegDecoder()
308 {
309     if (m_pExtProvider) {
310         m_pExtProvider->DestroyDecoder(m_pExtContext);
311         return;
312     }
313     if (m_pScanlineBuf) {
314         FX_Free(m_pScanlineBuf);
315     }
316     if (m_bInited) {
317         jpeg_destroy_decompress(&cinfo);
318     }
319 }
320 FX_BOOL CCodec_JpegDecoder::InitDecode()
321 {
322     cinfo.err = &jerr;
323     cinfo.client_data = &m_JmpBuf;
324     if (setjmp(m_JmpBuf) == -1) {
325         return FALSE;
326     }
327     jpeg_create_decompress(&cinfo);
328     m_bInited = TRUE;
329     cinfo.src = &src;
330     src.bytes_in_buffer = m_SrcSize;
331     src.next_input_byte = m_SrcBuf;
332     if (setjmp(m_JmpBuf) == -1) {
333         jpeg_destroy_decompress(&cinfo);
334         m_bInited = FALSE;
335         return FALSE;
336     }
337     cinfo.image_width = m_OrigWidth;
338     cinfo.image_height = m_OrigHeight;
339     int ret = jpeg_read_header(&cinfo, TRUE);
340     if (ret != JPEG_HEADER_OK) {
341         return FALSE;
342     }
343     if (cinfo.saw_Adobe_marker) {
344         m_bJpegTransform = TRUE;
345     }
346     if (cinfo.num_components == 3 && !m_bJpegTransform) {
347         cinfo.out_color_space = cinfo.jpeg_color_space;
348     }
349     m_OrigWidth = cinfo.image_width;
350     m_OrigHeight = cinfo.image_height;
351     m_OutputWidth = m_OrigWidth;
352     m_OutputHeight = m_OrigHeight;
353     m_nDefaultScaleDenom = cinfo.scale_denom;
354     return TRUE;
355 }
356 FX_BOOL CCodec_JpegDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
357                                    int nComps, FX_BOOL ColorTransform, IFX_JpegProvider* pJP)
358 {
359     if (pJP) {
360         m_pExtProvider = pJP;
361         m_pExtContext = m_pExtProvider->CreateDecoder(src_buf, src_size, width, height, nComps, ColorTransform);
362         return m_pExtContext != NULL;
363     }
364     _JpegScanSOI(src_buf, src_size);
365     m_SrcBuf = src_buf;
366     m_SrcSize = src_size;
367     jerr.error_exit = _error_fatal;
368     jerr.emit_message = _error_do_nothing1;
369     jerr.output_message = _error_do_nothing;
370     jerr.format_message = _error_do_nothing2;
371     jerr.reset_error_mgr = _error_do_nothing;
372     src.init_source = _src_do_nothing;
373     src.term_source = _src_do_nothing;
374     src.skip_input_data = _src_skip_data;
375     src.fill_input_buffer = _src_fill_buffer;
376     src.resync_to_restart = _src_resync;
377     m_bJpegTransform = ColorTransform;
378     if(src_size > 1 && FXSYS_memcmp32(src_buf + src_size - 2, "\xFF\xD9", 2) != 0) {
379         ((FX_LPBYTE)src_buf)[src_size - 2] = 0xFF;
380         ((FX_LPBYTE)src_buf)[src_size - 1] = 0xD9;
381     }
382     m_OutputWidth = m_OrigWidth = width;
383     m_OutputHeight = m_OrigHeight = height;
384     if (!InitDecode()) {
385         return FALSE;
386     }
387     if (cinfo.num_components < nComps) {
388         return FALSE;
389     }
390     if ((int)cinfo.image_width < width) {
391         return FALSE;
392     }
393     m_Pitch = (cinfo.image_width * cinfo.num_components + 3) / 4 * 4;
394     m_pScanlineBuf = FX_Alloc(FX_BYTE, m_Pitch);
395     if (m_pScanlineBuf == NULL) {
396         return FALSE;
397     }
398     m_nComps = cinfo.num_components;
399     m_bpc = 8;
400     m_bColorTransformed = FALSE;
401     m_bStarted = FALSE;
402     return TRUE;
403 }
404 extern "C" {
405     FX_INT32 FX_GetDownsampleRatio(FX_INT32 originWidth, FX_INT32 originHeight, FX_INT32 downsampleWidth, FX_INT32 downsampleHeight)
406     {
407         int iratio_w = originWidth / downsampleWidth;
408         int iratio_h = originHeight / downsampleHeight;
409         int ratio = (iratio_w > iratio_h) ? iratio_h : iratio_w;
410         if (ratio >= 8) {
411             return 8;
412         } else if (ratio >= 4) {
413             return 4;
414         } else if (ratio >= 2) {
415             return 2;
416         }
417         return 1;
418     }
419 }
420 void CCodec_JpegDecoder::v_DownScale(int dest_width, int dest_height)
421 {
422     if (m_pExtProvider) {
423         m_pExtProvider->DownScale(m_pExtContext, dest_width, dest_height);
424         return;
425     }
426     int old_scale = m_DownScale;
427     m_DownScale = FX_GetDownsampleRatio(m_OrigWidth, m_OrigHeight, dest_width, dest_height);
428     m_OutputWidth = (m_OrigWidth + m_DownScale - 1) / m_DownScale;
429     m_OutputHeight = (m_OrigHeight + m_DownScale - 1) / m_DownScale;
430     m_Pitch = (m_OutputWidth * m_nComps + 3) / 4 * 4;
431     if (old_scale != m_DownScale) {
432         m_NextLine = -1;
433     }
434 }
435 FX_BOOL CCodec_JpegDecoder::v_Rewind()
436 {
437     if (m_pExtProvider) {
438         return m_pExtProvider->Rewind(m_pExtContext);
439     }
440     if (m_bStarted) {
441         jpeg_destroy_decompress(&cinfo);
442         if (!InitDecode()) {
443             return FALSE;
444         }
445     }
446     if (setjmp(m_JmpBuf) == -1) {
447         return FALSE;
448     }
449     cinfo.scale_denom = m_nDefaultScaleDenom * m_DownScale;
450     m_OutputWidth = (m_OrigWidth + m_DownScale - 1) / m_DownScale;
451     m_OutputHeight = (m_OrigHeight + m_DownScale - 1) / m_DownScale;
452     if (!jpeg_start_decompress(&cinfo)) {
453         jpeg_destroy_decompress(&cinfo);
454         return FALSE;
455     }
456     if ((int)cinfo.output_width > m_OrigWidth) {
457         FXSYS_assert(FALSE);
458         return FALSE;
459     }
460     m_bStarted = TRUE;
461     return TRUE;
462 }
463 FX_LPBYTE CCodec_JpegDecoder::v_GetNextLine()
464 {
465     if (m_pExtProvider) {
466         return m_pExtProvider->GetNextLine(m_pExtContext);
467     }
468     int nlines = jpeg_read_scanlines(&cinfo, &m_pScanlineBuf, 1);
469     if (nlines < 1) {
470         return NULL;
471     }
472     return m_pScanlineBuf;
473 }
474 FX_DWORD CCodec_JpegDecoder::GetSrcOffset()
475 {
476     if (m_pExtProvider) {
477         return m_pExtProvider->GetSrcOffset(m_pExtContext);
478     }
479     return (FX_DWORD)(m_SrcSize - src.bytes_in_buffer);
480 }
481 ICodec_ScanlineDecoder* CCodec_JpegModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size,
482         int width, int height, int nComps, FX_BOOL ColorTransform)
483 {
484     if (src_buf == NULL || src_size == 0) {
485         return NULL;
486     }
487     CCodec_JpegDecoder* pDecoder = FX_NEW CCodec_JpegDecoder;
488     if (pDecoder == NULL) {
489         return NULL;
490     }
491     if (!pDecoder->Create(src_buf, src_size, width, height, nComps, ColorTransform, m_pExtProvider)) {
492         delete pDecoder;
493         return NULL;
494     }
495     return pDecoder;
496 }
497 FX_BOOL CCodec_JpegModule::LoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height,
498                                     int& num_components, int& bits_per_components, FX_BOOL& color_transform,
499                                     FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length)
500 {
501     if (m_pExtProvider) {
502         return m_pExtProvider->LoadInfo(src_buf, src_size, width, height,
503                                         num_components, bits_per_components, color_transform,
504                                         icc_buf_ptr, icc_length);
505     }
506     return _JpegLoadInfo(src_buf, src_size, width, height, num_components, bits_per_components, color_transform, icc_buf_ptr, icc_length);
507 }
508 FX_BOOL CCodec_JpegModule::Encode(const CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality, FX_LPCBYTE icc_buf, FX_DWORD icc_length)
509 {
510     if (m_pExtProvider) {
511         return m_pExtProvider->Encode(pSource, dest_buf, dest_size, quality, icc_buf, icc_length);
512     }
513     if(pSource->GetBPP() < 8 || pSource->GetPalette() != NULL) {
514         ASSERT(pSource->GetBPP() >= 8 && pSource->GetPalette() == NULL);
515         return FALSE;
516     }
517     _JpegEncode(pSource, dest_buf, dest_size, quality, icc_buf, icc_length);
518     return TRUE;
519 }
520 struct FXJPEG_Context {
521     jmp_buf                     m_JumpMark;
522     jpeg_decompress_struct m_Info;
523     jpeg_error_mgr      m_ErrMgr;
524     jpeg_source_mgr     m_SrcMgr;
525     unsigned int        m_SkipSize;
526     void*               (*m_AllocFunc)(unsigned int);
527     void                (*m_FreeFunc)(void*);
528 };
529 extern "C" {
530     static void _error_fatal1(j_common_ptr cinfo)
531     {
532         longjmp(((FXJPEG_Context*)cinfo->client_data)->m_JumpMark, -1);
533     }
534 };
535 extern "C" {
536     static void _src_skip_data1(struct jpeg_decompress_struct* cinfo, long num)
537     {
538         if (cinfo->src->bytes_in_buffer < (size_t)num) {
539             ((FXJPEG_Context*)cinfo->client_data)->m_SkipSize = (unsigned int)(num - cinfo->src->bytes_in_buffer);
540             cinfo->src->bytes_in_buffer = 0;
541         } else {
542             cinfo->src->next_input_byte += num;
543             cinfo->src->bytes_in_buffer -= num;
544         }
545     }
546 };
547 static void* jpeg_alloc_func(unsigned int size)
548 {
549     return FX_Alloc(char, size);
550 }
551 static void jpeg_free_func(void* p)
552 {
553     FX_Free(p);
554 }
555 void* CCodec_JpegModule::Start()
556 {
557     if (m_pExtProvider) {
558         return m_pExtProvider->Start();
559     }
560     FXJPEG_Context* p = (FXJPEG_Context*)FX_Alloc(FX_BYTE, sizeof(FXJPEG_Context));
561     if (p == NULL) {
562         return NULL;
563     }
564     p->m_AllocFunc = jpeg_alloc_func;
565     p->m_FreeFunc = jpeg_free_func;
566     p->m_ErrMgr.error_exit = _error_fatal1;
567     p->m_ErrMgr.emit_message = _error_do_nothing1;
568     p->m_ErrMgr.output_message = _error_do_nothing;
569     p->m_ErrMgr.format_message = _error_do_nothing2;
570     p->m_ErrMgr.reset_error_mgr = _error_do_nothing;
571     p->m_SrcMgr.init_source = _src_do_nothing;
572     p->m_SrcMgr.term_source = _src_do_nothing;
573     p->m_SrcMgr.skip_input_data = _src_skip_data1;
574     p->m_SrcMgr.fill_input_buffer = _src_fill_buffer;
575     p->m_SrcMgr.resync_to_restart = _src_resync;
576     p->m_Info.client_data = p;
577     p->m_Info.err = &p->m_ErrMgr;
578     if (setjmp(p->m_JumpMark) == -1) {
579         return 0;
580     }
581     jpeg_create_decompress(&p->m_Info);
582     p->m_Info.src = &p->m_SrcMgr;
583     p->m_SkipSize = 0;
584     return p;
585 }
586 void CCodec_JpegModule::Finish(void* pContext)
587 {
588     if (m_pExtProvider) {
589         m_pExtProvider->Finish(pContext);
590         return;
591     }
592     FXJPEG_Context* p = (FXJPEG_Context*)pContext;
593     jpeg_destroy_decompress(&p->m_Info);
594     p->m_FreeFunc(p);
595 }
596 void CCodec_JpegModule::Input(void* pContext, const unsigned char* src_buf, FX_DWORD src_size)
597 {
598     if (m_pExtProvider) {
599         m_pExtProvider->Input(pContext, src_buf, src_size);
600         return;
601     }
602     FXJPEG_Context* p = (FXJPEG_Context*)pContext;
603     if (p->m_SkipSize) {
604         if (p->m_SkipSize > src_size) {
605             p->m_SrcMgr.bytes_in_buffer = 0;
606             p->m_SkipSize -= src_size;
607             return;
608         }
609         src_size -= p->m_SkipSize;
610         src_buf += p->m_SkipSize;
611         p->m_SkipSize = 0;
612     }
613     p->m_SrcMgr.next_input_byte = src_buf;
614     p->m_SrcMgr.bytes_in_buffer = src_size;
615 }
616 int CCodec_JpegModule::ReadHeader(void* pContext, int* width, int* height, int* nComps)
617 {
618     if (m_pExtProvider) {
619         return m_pExtProvider->ReadHeader(pContext, width, height, nComps);
620     }
621     FXJPEG_Context* p = (FXJPEG_Context*)pContext;
622     if (setjmp(p->m_JumpMark) == -1) {
623         return 1;
624     }
625     int ret = jpeg_read_header(&p->m_Info, true);
626     if (ret == JPEG_SUSPENDED) {
627         return 2;
628     }
629     if (ret != JPEG_HEADER_OK) {
630         return 1;
631     }
632     *width = p->m_Info.image_width;
633     *height = p->m_Info.image_height;
634     *nComps = p->m_Info.num_components;
635     return 0;
636 }
637 FX_BOOL CCodec_JpegModule::StartScanline(void* pContext, int down_scale)
638 {
639     if (m_pExtProvider) {
640         return m_pExtProvider->StartScanline(pContext, down_scale);
641     }
642     FXJPEG_Context* p = (FXJPEG_Context*)pContext;
643     if (setjmp(p->m_JumpMark) == -1) {
644         return FALSE;
645     }
646     p->m_Info.scale_denom = down_scale;
647     return jpeg_start_decompress(&p->m_Info);
648 }
649 FX_BOOL CCodec_JpegModule::ReadScanline(void* pContext, unsigned char* dest_buf)
650 {
651     if (m_pExtProvider) {
652         return m_pExtProvider->ReadScanline(pContext, dest_buf);
653     }
654     FXJPEG_Context* p = (FXJPEG_Context*)pContext;
655     if (setjmp(p->m_JumpMark) == -1) {
656         return FALSE;
657     }
658     int nlines = jpeg_read_scanlines(&p->m_Info, &dest_buf, 1);
659     return nlines == 1;
660 }
661 FX_DWORD CCodec_JpegModule::GetAvailInput(void* pContext, FX_LPBYTE* avail_buf_ptr)
662 {
663     if (m_pExtProvider) {
664         return m_pExtProvider->GetAvailInput(pContext, avail_buf_ptr);
665     }
666     if(avail_buf_ptr != NULL) {
667         *avail_buf_ptr = NULL;
668         if(((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer > 0) {
669             *avail_buf_ptr = (FX_LPBYTE)((FXJPEG_Context*)pContext)->m_SrcMgr.next_input_byte;
670         }
671     }
672     return (FX_DWORD)((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer;
673 }