📄 jpeg.c
字号:
/*Copyright George Peter Staplin 2003*//*You may use/copy/modify/distribute this software for any purpose *provided that I am given credit and you don't sue me for any bugs. *//*Please contact me using GeorgePS@XMission.com if you like this, or *have questions. */ #include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <jpeglib.h>#include <jerror.h>#include <time.h>#ifndef u_char#define u_char unsigned char#endif void jpeg_error_exit (j_common_ptr cinfo) { cinfo->err->output_message (cinfo); exit (EXIT_FAILURE);}/*This returns an array for a 24 bit image.*/u_char *decode_jpeg (char *filename, short *widthPtr, short *heightPtr){ register JSAMPARRAY lineBuf; struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr err_mgr; int bytesPerPix; FILE *inFile; u_char *retBuf; inFile = fopen (filename, "rb"); if (NULL == inFile) { printf ("Open file error %s\n",filename); return NULL; } cinfo.err = jpeg_std_error (&err_mgr); err_mgr.error_exit = jpeg_error_exit; jpeg_create_decompress (&cinfo); jpeg_stdio_src (&cinfo, inFile); jpeg_read_header (&cinfo, 1); cinfo.do_fancy_upsampling = 0; cinfo.do_block_smoothing = 0; jpeg_start_decompress (&cinfo); *widthPtr = cinfo.output_width; *heightPtr = cinfo.output_height; bytesPerPix = cinfo.output_components; lineBuf = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE, (*widthPtr * bytesPerPix), 1); retBuf = (u_char *) malloc (3 * (*widthPtr * *heightPtr)); if (NULL == retBuf) { perror (NULL); return NULL; } if (3 == bytesPerPix) { int x; int y; int i; for (y = 0; y < cinfo.output_height; ++y) { jpeg_read_scanlines (&cinfo, lineBuf, 1); memcpy ((retBuf + y * *widthPtr * 3),lineBuf[0],3 * *widthPtr); } } else if (1 == bytesPerPix) { unsigned int col; int lineOffset = (*widthPtr * 3); int lineBufIndex; int x ; int y; for (y = 0; y < cinfo.output_height; ++y) { jpeg_read_scanlines (&cinfo, lineBuf, 1); lineBufIndex = 0; for (x = 0; x < lineOffset; ++x) { col = lineBuf[0][lineBufIndex]; retBuf[(lineOffset * y) + x] = col; ++x; retBuf[(lineOffset * y) + x] = col; ++x; retBuf[(lineOffset * y) + x] = col; ++lineBufIndex; } } } else { fprintf (stderr, "Error: the number of color channels is %d. This program only handles 1 or 3\n", bytesPerPix); return NULL; } jpeg_finish_decompress (&cinfo); jpeg_destroy_decompress (&cinfo); fclose (inFile); return retBuf;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -