2022-03-09 10:37:11 +01:00

85 lines
2.8 KiB
C

/**
******************************************************************************
* @file LibJPEG/LibJPEG_Encoding/Src/decode.c
* @author MCD Application Team
* @brief This file contain the decompress method.
******************************************************************************
* @attention
*
* Copyright (c) 2017 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "decode.h"
/* Private typedef -----------------------------------------------------------*/
/* This struct contains the JPEG decompression parameters */
struct jpeg_decompress_struct cinfo;
/* This struct represents a JPEG error handler */
struct jpeg_error_mgr jerr;
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Jpeg Decode
* @param callback: line decoding callback
* @param file1: pointer to the jpg file
* @param width: image width
* @param buff: pointer to the image line
* @retval None
*/
void jpeg_decode(JFILE *file, uint32_t width, uint8_t * buff, uint8_t (*callback)(uint8_t*, uint32_t))
{
/* Decode JPEG Image */
JSAMPROW buffer[2] = {0}; /* Output row buffer */
uint32_t row_stride = 0; /* physical row width in image buffer */
buffer[0] = buff;
/* Step 1: allocate and initialize JPEG decompression object */
cinfo.err = jpeg_std_error(&jerr);
/* Initialize the JPEG decompression object */
jpeg_create_decompress(&cinfo);
jpeg_stdio_src (&cinfo, file);
/* Step 3: read image parameters with jpeg_read_header() */
jpeg_read_header(&cinfo, TRUE);
/* Step 4: set parameters for decompression */
cinfo.dct_method = JDCT_FLOAT;
/* Step 5: start decompressor */
jpeg_start_decompress(&cinfo);
row_stride = width * 3;
while (cinfo.output_scanline < cinfo.output_height)
{
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
if (callback(buffer[0], row_stride) != 0)
{
break;
}
}
/* Step 6: Finish decompression */
jpeg_finish_decompress(&cinfo);
/* Step 7: Release JPEG decompression object */
jpeg_destroy_decompress(&cinfo);
}