当前位置:   article > 正文

使用jpeg库将jpeg图像解码为yuv_windows下面jpeg转成yuv422

windows下面jpeg转成yuv422
在做算法的过程中经常终端传来的是jpeg图片,需要将jpeg解码为yuv再进行处理。这里使用jpeg-6b交叉编译,然后进行解码,下面是解码的过程:
  • 1
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <setjmp.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <time.h>


#include <stdlib.h>
#include <string.h>

 #include "jpeglib.h"
 #include <setjmp.h>

 #include "decode.h"


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;

void my_decompress_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);
}
int decode_jpeg_file (char *frame,int frame_size,char *decoded_frame,int *decoded_frame_size,int *decoded_width,int *decoded_height,int color_space)
{
			
	struct jpeg_decompress_struct cinfo;
	struct my_error_mgr jerr;
	FILE *infile, *outfile;
	JSAMPARRAY buffer;      /* Output row buffer */
	int row_stride;     /* physical row width in output buffer */

	int finished = 1;
	int chromaWidth, chromaHeight;
	int yMask,xMask;

	int x,y;
	int width,height;

	unsigned char *pixels=NULL,*rgbPixels=NULL, *src=NULL;
	unsigned char *yPixels=NULL, *uPixels=NULL, *vPixels=NULL;
	unsigned char *yPtr=NULL, *uPtr=NULL, *vPtr=NULL;

	cinfo.err = jpeg_std_error(&jerr.pub);
	jerr.pub.error_exit = my_decompress_error_exit;
	if (setjmp(jerr.setjmp_buffer)) 
	{
		jpeg_destroy_decompress(&cinfo);
		//fclose(infile);
		return -1;
	}
	jpeg_create_decompress(&cinfo);
	//jpeg_stdio_src(&cinfo, infile);
	jpeg_stdio_buffer_src(&cinfo,(unsigned char *)(frame),frame_size);
	(void) jpeg_read_header(&cinfo, TRUE);
	/*set parameters for decompression */
	cinfo.out_color_space = JCS_YCbCr;
	(void) jpeg_start_decompress(&cinfo);
	width  = cinfo.output_width;
	height = cinfo.output_height;
	*decoded_width = width;
	*decoded_height = height;

	pixels = (unsigned char *)malloc(width*height*3);
	if(pixels==NULL)
		printf("Malloc pixels failed!!!!\n");
	memset(pixels, 0, width * height * 3);
	
	src = rgbPixels = pixels;
	/* 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 = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

	printf("cinfo.output_width:%d\n",cinfo.output_width);
	printf("cinfo.output_height:%d\n",cinfo.output_height);
	printf("cinfo.output_components:%d\n",cinfo.output_components);
	while (cinfo.output_scanline < cinfo.output_height)
	{
		int num_rows = jpeg_read_scanlines(&cinfo, buffer, 1);
	//	printf("cinfo.output_scanline:%d\n",cinfo.output_scanline);
	//	printf("num_rows:%d\n",num_rows);
		
	//	printf("\t\tcinfo.output_components:%d\n",cinfo.output_components);
	//	getchar();

		if (num_rows == 0) 
		{
			finished = 0;
			break;
		}
		if (cinfo.output_components == 1) 
		{        // greyscale
			unsigned int i;
			unsigned char *in = (unsigned char *)(*buffer);
			for (i = 0; i < width * num_rows; ++i)
			{
				*pixels++ = *in; // red   b
				*pixels++ = *in; // green  g
				*pixels++ = *in; // blue  r
				++in;
			}
		} 
		else if (cinfo.output_components == 3) 
		{ // RGB
			memcpy(rgbPixels, (*buffer), num_rows * width * 3);
			rgbPixels += num_rows * width * 3;
			//memcpy(pixels, (*buffer), num_rows * width * 3);
			//pixels += num_rows * width * 3;
		}
	}
	rgbPixels = pixels;
	if(finished)
	{
		printf("jpeg_finish_decompress!\n");
		(void) jpeg_finish_decompress(&cinfo);
	}
	printf("\n");
	jpeg_destroy_decompress(&cinfo);
	printf("jpeg_destroy_decompress finished! \n");
	
//	fwrite(yPixels, 1, width * height, outfile);
//	fwrite(uPixels, 1, chromaWidth*chromaHeight, outfile);
//	fwrite(vPixels, 1, chromaWidth*chromaHeight, outfile);

//	fwrite(rgbPixels, 1, width * height*3, outfile);

	

	//if( NULL !=infile)
	//{
	//	fclose(infile);
	//}
	//if(NULL != outfile)
	//{
	//	fclose(outfile);
	//}

	/*
	if(width!=352||height!=288)
	{
		printf("Decoded img:wid=%d,hgt=%d\tError!!!!!!!not 352*288!!!!\n",width,height);
		return -1;
	}
	else
	*/
	if (color_space == DECODE_COLOR_Y||color_space == DECODE_COLOR_GRAY)
	{
		yPixels = (unsigned char*)malloc(height * width);
		if(yPixels==NULL)printf("Malloc yPixels fail!!!\n");

		yPtr = yPixels;
		for(y = 0; y < height; y++)
		{
			for(x = 0; x < width; x++)
			{	
				*yPtr++ = *rgbPixels++;
				rgbPixels += 2;					
			}
		}
		memcpy(decoded_frame, yPixels, height * width );
		*decoded_frame_size = height * width;
	} 
	else if (color_space == DECODE_COLOR_YUV420)
	{
		chromaWidth = width/2;
		chromaHeight = height/2;
		
		printf("color_space DECODE_COLOR_YUV420! \n");
		
		yMask = xMask = 1;

		yPixels = (unsigned char*)malloc(width * height);
		if(yPixels==NULL)printf("Malloc yPixels fail!!!\n");
		uPixels = (unsigned char*)malloc(chromaWidth*chromaHeight);	
		if(uPixels==NULL)printf("Malloc uPixels fail!!!\n");
		vPixels = (unsigned char*)malloc(chromaWidth*chromaHeight);		
		if(vPixels==NULL)printf("Malloc vPixels fail!!!\n");
		
		yPtr = yPixels;
		uPtr = uPixels;
		vPtr = vPixels;
		for(y = 0; y < height; y++)
		{
			for(x = 0; x < width; x++)
			{	
				*yPtr++ = *rgbPixels++;

				if((y & yMask) == 0 && (x & xMask) == 0)
				{
					*uPtr++ = *rgbPixels++;
					*vPtr++ = *rgbPixels++;	
				}
				else
				{
					rgbPixels += 2;
				}				
			}
		}
		memcpy(decoded_frame, yPixels, height * width );
		memcpy(decoded_frame+height * width, uPixels, chromaHeight * chromaWidth );
		memcpy(decoded_frame+height * width+chromaHeight * chromaWidth, vPixels, chromaHeight * chromaWidth );
		*decoded_frame_size = height * width/2*3;
		printf("decoded_frame:%d\n", *decoded_frame_size);
		rgbPixels=NULL;
		
	}
	else if (color_space == DECODE_COLORYUV444)
	{
		chromaWidth = width;
		chromaHeight = height;

		yPixels = (unsigned char*)malloc(height * width);
		if(yPixels==NULL)printf("Malloc yPixels fail!!!\n");
		uPixels = (unsigned char*)malloc(chromaWidth*chromaHeight);	
		if(uPixels==NULL)printf("Malloc uPixels fail!!!\n");
		vPixels = (unsigned char*)malloc(chromaWidth*chromaHeight);		
		if(vPixels==NULL)printf("Malloc vPixels fail!!!\n");

		yPtr = yPixels;
		uPtr = uPixels;
		vPtr = vPixels;
		for(y = 0; y < height; y++)
		{
			for(x = 0; x < width; x++)
			{	
				*yPtr++ = *rgbPixels++;
				*uPtr++ = *rgbPixels++;
				*vPtr++ = *rgbPixels++;					
			}
		}
		memcpy(decoded_frame, yPixels, height * width );
		memcpy(decoded_frame+height * width, uPixels, height * width );
		memcpy(decoded_frame+2*height * width, vPixels, height * width );
		*decoded_frame_size = height * width*3;

	}
	
	if( NULL !=pixels)
	{
		free(pixels);
		pixels =  NULL;
	}

	if( NULL !=yPixels)
	{
		free(yPixels);
		yPixels = NULL;
	}
	if( NULL !=uPixels)
	{
		free(uPixels);
		uPixels = NULL;
	}
	if( NULL !=vPixels)
	{
		free(vPixels);
		vPixels = NULL;
	}
	if( NULL !=rgbPixels)
	{
		rgbPixels = NULL;
	}
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281

需要注意的是,jpeg-6b解码好像只能用gcc编译,用g++会出错,我通常是将其封装成库,用gcc编译,然后在c++中调用。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号