File indexing completed on 2025-01-19 03:55:55

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-11-22
0007  * Description : some workaround functions to read jpeg
0008  *               files without relying on libjpeg
0009  *
0010  * SPDX-FileCopyrightText: 2008 by Patrick Spendrin <ps ml at gmx dot de>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "jpegwin.h"
0017 
0018 // Qt includes
0019 
0020 #include <QFile>
0021 #include <QByteArray>
0022 
0023 namespace Digikam
0024 {
0025 
0026 namespace JPEGUtils
0027 {
0028 
0029 void init_source (j_decompress_ptr /*cinfo*/)
0030 {
0031 }
0032 
0033 boolean fill_input_buffer (j_decompress_ptr cinfo)
0034 {
0035     digikam_source_mgr* const src  = (digikam_source_mgr*) cinfo->src;
0036 
0037     // Create a fake EOI marker
0038 
0039     src->eoi[0]              = (JOCTET) 0xFF;
0040     src->eoi[1]              = (JOCTET) JPEG_EOI;
0041     src->pub.next_input_byte = src->eoi;
0042     src->pub.bytes_in_buffer = 2;
0043 
0044     return true;
0045 }
0046 
0047 void skip_input_data (j_decompress_ptr cinfo, long nbytes)
0048 {
0049     digikam_source_mgr* const src = (digikam_source_mgr*) cinfo->src;
0050 
0051     if (nbytes > 0)
0052     {
0053         while (nbytes > (long) src->pub.bytes_in_buffer)
0054         {
0055             nbytes -= (long) src->pub.bytes_in_buffer;
0056             (void) fill_input_buffer(cinfo);
0057         }
0058 
0059         src->pub.next_input_byte += (size_t) nbytes;
0060         src->pub.bytes_in_buffer -= (size_t) nbytes;
0061     }
0062 }
0063 
0064 void term_source (j_decompress_ptr /*cinfo*/)
0065 {
0066 }
0067 
0068 void jpeg_memory_src (j_decompress_ptr cinfo, const JOCTET* buffer, size_t bufsize)
0069 {
0070     digikam_source_mgr* src = 0;
0071 
0072     if (cinfo->src == NULL)
0073     {
0074         cinfo->src = (struct jpeg_source_mgr*) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
0075                         JPOOL_PERMANENT,
0076                         sizeof(digikam_source_mgr));
0077     }
0078 
0079     src                        = (digikam_source_mgr*) cinfo->src;
0080     src->pub.init_source       = init_source;
0081     src->pub.fill_input_buffer = fill_input_buffer;
0082     src->pub.skip_input_data   = skip_input_data;
0083     src->pub.resync_to_restart = jpeg_resync_to_restart;    // default
0084     src->pub.term_source       = term_source;
0085     src->pub.next_input_byte   = buffer;
0086     src->pub.bytes_in_buffer   = bufsize;
0087 }
0088 
0089 } // namespace JPEGUtils
0090 
0091 } // namespace Digikam