comparison TOOLS/mwallp/jpeg.c @ 6457:95918d5066b6

mwallp - simple wallpaper setting tool using MPlayer codebase
author arpi
date Mon, 17 Jun 2002 14:13:10 +0000
parents
children 4930b82dbf02
comparison
equal deleted inserted replaced
6456:f0b64210ce97 6457:95918d5066b6
1 // based on vd_ijpg.c by Pontscho
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <jpeglib.h>
7 #define UINT16 IJPG_UINT16
8 #define INT16 IJPG_INT16
9
10 #include <setjmp.h>
11
12 #include <inttypes.h>
13
14 #include "img_format.h"
15
16 #include "swscale.h"
17 #include "rgb2rgb.h"
18
19 static SwsContext *swsContext=NULL;
20
21 typedef struct
22 {
23 struct jpeg_source_mgr pub;
24 unsigned char * inbuf;
25 int bufsize;
26 } my_source_mgr;
27
28 typedef my_source_mgr * my_src_ptr;
29
30 METHODDEF(void) init_source (j_decompress_ptr cinfo)
31 {
32 }
33
34 METHODDEF(boolean) fill_input_buffer (j_decompress_ptr cinfo)
35 {
36 my_src_ptr src = (my_src_ptr) cinfo->src;
37 size_t nbytes;
38 src->pub.next_input_byte = src->inbuf;
39 src->pub.bytes_in_buffer = src->bufsize;
40 return TRUE;
41 }
42
43 METHODDEF(void) skip_input_data (j_decompress_ptr cinfo, long num_bytes)
44 {
45 my_src_ptr src = (my_src_ptr) cinfo->src;
46
47 if (num_bytes > 0)
48 {
49 while (num_bytes > (long) src->pub.bytes_in_buffer)
50 {
51 num_bytes -= (long) src->pub.bytes_in_buffer;
52 (void) fill_input_buffer(cinfo);
53 }
54 src->pub.next_input_byte += (size_t) num_bytes;
55 src->pub.bytes_in_buffer -= (size_t) num_bytes;
56 }
57 }
58
59 METHODDEF(void) term_source (j_decompress_ptr cinfo) { }
60
61 GLOBAL(void) jpeg_buf_src ( j_decompress_ptr cinfo, char * inbuf,int bufsize )
62 {
63 my_src_ptr src;
64 if (cinfo->src == NULL) cinfo->src=malloc( sizeof( my_source_mgr ) );
65 src = (my_src_ptr) cinfo->src;
66 src->pub.init_source = init_source;
67 src->pub.fill_input_buffer = fill_input_buffer;
68 src->pub.skip_input_data = skip_input_data;
69 src->pub.resync_to_restart = jpeg_resync_to_restart;
70 src->pub.term_source = term_source;
71 src->inbuf = inbuf;
72 src->bufsize=bufsize;
73 src->pub.bytes_in_buffer = 0;
74 src->pub.next_input_byte = NULL;
75 }
76
77 struct my_error_mgr
78 {
79 struct jpeg_error_mgr pub;
80 jmp_buf setjmp_buffer;
81 };
82
83 typedef struct my_error_mgr * my_error_ptr;
84
85 METHODDEF(void) my_error_exit (j_common_ptr cinfo)
86 {
87 my_error_ptr myerr=(my_error_ptr) cinfo->err;
88 (*cinfo->err->output_message) (cinfo);
89 longjmp(myerr->setjmp_buffer, 1);
90 }
91
92 static struct jpeg_decompress_struct cinfo;
93 static struct my_error_mgr jerr;
94
95 // decode a frame
96 int decode_jpeg(void* data,int len,char* dbuffer,int dwidth,int dheight, int dstride,int dbpp){
97 int width,height,depth,i;
98 int row_stride;
99 unsigned char* img=NULL;
100 int in_fmt=0;
101
102 if ( len <= 0 ) return 0; // skipped frame
103
104 cinfo.err=jpeg_std_error( &jerr.pub );
105 jerr.pub.error_exit=my_error_exit;
106 if( setjmp( jerr.setjmp_buffer ) )
107 {
108 return 0;
109 }
110
111 jpeg_create_decompress( &cinfo );
112 jpeg_buf_src( &cinfo,data,len );
113 jpeg_read_header( &cinfo,TRUE );
114 width=cinfo.image_width;
115 height=cinfo.image_height;
116 jpeg_start_decompress( &cinfo );
117 depth=cinfo.output_components * 8;
118
119 switch( depth ) {
120 case 8: in_fmt=IMGFMT_Y8;break;
121 case 24: in_fmt=IMGFMT_RGB|24;break;
122 default: return 0;
123 }
124
125 row_stride=cinfo.output_width * cinfo.output_components;
126 img=malloc(row_stride * (height+1));
127
128 for ( i=0;i < height;i++ ){
129 // char* row=dbuffer+i*dstride;
130 char* row=img+row_stride*i;
131 jpeg_read_scanlines( &cinfo,(JSAMPLE**)&row,1 );
132 }
133
134 jpeg_finish_decompress(&cinfo);
135 jpeg_destroy_decompress(&cinfo);
136
137 swsContext= getSwsContextFromCmdLine(width,height, in_fmt,
138 dwidth,dheight, IMGFMT_BGR|dbpp);
139
140 swsContext->swScale(swsContext,&img,&row_stride,0,height,&dbuffer, &dstride);
141
142 freeSwsContext(swsContext);
143
144 free(img);
145
146 return 1;
147 }