⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decompress.cpp

📁 用于机器人自动低分辨路的地图测绘程序。用于机器人控制测绘。分为远端控制端和本地控制端。控制电机为标准舵机。
💻 CPP
字号:
/*
    Robot Interface Remote Client
    (C) 2006 Jason Hunt
    nulluser@gmail.com

    FILE: decompress.cpp
    
    You have to wrap jpeglib.h in extern "C" { } 
    
    Code based on libjpeg
*/

#include <stdio.h>
#include <jpeglib.h>
#include <jconfig.h>
#include <jerror.h>

#pragma hdrstop

#include "display.h"
#include "decompress.h"

// Simple source manager
typedef struct {
    struct jpeg_source_mgr pub;	

    unsigned int data_size;
    JOCTET * buffer;
    char *source_buffer;

} my_source_mgr;

typedef my_source_mgr * my_src_ptr;

/* Not needed */
METHODDEF(void) camera_init_source (j_decompress_ptr cinfo) {}
METHODDEF(void) camera_term_source (j_decompress_ptr cinfo) {}

/* Setup for data fill */
METHODDEF(boolean) camera_fill_input_buffer (j_decompress_ptr cinfo)
{
    my_src_ptr src = (my_src_ptr) cinfo->src;

    src->pub.next_input_byte = (JOCTET *) src->source_buffer;
    src->pub.bytes_in_buffer = src->data_size;

    return TRUE;
}
/* End of camera_fill_input_buffer */


/* Skip data in buffer some data in the buffer */
METHODDEF(void) camera_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
    my_src_ptr src = (my_src_ptr) cinfo->src;
    src->pub.next_input_byte += num_bytes;
    src->pub.bytes_in_buffer -= num_bytes;
}
/* End of camera_skip_input_data  */



/* Init a buffer as a source */
GLOBAL(void) camera_jpeg_src (j_decompress_ptr cinfo, char *data, unsigned int size)
{
    my_src_ptr src;

    if (cinfo->src == NULL) 
    {
        cinfo->src = (struct jpeg_source_mgr *)
                   (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_source_mgr));

        src = (my_src_ptr) cinfo->src;
        src->buffer = NULL;
    }

    src->source_buffer = data;
    src->data_size = size;
    
    src = (my_src_ptr) cinfo->src;
    src->pub.init_source = camera_init_source;
    src->pub.fill_input_buffer = camera_fill_input_buffer;
    src->pub.skip_input_data = camera_skip_input_data;
    src->pub.resync_to_restart = jpeg_resync_to_restart;
    src->pub.term_source = camera_term_source;
    src->pub.bytes_in_buffer = 0;
    src->pub.next_input_byte = NULL;
}
/* End of camera_jpeg_src */


/* Decomepress the image in [compressed_image] */
void decompress_image(char *compressed_image, unsigned int compressed_size, 
                      color_type *out_buff, unsigned int out_x, unsigned int out_y)
{
    struct jpeg_decompress_struct cinfo;

    jpeg_error_mgr jerr;
    JSAMPARRAY buffer;	

    cinfo.err = jpeg_std_error(&jerr);
  
    jpeg_create_decompress(&cinfo);
    camera_jpeg_src(&cinfo, compressed_image, compressed_size);
    jpeg_read_header(&cinfo, TRUE);
    jpeg_start_decompress(&cinfo);
    
    int row_stride = cinfo.output_width * cinfo.output_components;
    
    buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

    if (cinfo.output_height > out_y) return;

    while (cinfo.output_scanline < cinfo.output_height) 
    {
        jpeg_read_scanlines(&cinfo, buffer, 1);

        // Decode the scan_line

        unsigned int index = (cinfo.output_scanline-1)* out_x;
        unsigned int bi = 0;

        if (cinfo.output_components == 1) // Grayscale decode
        {
            for (unsigned int i = 0; i < out_x; i++)
            {
                unsigned char color = buffer[0][bi++];
                out_buff[index].r = color;
                out_buff[index].g = color;
                out_buff[index++].b = color;
            }   
        } else
        {  // Color decode
            for (unsigned int i = 0; i < out_x; i++)
            {
                out_buff[index].b =   buffer[0][bi++];
                out_buff[index].g =   buffer[0][bi++];
                out_buff[index++].r = buffer[0][bi++];     
            }
        }

     }

    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
}
/* End of decompress_image */
 



  





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -