📄 byjpeg.cpp
字号:
#include <stdio.h>#include <memory.h>#include "byjpeg.h"#include <setjmp.h>#ifdef __cplusplusextern "C" {#endif#include "jpeglib.h"#ifdef __cplusplus}#endifstruct my_error_mgr { struct jpeg_error_mgr pub; /* "public" fields */ jmp_buf setjmp_buffer; /* for return to caller */};typedef struct my_error_mgr* my_error_ptr;static void my_error_exit(j_common_ptr cinfo){ /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */ my_error_ptr myerr = (my_error_ptr) cinfo->err; /* Always display the message. */ /* We could postpone this until after returning, if we chose. */ (*cinfo->err->output_message) (cinfo); /* Return control to the setjmp point */ longjmp(myerr->setjmp_buffer, 1);}void JpegInitDestination(j_compress_ptr cinfo){}static boolean JpegEmptyOutputBuffer(j_compress_ptr cinfo){ return TRUE;}static void JpegTermDestination(j_compress_ptr cinfo){ // jpegDstDataLen = jpegDstBufferLen - jpegDstManager.free_in_buffer;}// Raw RGB Data converted to Jpeg databool rgb_to_jpeg(int w, int h, const char* rgb_data, int rgb_size, char* jpeg_data, int* jpeg_size, int quality){ struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; struct jpeg_destination_mgr jpegDstManager; int ret; unsigned char *srcBuf = new unsigned char[w * 3]; JSAMPROW rowPointer[1]; int left_size; memset(&cinfo, 0, sizeof(cinfo)); memset(&jerr, 0, sizeof(jerr)); memset(&jpegDstManager, 0, sizeof(jpegDstManager)); rowPointer[0] = (JSAMPROW)srcBuf; left_size = *jpeg_size; cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); cinfo.image_width = w; cinfo.image_height = h; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; cinfo.raw_data_in = true; jpeg_set_defaults(&cinfo); cinfo.dest = &jpegDstManager; jpegDstManager.next_output_byte = (unsigned char*)jpeg_data; jpegDstManager.free_in_buffer = left_size; jpegDstManager.init_destination = JpegInitDestination; jpegDstManager.empty_output_buffer = JpegEmptyOutputBuffer; jpegDstManager.term_destination = JpegTermDestination; // 0 - 100 if (quality > 0) jpeg_set_quality(&cinfo, quality, TRUE); jpeg_start_compress(&cinfo, TRUE); for (int y = h - 1; y >= 0; y--) { rowPointer[0] = (unsigned char*)(rgb_data + y * w * 3); ret = jpeg_write_scanlines(&cinfo, rowPointer, 1); } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); *jpeg_size = left_size - jpegDstManager.free_in_buffer; delete[] srcBuf; return *jpeg_size < left_size;}void JpegInitSource(j_decompress_ptr cinfo){}boolean JpegFillInputBuffer(j_decompress_ptr cinfo){ /* jpegError = true; jpegSrcManager.bytes_in_buffer = jpegBufferLen; jpegSrcManager.next_input_byte = (JOCTET *)jpegBufferPtr; */ return TRUE;}void JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes){ /* if (num_bytes < 0 || (size_t)num_bytes > jpegSrcManager.bytes_in_buffer) { jpegError = true; jpegSrcManager.bytes_in_buffer = jpegBufferLen; jpegSrcManager.next_input_byte = (JOCTET *)jpegBufferPtr; } else { jpegSrcManager.next_input_byte += (size_t) num_bytes; jpegSrcManager.bytes_in_buffer -= (size_t) num_bytes; } */}void JpegTermSource(j_decompress_ptr cinfo){ /* No work necessary here. */}bool jpeg_to_rgb(const char* jpeg_data, int jpeg_size, char* rgb_data, int rgb_size, int w, int h){ struct jpeg_decompress_struct cinfo; struct jpeg_source_mgr jpegSrcManager; int ret; JSAMPROW rowPointer[1]; // struct jpeg_error_mgr jerr; // cinfo.err = jpeg_std_error(&jerr); // struct my_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = my_error_exit; /* Establish the setjmp return context for my_error_exit to use. */ if (setjmp(jerr.setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. We * need to clean up the JPEG object, close the input file, and * return. */ jpeg_destroy_decompress(&cinfo); printf("jpeg_to_rgb failed, jmp\n"); return false; } jpeg_create_decompress(&cinfo); jpegSrcManager.init_source = JpegInitSource; jpegSrcManager.fill_input_buffer = JpegFillInputBuffer; jpegSrcManager.skip_input_data = JpegSkipInputData; jpegSrcManager.resync_to_restart = jpeg_resync_to_restart; jpegSrcManager.term_source = JpegTermSource; jpegSrcManager.next_input_byte = (unsigned char*)jpeg_data; jpegSrcManager.bytes_in_buffer = jpeg_size; cinfo.src = &jpegSrcManager; jpeg_read_header(&cinfo, TRUE); cinfo.out_color_space = JCS_RGB; jpeg_start_decompress(&cinfo); if (cinfo.output_width != (unsigned int)w && cinfo.output_height != (unsigned int)h) { jpeg_destroy_decompress(&cinfo); return false; } for (int dy = h - 1; cinfo.output_scanline < cinfo.output_height && dy >= 0; dy--) { rowPointer[0] = (unsigned char *)(rgb_data + w * dy * 3); ret = jpeg_read_scanlines(&cinfo, rowPointer, 1); } jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); return true;}/*void put_image_jpeg(char *filename, char *image, int width, int height, int quality, int dct_method){ int y, line_width; JSAMPROW row_ptr[1]; struct jpeg_compress_struct cjpeg; struct jpeg_error_mgr jerr; char *line; FILE *fp; time_t t; struct tm *tm; int len; char* full_name = filename; line = new char[width * 3]; if (!line) return; if ((fp = fopen(full_name, "wb")) == NULL) { fprintf(stderr, "can't open %s for binary write", full_name); perror(" and the reason is"); return; } // fprintf(stderr, " writing image to %s\n", full_name); cjpeg.err = jpeg_std_error(&jerr); // errors get written to stderr jpeg_create_compress (&cjpeg); cjpeg.image_width = width; cjpeg.image_height= height; cjpeg.input_components = 3; cjpeg.in_color_space = JCS_RGB; jpeg_set_defaults (&cjpeg); jpeg_set_quality (&cjpeg, quality, TRUE); // cjpeg.dct_method = dct_method; // jpeg_stdio_dest (&cjpeg, stdout); jpeg_stdio_dest (&cjpeg, fp); // data written to file jpeg_start_compress(&cjpeg, TRUE); row_ptr[0] = (JSAMPROW)line; line_width = width * 3; for ( y = 0; y < height; y++) { memcpy(line,image,line_width); jpeg_write_scanlines(&cjpeg, row_ptr, 1); image += line_width; } jpeg_finish_compress (&cjpeg); jpeg_destroy_compress (&cjpeg); delete[] line; if (fp) { fflush(fp); fclose(fp); }}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -