/*
* jpeg_read.c
*
* JPEGOBJ = JPEG_READ(FILENAME)
*
* Returns JPEGOBJ, a Matlab struct containing the JPEG header information,
* quantization tables, and the DCT coefficients.
*
* This software is based in part on the work of the Independent JPEG Group.
* In order to compile, you must first build IJG's JPEG Tools code library,
* available at ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz.
*
* Phil Sallee 6/2003
*
* Copyright (c) 2003 The Regents of the University of California.
* All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for educational, research and non-profit purposes,
* without fee, and without a written agreement is hereby granted,
* provided that the above copyright notice, this paragraph and the
* following three paragraphs appear in all copies.
*
* Permission to incorporate this software into commercial products may
* be obtained by contacting the University of California. Contact Jo Clare
* Peterman, University of California, 428 Mrak Hall, Davis, CA, 95616.
*
* This software program and documentation are copyrighted by The Regents
* of the University of California. The software program and
* documentation are supplied "as is", without any accompanying services
* from The Regents. The Regents does not warrant that the operation of
* the program will be uninterrupted or error-free. The end-user
* understands that the program was developed for research purposes and
* is advised not to rely exclusively on the program for any reason.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
* FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
* INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND
* ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF
* CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
* BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE
* MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <jerror.h>
#include <jpeglib.h>
#include <setjmp.h>
#include "mex.h"
/* We need to create our own error handler so that we can override the
* default handler in case a fatal error occurs. The standard error_exit
* method calls exit() which doesn't clean things up properly and also
* exits Matlab. This is described in the example.c routine provided in
* the IJG's code library.
*/
struct my_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
/* The default output_message routine causes a seg fault in Matlab,
* at least on Windows. Its generally used to emit warnings, since
* fatal errors call the error_exit routine, so we emit a Matlab
* warning instead. If desired, warnings can be turned off by the
* user with "warnings off". -- PAS 10/03
*/
METHODDEF(void)
my_output_message (j_common_ptr cinfo)
{
char buffer[JMSG_LENGTH_MAX];
/* Create the message */
(*cinfo->err->format_message) (cinfo, buffer);
mexWarnMsgTxt(buffer);
}
typedef struct my_error_mgr * my_error_ptr;
METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
char buffer[JMSG_LENGTH_MAX];
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
my_error_ptr myerr = (my_error_ptr) cinfo->err;
/* create the message */
(*cinfo->err->format_message) (cinfo, buffer);
printf("Error: %s\n",buffer);
/* return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1);
}
/* Substitute for mxCreateDoubleScalar, which is not available
* on earlier versions of Matlab */
mxArray *mxCDS(double val) {
mxArray *mxtemp;
double *p;
mxtemp = mxCreateDoubleMatrix(1, 1, mxREAL);
p = mxGetPr(mxtemp);
*p = val;
return mxtemp;
}
/* mex function */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
jpeg_component_info *compptr;
jvirt_barray_ptr *coef_arrays;
jpeg_saved_marker_ptr marker_ptr;
FILE *infile;
JDIMENSION blk_x,blk_y;
JBLOCKARRAY buffer;
JCOEFPTR bufptr;
JQUANT_TBL *quant_ptr;
JHUFF_TBL *huff_ptr;
int strlen, c_width, c_height, ci, i, j, n, dims[2];
char *filename;
double *mp, *mptop;
mxChar *mcp;
mxArray *mxtemp, *mxjpeg_obj, *mxcoef_arrays, *mxcomments;
mxArray *mxquant_tables, *mxhuff_tables, *mxcomp_info;
/* field names jpeg_obj Matlab struct */
const char *jobj_field_names[] = {
"image_width", /* image width in pixels */
"image_height", /* image height in pixels */
"image_components", /* number of image color components */
"image_color_space", /* in/out_color_space */
"jpeg_components", /* number of JPEG color components */
"jpeg_color_space", /* color space of DCT coefficients */
"comments", /* COM markers, if any */
"coef_arrays", /* DCT arrays for each component */
"quant_tables", /* quantization tables */
"ac_huff_tables", /* AC huffman encoding tables */
"dc_huff_tables", /* DC huffman encoding tables */
"optimize_coding", /* flag to optimize huffman tables */
"comp_info", /* component info struct array */
"progressive_mode", /* is progressive mode */
};
const int num_jobj_fields = 14;
/* field names comp_info struct */
const char *comp_field_names[] = {
"component_id", /* JPEG one byte identifier code */
"h_samp_factor", /* horizontal sampling factor */
"v_samp_factor", /* vertical sampling factor */
"quant_tbl_no", /* quantization table number for component */
"dc_tbl_no", /* DC entropy coding table number */
"ac_tbl_no" /* AC entropy encoding table number */
};
const int num_comp_fields = 6;
const char *huff_field_names[] = {"counts","symbols"};
/* check input value */
if (nrhs != 1) mexErrMsgTxt("One input argument required.");
if (mxIsChar(prhs[0]) != 1)
mexErrMsgTxt("Filename must be a string");
/* check output return jpegobj struct */
if (nlhs > 1) mexErrMsgTxt("Too many output arguments");
/* get filename */
strlen = mxGetM(prhs[0])*mxGetN(prhs[0]) + 1;
filename = mxCalloc(strlen, sizeof(char));
mxGetString(prhs[0],filename,strlen);
/* open file */
if ((infile = fopen(filename, "rb")) == NULL)
mexErrMsgTxt("Can't open file");
/* set up the normal JPEG error routines, then override error_exit. */
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
jerr.pub.output_message = my_output_message;
/* establish the setjmp return context for my_error_exit to use. */
if (setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
fclose(infile);
mexErrMsgTxt("Error reading file");
}
/* initialize JPEG decompression object */
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
/* save contents of markers */
jpeg_save_markers(&cinfo, JPEG_COM, 0xFFFF);
/* read header and coefficients */
jpeg_read_header(&cinfo, TRUE);
/* create Matlab jpegobj struct */
mxjpeg_obj = mxCreateStructMatrix(1,1,num_jobj_fields,jobj_field_names);
/* for some reason out_color_components isn't being set by
jpeg_read_header, so we will infer it from out_color_space: */
switch (cinfo.out_color_space) {
case JCS_GRAYSCALE:
cinfo.out_color_components = 1;
break;
case JCS_RGB:
cinfo.out_color_components = 3;
break;
case JCS_YCbCr:
cinfo.out_color_components = 3;
break;
case JCS_CMYK:
cinfo.out_color_components = 4;

总想玩世不恭
- 粉丝: 215
- 资源: 1
最新资源
- 基于PyTorch的MOPSO算法:引导种群逼近Pareto前沿的粒子群优化方法程序研究与应用,基于PyTorch的多目标粒子群算法:MOPSO实现及逼近真实Pareto前沿的种群优化策略,基于pyt
- 车机(飞思卡尔芯片) 系统签名(app公签)
- 如何正确使用deepseek?99%的人都错了.zip
- 基于双边LCC移相控制的无线电能传输系统与PI及MPC模型预测控制实现输出电压恒定,双边LCC移相控制与无线电能传输技术的融合:实现恒定电压PI控制与MPC模型预测控制,双边LCC移相控制,pi控制输
- 零基础使用DeepSeek高效提问技巧.zip
- Multisim仿真工具在模拟电路设计中的首次应用:运算放大器电路构建与测试
- 车机公签,方易通9853 apk签名
- 1000个DeepSeek神级提示词,让你轻松驾驭AI赶紧收藏.zip
- MATLAB代码在线实现:基于最小二乘法的锂电池一阶RC模型参数快速辨识法,基于最小二乘法的锂电池一阶RC模型参数在线辨识MATLAB代码实现,采用最小二乘法在线辨识锂电池一阶RC模型参数的MATLA
- 3个DeepSeek隐藏玩法,99%的人都不知道!.zip
- 横向定标与逆合成孔径雷达ISAR成像的MATLAB仿真程序:精确两步交叉范围缩放法与散射点提取技术研究,**横纵探索:逆合成孔径雷达(ISAR)成像技术与信号处理的精准算法复现**,横向定标 地基逆合
- android安卓原生系统签名,app公签,车机公签
- SPSS workshop (data of construction)
- 全桥与半桥LLC谐振DC-DC变换器的设计与Simulink仿真,包括开环与电压闭环仿真及电路参数计算过程,全桥与半桥LLC谐振DC-DC变换器的设计与Simulink仿真,含开环与电压闭环仿真及电路
- 高速信号链设计中噪声源的影响及优化策略:噪声带宽与信噪比提高方法
- 基于FPGA的永磁同步伺服系统矢量控制设计:集成电流环、速度环与SVPWM模块,采用Verilog实现坐标变换与电机反馈接口,基于FPGA实现永磁同步伺服控制系统的矢量控制与电流环设计:Verilog
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


