static int py_JpegObject_init(py_JpegObject *self, PyObject *args, PyObject *kwds) { if (args == NULL) { return -1; } const char *filename; if (!PyArg_ParseTuple(args, "s", &filename)) { return -1; } FILE *file = fopen(filename, "rb"); if (file == NULL) { PyErr_SetString(PyExc_IOError, "no such file!"); return -1; } int err = FALSE; if (0 != readjpeg(file, &self->stuff)) { PyErr_SetString(PyExc_IOError, "file loading failed!"); err = TRUE; } else { self->inited = TRUE; } if (0 != fclose(file)) { if (PyErr_Occurred() == NULL) { PyErr_SetFromErrno(PyExc_IOError); } err = TRUE; } if (err) { if (self->inited) { closejpeg(&self->stuff); } return -1; } return 0; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l) { HDC hdc;// = GetDC(hwnd); PAINTSTRUCT ps; RECT rect; BITMAPINFO bmi; ZeroMemory(&bmi, sizeof(BITMAPINFO)); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 24; bmi.bmiHeader.biCompression = BI_RGB; switch (msg) { case WM_DESTROY: PostQuitMessage(1); break; case WM_PAINT: { hdc=BeginPaint(hwnd,&ps); //GetWindowRect(hwnd,&rect); //RedrawWindow(hwnd,&rect,0,0); if(num==vs.length-1)exit(0); readjpeg(vs.strs[num++], &img); bmi.bmiHeader.biWidth = img.Width; bmi.bmiHeader.biHeight = img.Height; //SetDIBitsToDevice(hdc, 0, 0, img.Width, img.Height,0, 0, 0, img.Height, img.Data, &bmi, DIB_RGB_COLORS); SetStretchBltMode(hdc,STRETCH_HALFTONE);//注意第二个参数会影响显示质量 StretchDIBits(hdc, 0, 0, img.Width/2, img.Height/2,0, 0,img.Width,img.Height, img.Data, &bmi, DIB_RGB_COLORS,SRCCOPY); free(img.Data); EndPaint(hwnd,&ps); } break; default: return DefWindowProc(hwnd, msg, w, l); } return 0; }
unsigned char *readpicture(char *filename, int *width, int *height, int printflag){ char *ext; unsigned char *returncode; char *filebuffer=NULL; int allocated; STRUCTSTAT statbuffer; if(filename==NULL)return NULL; if(STAT(filename,&statbuffer)==0){ filebuffer=filename; allocated=0; } else{ size_t lenbuffer; if(texturedir==NULL){ if(printflag==1){ fprintf(stderr,"*** Error: texture file: %s unavailable\n",filename); } return NULL; } else{ FILE *stream; lenbuffer=strlen(filename)+strlen(texturedir)+1; NewMemory((void **)&filebuffer,(unsigned int)(lenbuffer+1)); allocated=1; strcpy(filebuffer,texturedir); strcat(filebuffer,dirseparator); strcat(filebuffer,filename); stream=fopen(filebuffer,"rb"); if(stream==NULL){ if(printflag==1){ fprintf(stderr,"*** Error: texture file: %s unavailable\n",filebuffer); } FREEMEMORY(filebuffer); return NULL; } else{ fclose(stream); } } } if(printflag==1)PRINTF("Loading texture:%s ",filebuffer); ext = filebuffer + strlen(filebuffer) - 4; if(strncmp(ext,".jpg",4)==0||strncmp(ext,".JPG",4)==0){ returncode = readjpeg(filebuffer,width,height,pixel_skip); } else if(strncmp(ext,".png",4)==0||strncmp(ext,".PNG",4)==0){ returncode = readpng(filebuffer,width,height); } else{ if(allocated==1){ FREEMEMORY(filebuffer); } return NULL; } if(allocated==1){ FREEMEMORY(filebuffer); } if(printflag==1){ if(returncode!=NULL){ PRINTF(" - completed\n"); } else{ PRINTF(" - failed\n"); fprintf(stderr,"*** Error: attempt to input %s failed\n",filename); } } return returncode; }
int readimage(rawimage * img) { int rc; int xres, yres; unsigned char * imgdata; char * name = img->name; if (strstr(name, ".ppm")) { rc = readppm(name, &xres, &yres, &imgdata); } else if (strstr(name, ".tga")) { rc = readtga(name, &xres, &yres, &imgdata); } else if (strstr(name, ".jpg")) { rc = readjpeg(name, &xres, &yres, &imgdata); } else if (strstr(name, ".gif")) { rc = IMAGEUNSUP; } else if (strstr(name, ".png")) { rc = IMAGEUNSUP; } else if (strstr(name, ".tiff")) { rc = IMAGEUNSUP; } else if (strstr(name, ".rgb")) { rc = IMAGEUNSUP; } else if (strstr(name, ".xpm")) { rc = IMAGEUNSUP; } else { rc = readppm(name, &xres, &yres, &imgdata); } switch (rc) { case IMAGEREADERR: fprintf(stderr, "Short read encountered while loading image %s\n", name); rc = IMAGENOERR; /* remap to non-fatal error */ break; case IMAGEUNSUP: fprintf(stderr, "Cannot read unsupported image format for image %s\n", name); break; } /* If the image load failed, create a tiny white colored image to fake it */ /* this allows a scene to render even when a file can't be loaded */ if (rc != IMAGENOERR) { rc = fakeimage(name, &xres, &yres, &imgdata); } /* If we succeeded in loading the image, return it. */ if (rc == IMAGENOERR) { img->xres = xres; img->yres = yres; img->bpp = 3; img->data = imgdata; } return rc; }