📄 bitmap_def.c.svn-base
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/time.h>#include <sys/types.h>#include <sys/stat.h>#include <assert.h>#include <setjmp.h>#include <jpeglib.h>#include <zlib.h>#include <string.h>#include "base_types.h"#include "define.h"#include "bitmap_def.h"#include "config.h"#if WITH_DMALLOC#include <dmalloc.h>#endif//#define PRINT_DGstatic struct jpeg_decompress_struct jpegObject;static struct jpeg_source_mgr jpegSourceManager;//static unsigned char *inputData2;static unsigned char *inputData;static int haveTables = 0;struct 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;struct my_error_mgr jpegErrorMgr;METHODDEF(void) my_error_exit (j_common_ptr cInfo) { my_error_ptr myerr = (my_error_ptr) cInfo->err; (*cInfo->err->output_message) (cInfo); longjmp(myerr->setjmp_buffer, 1);}//// Methods for Source data managerstatic void initSource(struct jpeg_decompress_struct *cInfo) { cInfo->src->bytes_in_buffer = 0;}static boolean fillInputBuffer(struct jpeg_decompress_struct *cInfo) { cInfo->src->next_input_byte = inputData; cInfo->src->bytes_in_buffer = 1; inputData++; return 1;}static void skipInputData(struct jpeg_decompress_struct *cInfo, long count) { cInfo->src->bytes_in_buffer = 0; inputData += count;}static boolean resyncToRestart(struct jpeg_decompress_struct *cInfo, int desired) { return jpeg_resync_to_restart(cInfo, desired);}static void termSource(struct jpeg_decompress_struct *cInfo) {}/////////////////////static int buildJpegAlpha(struct DefineBitsJPEG *b, unsigned char *buffer) { z_stream stream; int status; unsigned char *data;#ifdef PRINT_DG printf("flash: loading jpeg (buildJpegAlpha)\n");#endif data = (unsigned char*)malloc(b->Width*b->Height); assert(data != NULL) ; stream.next_in = buffer; stream.avail_in = 1; stream.next_out = data; stream.avail_out = b->Width*b->Height; stream.zalloc = Z_NULL; stream.zfree = Z_NULL; status = inflateInit(&stream); while (1) { status = inflate(&stream, Z_SYNC_FLUSH) ; if (status == Z_STREAM_END) { break; } if (status != Z_OK) { printf("Zlib data error : %s\n", stream.msg); free(data); assert(0); } stream.avail_in = 1; } inflateEnd(&stream); b->Alpha_bufP = data;#ifdef PRINT_DG printf("flash: loading jpeg (buildJpegAlpha) finished !\n");#endif return 0;}int buildFromJpegInterchangeData (int read_alpha, long offset,struct DefineBitsJPEG* outStruct) { struct jpeg_decompress_struct cInfo; struct jpeg_source_mgr mySrcMgr; struct my_error_mgr jerr; JSAMPROW buffer[1]; /* Output row buffer */ int row_stride; /* physical row width in output buffer */ unsigned char *ptrPix; long n; long nbColors; int status; // struct DefineBitMap *outStruct;#ifdef PRINT_DG printf("flash: loading jpeg (Interchange)\n");#endif // Kludge to correct some corrupted files if (outStruct->JPEGData[1] == 0xd9 && outStruct->JPEGData[3] == 0xd8) { outStruct->JPEGData[3] = 0xd9; outStruct->JPEGData[1] = 0xd8; } 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)) { jpeg_destroy_decompress(&cInfo); if (outStruct->PixelsP) { free(outStruct->PixelsP); outStruct->PixelsP = NULL; } assert(0); } // Set current stream pointer to stream inputData = outStruct->JPEGData; jpeg_create_decompress(&cInfo); // Setup source manager structure mySrcMgr.init_source = initSource; mySrcMgr.fill_input_buffer = fillInputBuffer; mySrcMgr.skip_input_data = skipInputData; mySrcMgr.resync_to_restart = resyncToRestart; mySrcMgr.term_source = termSource; mySrcMgr.next_input_byte = NULL; /* until buffer loaded */ // Set default source manager cInfo.src = &mySrcMgr; status = jpeg_read_header(&cInfo, FALSE); assert(status==JPEG_HEADER_OK||status==JPEG_HEADER_TABLES_ONLY);#ifdef PRINT_DG printf("flash: loading jpeg (Interchange) jpeg_read_header(&cInfo, FALSE) :%d \n",status);#endif status = jpeg_read_header(&cInfo, TRUE); assert(status==JPEG_HEADER_OK||status==JPEG_HEADER_TABLES_ONLY);#ifdef PRINT_DG printf("flash: loading jpeg (Interchange) jpeg_read_header(&cInfo, TRUE) :%d \n",status);#endif cInfo.quantize_colors = TRUE; // Create colormapped image status = jpeg_start_decompress(&cInfo); assert(status==TRUE);#ifdef PRINT_DG printf("flash: loading jpeg (Interchange) jpeg_start_decompress :%d \n",status);#endif // Set objet dimensions outStruct->Height = cInfo.output_height; outStruct->Width = cInfo.output_width; outStruct->Bpl = outStruct->Width; outStruct->PixelsP = (unsigned char *)malloc(outStruct->Height*outStruct->Width);#ifdef PRINT_DG printf("flash: loading jpeg (Interchange) outStruct->PixelsP beginPos :%d \n",outStruct->PixelsP); printf("flash: loading jpeg (Interchange) outStruct->Height :%d \n",outStruct->Height); printf("flash: loading jpeg (Interchange) outStruct->Width :%d \n",outStruct->Width); printf("flash: loading jpeg (Interchange) outStruct->output_components :%d \n",cInfo.output_components); printf("flash: loading jpeg (Interchange) length :%d \n",outStruct->Height*outStruct->Width); printf("flash: loading jpeg (Interchange) outStruct->PixelsP endPos :%d \n",outStruct->PixelsP + (outStruct->Height*outStruct->Width));#endif assert(outStruct->PixelsP != NULL) ; ptrPix = outStruct->PixelsP; /* JSAMPLEs per row in output buffer */ row_stride = cInfo.output_width * cInfo.output_components; /* Make a one-row-high sample array that will go away when done with image */ buffer[0] = (JSAMPROW)malloc(row_stride); /* jpeg_read_scanlines(...); */ while (cInfo.output_scanline < cInfo.output_height) { status = jpeg_read_scanlines(&cInfo, buffer, 1); assert(status==1); memcpy(ptrPix,buffer[0],row_stride); ptrPix+= row_stride; }#ifdef PRINT_DG printf("flash: loading jpeg (Interchange) ptrPix endPos :%d \n",ptrPix); printf("flash: loading jpeg (Interchange) remain space :%d \n",outStruct->PixelsP+(outStruct->Height*outStruct->Width*2)-ptrPix);#endif free(&(buffer[0][0])); outStruct->ColormapP = (struct RGBA*)malloc(cInfo.actual_number_of_colors*sizeof(struct RGBA)); assert(outStruct->ColormapP != NULL) ; nbColors = cInfo.actual_number_of_colors; outStruct->Colors = nbColors; for(n=0; n < nbColors; n++) { outStruct->ColormapP[n].Red = cInfo.colormap[0][n]; outStruct->ColormapP[n].Green = cInfo.colormap[1][n]; outStruct->ColormapP[n].Blue = cInfo.colormap[2][n]; } status = jpeg_finish_decompress(&cInfo); assert(status==TRUE); jpeg_destroy_decompress(&cInfo); if (read_alpha) { if (buildJpegAlpha(outStruct, outStruct->JPEGData + offset) < 0) { assert(0); } }#ifdef PRINT_DG printf("flash: loading jpeg (Interchange) finished!\n");#endif return 0;}// Read JPEG image using pre-loaded Tablesint buildFromJpegAbbreviatedData(unsigned char *stream,struct DefineBits* outStruct) { // struct jpeg_decompress_struct jpegObject; // struct jpeg_source_mgr mySrcMgr; // struct my_error_mgr jerr; JSAMPROW buffer[1]; unsigned char *ptrPix; int stride; long n; long nbColors; int status;#ifdef PRINT_DG printf("flash: loading jpeg (abbreviated)\n");#endif // Set current stream pointer to stream inputData = stream; if (setjmp(jpegErrorMgr.setjmp_buffer)) { // JPEG data Error // jpeg_destroy_decompress(&jpegObject); if (outStruct->PixelsP) { free(outStruct->PixelsP); outStruct->PixelsP = NULL; } assert(0); } status = jpeg_read_header(&jpegObject, TRUE); assert(status==JPEG_HEADER_OK||status==JPEG_HEADER_TABLES_ONLY);#ifdef PRINT_DG printf("flash: loading jpeg (abbreviated) jpeg_read_header(&jpegObject, TRUE) :%d \n",status);#endif jpegObject.quantize_colors = TRUE; // Create colormapped image status = jpeg_start_decompress(&jpegObject); assert(status==TRUE);#ifdef PRINT_DG printf("flash: loading jpeg (abbreviated) jpeg_start_decompress :%d \n",status);#endif#ifdef PRINT_DG printf("flash: loading jpeg (abbreviated) cInfo.desired_number_of_colors :%d \n",jpegObject.desired_number_of_colors); printf("flash: loading jpeg (abbreviated) cInfo.num_components :%d \n",jpegObject.num_components); printf("flash: loading jpeg (abbreviated) cInfo.jpeg_color_space :%d \n",jpegObject.jpeg_color_space); printf("flash: loading jpeg (abbreviated) cInfo.out_color_space :%d \n",jpegObject.out_color_space); printf("flash: loading jpeg (abbreviated) cInfo.out_color_components :%d \n",jpegObject.out_color_components); printf("flash: loading jpeg (abbreviated) cInfo.output_components :%d \n",jpegObject.output_components); printf("flash: loading jpeg (abbreviated) cInfo.actual_number_of_colors :%d \n",jpegObject.actual_number_of_colors); printf("flash: loading jpeg (abbreviated) cInfo.data_precision :%d \n",jpegObject.data_precision);#endif // Set objet dimensions outStruct->Height = jpegObject.output_height; outStruct->Width = jpegObject.output_width; outStruct->Bpl = outStruct->Width; outStruct->PixelsP = (unsigned char *)malloc(outStruct->Height*outStruct->Width); assert(outStruct->PixelsP != NULL) ; ptrPix = outStruct->PixelsP; stride = jpegObject.output_width * jpegObject.output_components; buffer[0] = (JSAMPROW)malloc(stride); while (jpegObject.output_scanline < jpegObject.output_height) { status = jpeg_read_scanlines(&jpegObject, buffer, 1); assert(status==1); memcpy(ptrPix,buffer[0],stride); ptrPix+= stride; } free(&(buffer[0][0])); outStruct->ColormapP = (struct RGBA*)malloc(jpegObject.actual_number_of_colors*sizeof(struct RGBA)); assert(outStruct->ColormapP != NULL) ; nbColors = jpegObject.actual_number_of_colors; outStruct->Colors = nbColors; for(n=0; n < nbColors; n++) { outStruct->ColormapP[n].Red = jpegObject.colormap[0][n]; outStruct->ColormapP[n].Green = jpegObject.colormap[1][n]; outStruct->ColormapP[n].Blue = jpegObject.colormap[2][n]; } status = jpeg_finish_decompress(&jpegObject); assert(status==TRUE);#ifdef PRINT_DG printf("flash: loading jpeg (abbreviated) jpeg_finish_decompress :%d \n",status); printf("flash: loading jpeg (abbreviated) finished!\n");#endif return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -