#include <stdio.h>
#include <jpeglib.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
unsigned short int *buffer_16bpp;
void convert_for_16(int w, int x, int y, int r, int g, int b){
buffer_16bpp[y*w+x]=((r>>3)<<11)+((g>>2)<<5)+(b>>3);
}
GC gc_background;
XImage *xim;
int width,height;
Display *display;
Window window;
void createPixmap()
{
Pixmap pixmap;
int depth;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
int screen;
unsigned long window_mask;
XSetWindowAttributes window_attributes;
display=XOpenDisplay((char*)0);
depth = DefaultDepth (display,DefaultScreen(display));
screen = DefaultScreen(display);
XVisualInfo vinfo;
window_attributes.colormap=XCreateColormap(display, DefaultRootWindow(display), vinfo.visual, AllocNone);
window_attributes.event_mask = 0; //StructureNotifyMask | ExposureMask;
// window_attributes.background_pixel=0;
window_attributes.border_pixel=0;
window_mask = CWBackPixel; // | CWBorderPixel | CWColormap;// | CWEventMask;
window=XCreateWindow(display,DefaultRootWindow(display), 0, 0, 640,480, 0, depth,InputOutput, 0, window_mask,&window_attributes); //CopyFromParent,window_mask ,&window_attributes);
XMapWindow(display,window);
XFlush(display);
pixmap = XCreatePixmap(display, window, 640, 480, DefaultDepth(display,0));
FILE *infile;
char filename[]="loading.jpg";
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
if((infile = fopen (filename,"rb")) == NULL){
fprintf(stderr,"ERROR opening file oad.jpg\n");
return;
}
jpeg_stdio_src(&cinfo,infile);
jpeg_read_header(&cinfo,FALSE);
cinfo.do_fancy_upsampling = FALSE;
cinfo.do_block_smoothing = FALSE;
jpeg_start_decompress(&cinfo);
width = cinfo.output_width;
height = cinfo.output_height;
// depth = DefaultDepth(MP_Instance.display,MP_Instance.window);
depth=16;
if (depth == 16){
// store_data = &convert_for_16;
buffer_16bpp = (unsigned short int *)malloc((width)*(height)*2);
xim = XCreateImage (display, CopyFromParent,depth, ZPixmap,0,(char *)buffer_16bpp,width,height,16,width*2);
}
int g;
g=0;
int row_stride;
JSAMPARRAY buffer;
row_stride = cinfo.output_width * cinfo.output_components;
buffer=(*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE,row_stride,1);
int bpix;
bpix = cinfo.output_components;
int a;
int i;
while(cinfo.output_scanline < cinfo.output_height){
jpeg_read_scanlines(&cinfo,buffer,1);
a=0;
for(i=0; i< bpix*cinfo.output_width; i+=bpix){
convert_for_16(width,a,g,buffer[0][i],buffer[0][i+1],buffer[0][i+2]);
a++;
}
g++;
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
gc_background = XCreateGC(display,window, 0, NULL);
}
void drawPixmap()
{
XPutImage(display,window,gc_background,xim,0,0,0,0,width,height);
XFlush(display);
}
int main(){
createPixmap();
drawPixmap();
sleep(10);
return(0);
} |