使用TJpgDec进行JPEG图像解码

#include "tjpgd.h"

#include <string.h>

JDEC tjpeg_dev;
unsigned char jpg_buffer[4096];
unsigned char rgb_buffer[60000];

uint16_t image_index;
uint16_t image_size;

unsigned int jpeg_in_func(JDEC *jd, unsigned char *buf, unsigned int num)
{
 unsigned int rb;
 uint8_t *dev = jd->device;
 
 if(buf)
 {

  if(image_index + num <= image_size)
   rb = num;
  else
   rb = image_size - image_index;

  memcpy(buf, dev + image_index, rb);
  image_index += rb;
  return rb;
 }
 else
 {
  if(image_index + num <= image_size)
   rb = num;
  else
   rb = image_size - image_index;

  image_index += rb;

  return num;
 }
}

unsigned int jpeg_out_func(JDEC *jd, void *rgbbuf, JRECT *rect)
{
 unsigned char *src, *dst;
 unsigned int y, bws, bwd; 

 src = (unsigned char *)rgbbuf;
 dst = rgb_buffer + 3 * (rect->top * jd->width + rect->left);

 bws = 3 * (rect->right - rect->left + 1);
 bwd = 3 * jd->width;

 for (y = rect->top; y <= rect->bottom; y++)
 {
  memcpy(dst, src, bws);
  src += bws;
  dst += bwd;
 }

 return 0;
}

void jpg_decode(uint8_t *image, uint16_t count)
{
 image_size = count;
 image_index = 0;
 jd_prepare(&tjpeg_dev, jpeg_in_func, jpg_buffer, 4096, image);
 jd_decomp(&tjpeg_dev, jpeg_out_func, 0);
}

猜你喜欢

转载自blog.csdn.net/lushoumin/article/details/82661303