📄 devimage.c
字号:
for(i = 0; i < pimage->height; i++) rows[i] = pimage->imagebits + (i * pimage->pitch); png_set_expand(state); if(bit_depth == 16) png_set_strip_16(state); if(colourtype & PNG_COLOR_MASK_ALPHA) png_set_strip_alpha(state); if(colourtype == PNG_COLOR_TYPE_GRAY || colourtype == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(state); png_read_image(state, rows); png_read_end(state, NULL); free(rows); png_destroy_read_struct(&state, &pnginfo, NULL); return 1;nomem: EPRINTF("LoadPNG: Out of memory\n"); return 2;}#endif /* defined(HAVE_PNG_SUPPORT)*/#if defined(HAVE_BMP_SUPPORT)/* BMP stuff*/#define BI_RGB 0L#define BI_RLE8 1L#define BI_RLE4 2L#define BI_BITFIELDS 3Ltypedef unsigned char BYTE;typedef unsigned short WORD;typedef unsigned long DWORD;typedef long LONG;typedef struct { /* BITMAPFILEHEADER*/ BYTE bfType[2]; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits;} BMPFILEHEAD;#define FILEHEADSIZE 14/* windows style*/typedef struct { /* BITMAPINFOHEADER*/ DWORD BiSize; DWORD BiWidth; DWORD BiHeight; WORD BiPlanes; WORD BiBitCount; DWORD BiCompression; DWORD BiSizeImage; DWORD BiXpelsPerMeter; DWORD BiYpelsPerMeter; DWORD BiClrUsed; DWORD BiClrImportant;} BMPINFOHEAD;#define INFOHEADSIZE 40/* os/2 style*/typedef struct { /* BITMAPCOREHEADER*/ DWORD bcSize; WORD bcWidth; WORD bcHeight; WORD bcPlanes; WORD bcBitCount;} BMPCOREHEAD;#define COREHEADSIZE 12static int DecodeRLE8(MWUCHAR *buf, buffer_t *src);static int DecodeRLE4(MWUCHAR *buf, buffer_t *src);static void put4(int b);/* * BMP decoding routine *//* Changed by JHC to allow a buffer instead of a filename */static intLoadBMP(buffer_t *src, PMWIMAGEHDR pimage){ int h, i, compression; int headsize; MWUCHAR *imagebits; BMPFILEHEAD bmpf; BMPINFOHEAD bmpi; BMPCOREHEAD bmpc; MWUCHAR headbuffer[INFOHEADSIZE]; bseek(src, 0, SEEK_SET); pimage->imagebits = NULL; pimage->palette = NULL; /* read BMP file header*/ if (bread(src, &headbuffer, FILEHEADSIZE) != FILEHEADSIZE) return(0); bmpf.bfType[0] = headbuffer[0]; bmpf.bfType[1] = headbuffer[1]; /* Is it really a bmp file ? */ if (*(WORD*)&bmpf.bfType[0] != wswap(0x4D42)) /* 'BM' */ return 0; /* not bmp image*/ /*bmpf.bfSize = dwswap(dwread(&headbuffer[2]));*/ bmpf.bfOffBits = dwswap(dwread(&headbuffer[10])); /* Read remaining header size */ if (bread(src,&headsize,sizeof(DWORD)) != sizeof(DWORD)) return 0; /* not bmp image*/ headsize = dwswap(headsize); /* might be windows or os/2 header */ if(headsize == COREHEADSIZE) { /* read os/2 header */ if(bread(src, &headbuffer, COREHEADSIZE-sizeof(DWORD)) != COREHEADSIZE-sizeof(DWORD)) return 0; /* not bmp image*/ /* Get data */ bmpc.bcWidth = wswap(*(WORD*)&headbuffer[0]); bmpc.bcHeight = wswap(*(WORD*)&headbuffer[2]); bmpc.bcPlanes = wswap(*(WORD*)&headbuffer[4]); bmpc.bcBitCount = wswap(*(WORD*)&headbuffer[6]); pimage->width = (int)bmpc.bcWidth; pimage->height = (int)bmpc.bcHeight; pimage->bpp = bmpc.bcBitCount; if (pimage->bpp <= 8) pimage->palsize = 1 << pimage->bpp; else pimage->palsize = 0; compression = BI_RGB; } else { /* read windows header */ if(bread(src, &headbuffer, INFOHEADSIZE-sizeof(DWORD)) != INFOHEADSIZE-sizeof(DWORD)) return 0; /* not bmp image*/ /* Get data */ bmpi.BiWidth = dwswap(*(DWORD*)&headbuffer[0]); bmpi.BiHeight = dwswap(*(DWORD*)&headbuffer[4]); bmpi.BiPlanes = wswap(*(WORD*)&headbuffer[8]); bmpi.BiBitCount = wswap(*(WORD*)&headbuffer[10]); bmpi.BiCompression = dwswap(*(DWORD*)&headbuffer[12]); bmpi.BiSizeImage = dwswap(*(DWORD*)&headbuffer[16]); bmpi.BiXpelsPerMeter = dwswap(*(DWORD*)&headbuffer[20]); bmpi.BiYpelsPerMeter = dwswap(*(DWORD*)&headbuffer[24]); bmpi.BiClrUsed = dwswap(*(DWORD*)&headbuffer[28]); bmpi.BiClrImportant = dwswap(*(DWORD*)&headbuffer[32]); pimage->width = (int)bmpi.BiWidth; pimage->height = (int)bmpi.BiHeight; pimage->bpp = bmpi.BiBitCount; pimage->palsize = (int)bmpi.BiClrUsed; if (pimage->palsize > 256) pimage->palsize = 0; else if(pimage->palsize == 0 && pimage->bpp <= 8) pimage->palsize = 1 << pimage->bpp; compression = bmpi.BiCompression; } pimage->compression = MWIMAGE_BGR; /* right side up, BGR order*/ pimage->planes = 1; /* currently only 1, 4, 8 and 24 bpp bitmaps*/ if(pimage->bpp > 8 && pimage->bpp != 24) { EPRINTF("LoadBMP: image bpp not 1, 4, 8 or 24\n"); return 2; /* image loading error*/ } /* compute byte line size and bytes per pixel*/ ComputePitch(pimage->bpp, pimage->width, &pimage->pitch, &pimage->bytesperpixel); /* Allocate image */ if( (pimage->imagebits = malloc(pimage->pitch*pimage->height)) == NULL) goto err; if( (pimage->palette = malloc(256*sizeof(MWPALENTRY))) == NULL) goto err; /* get colormap*/ if(pimage->bpp <= 8) { for(i=0; i<pimage->palsize; i++) { pimage->palette[i].b = bgetc(src); pimage->palette[i].g = bgetc(src); pimage->palette[i].r = bgetc(src); if(headsize != COREHEADSIZE) bgetc(src); } } /* decode image data*/ bseek(src, bmpf.bfOffBits, SEEK_SET); h = pimage->height; /* For every row ... */ while (--h >= 0) { /* turn image rightside up*/ imagebits = pimage->imagebits + h*pimage->pitch; /* Get row data from file */ if(compression == BI_RLE8) { if(!DecodeRLE8(imagebits, src)) break; } else if(compression == BI_RLE4) { if(!DecodeRLE4(imagebits, src)) break; } else { if(bread(src, imagebits, pimage->pitch) != pimage->pitch) goto err; } } return 1; /* bmp image ok*/ err: EPRINTF("LoadBMP: image loading error\n"); if(pimage->imagebits) free(pimage->imagebits); if(pimage->palette) free(pimage->palette); return 2; /* bmp image error*/}/* * Decode one line of RLE8, return 0 when done with all bitmap data */static intDecodeRLE8(MWUCHAR *buf, buffer_t *src){ int c, n; MWUCHAR * p = buf; for( ;;) { switch( n = bgetc(src)) { case EOF: return( 0); case 0: /* 0 = escape*/ switch( n = bgetc(src)) { case 0: /* 0 0 = end of current scan line*/ return( 1); case 1: /* 0 1 = end of data*/ return( 1); case 2: /* 0 2 xx yy delta mode NOT SUPPORTED*/ (void)bgetc(src); (void)bgetc(src); continue; default: /* 0 3..255 xx nn uncompressed data*/ for( c=0; c<n; c++) *p++ = bgetc(src); if( n & 1) (void)bgetc(src); continue; } default: c = bgetc(src); while( n--) *p++ = c; continue; } }}/* * Decode one line of RLE4, return 0 when done with all bitmap data */static MWUCHAR *p;static int once;static voidput4(int b){ static int last; last = (last << 4) | b; if( ++once == 2) { *p++ = last; once = 0; }} static intDecodeRLE4(MWUCHAR *buf, buffer_t *src){ int c, n, c1, c2; p = buf; once = 0; c1 = 0; for( ;;) { switch( n = bgetc(src)) { case EOF: return( 0); case 0: /* 0 = escape*/ switch( n = bgetc(src)) { case 0: /* 0 0 = end of current scan line*/ if( once) put4( 0); return( 1); case 1: /* 0 1 = end of data*/ if( once) put4( 0); return( 1); case 2: /* 0 2 xx yy delta mode NOT SUPPORTED*/ (void)bgetc(src); (void)bgetc(src); continue; default: /* 0 3..255 xx nn uncompressed data*/ c2 = (n+3) & ~3; for( c=0; c<c2; c++) { if( (c & 1) == 0) c1 = bgetc(src); if( c < n) put4( (c1 >> 4) & 0x0f); c1 <<= 4; } continue; } default: c = bgetc(src); c1 = (c >> 4) & 0x0f; c2 = c & 0x0f; for( c=0; c<n; c++) put4( (c&1)? c2: c1); continue; } }}#endif /* defined(HAVE_BMP_SUPPORT)*/#if 0void print_image(PMWIMAGEHDR image){ int i; DPRINTF("Image:\n\n"); DPRINTF("height: %d\n", image->height); DPRINTF("width: %d\n", image->width); DPRINTF("planes: %d\n", image->planes); DPRINTF("bpp: %d\n", image->bpp); DPRINTF("compression: %d\n", image->compression); DPRINTF("palsize: %d\n", image->palsize); for (i=0;i<image->palsize;i++) DPRINTF("palette: %d, %d, %d\n", image->palette[i].r, image->palette[i].g, image->palette[i].b); for(i=0;i<(image->width*image->height);i++) DPRINTF("imagebits: %d\n", image->imagebits[i]);}#endif#if defined(HAVE_GIF_SUPPORT)/* Code for GIF decoding has been adapted from XPaint: *//* +-------------------------------------------------------------------+ *//* | Copyright 1990, 1991, 1993 David Koblas. | *//* | Copyright 1996 Torsten Martinsen. | *//* | Permission to use, copy, modify, and distribute this software | *//* | and its documentation for any purpose and without fee is hereby | *//* | granted, provided that the above copyright notice appear in all | *//* | copies and that both that copyright notice and this permission | *//* | notice appear in supporting documentation. This software is | *//* | provided "as is" without express or implied warranty. | *//* +-------------------------------------------------------------------+ *//* Portions Copyright (C) 1999 Sam Lantinga*//* Adapted for use in SDL by Sam Lantinga -- 7/20/98 *//* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//* GIF stuff*//* * GIF decoding routine */#define MAXCOLORMAPSIZE 256#define MAX_LWZ_BITS 12#define INTERLACE 0x40#define LOCALCOLORMAP 0x80#define CM_RED 0#define CM_GREEN 1#define CM_BLUE 2#define BitSet(byte, bit) (((byte) & (bit)) == (bit))#define ReadOK(src,buffer,len) bread(src, buffer, len)#define LM_to_uint(a,b) (((b)<<8)|(a))struct { unsigned int Width; unsigned int Height; unsigned char ColorMap[3][MAXCOLORMAPSIZE]; unsigned int BitPixel; unsigned int ColorResolution; unsigned int Background; unsigned int AspectRatio; int GrayScale;} GifScreen;static struct { int transparent; int delayTime; int inputFlag; int disposal;} Gif89;static int ReadColorMap(buffer_t *src, int number, unsigned char buffer[3][MAXCOLORMAPSIZE], int *flag);static int DoExtension(buffer_t *src, int label);static int GetDataBlock(buffer_t *src, unsigned char *buf);static int GetCode(buffer_t *src, int code_size, int flag);static int LWZReadByte(buffer_t *src, int flag, int input_code_size);static int ReadImage(buffer_t *src, PMWIMAGEHDR pimage, int len, int height, int, unsigned char cmap[3][MAXCOLORMAPSIZE], int gray, int interlace, int ignore);static intLoadGIF(buffer_t *src, PMWIMAGEHDR pimage){ unsigned char buf[16]; unsigned char c; unsigned char localColorMap[3][MAXCOLORMAPSIZE]; int grayScale; int useGlobalColormap; int bitPixel; int imageCount = 0; char version[4]; int imageNumber = 1; int ok = 0; bseek(src, 0, SEEK_SET); pimage->imagebits = NULL; pimage->palette = NULL; if (!ReadOK(src, buf, 6)) return 0; /* not gif image*/ if (strncmp((char *) buf, "GIF", 3) != 0) return 0; strncpy(version, (char *) buf + 3, 3); version[3] = '\0'; if (strcmp(version, "87a") != 0 && strcmp(version, "89a") != 0) { EPRINTF("LoadGIF: GIF version number not 87a or 89a\n"); return 2; /* image loading error*/ } Gif89.transparent = -1; Gif89.delayTime = -1; Gif89.inputFlag = -1; Gif89.disposal = 0; if (!ReadOK(src, buf, 7)) { EPRINTF("LoadGIF: bad screen descriptor\n"); return 2; /* image loading error*/ } GifScreen.Width = LM_to_uint(buf[0], buf[1]); GifScreen.Height = LM_to_uint(buf[2], buf[3]); GifScreen.BitPixel = 2 << (buf[4] & 0x07); GifScreen.ColorResolution = (((buf[4] & 0x70) >> 3) + 1); GifScreen.Background = buf[5]; GifScreen.AspectRatio = buf[6]; if (BitSet(buf[4], LOCALCOLORMAP)) { /* Global Colormap */ if (ReadColorMap(src, GifScreen.BitPixel, GifScreen.ColorMap, &GifScreen.GrayScale)) { EPRINTF("LoadGIF: bad global colormap\n"); return 2; /* image loading error*/ } } do { if (!ReadOK(src, &c, 1)) { EPRINTF("LoadGIF: EOF on image data\n"); goto done; } if (c == ';') { /* GIF terminator */ if (imageCount < imageNumber) { EPRINTF("LoadGIF: no image %d of %d\n", imageNumber,imageCount); goto done; } } if (c == '!') { /* Extension */ if (!ReadOK(src, &c, 1)) { EPRINTF("LoadGIF: EOF on extension function code\n"); goto done; } DoExtension(src, c); continue; } if (c != ',') { /* Not a valid start character */ continue; } ++imageCount; if (!ReadOK(src, buf, 9)) { EPRINTF("LoadGIF: bad image size\n"); goto done; } useGlobalColormap = !BitSet(buf[8], LOCALCOLORMAP); bitPixel = 1 << ((buf[8] & 0x07) + 1); if (!useGlobalColormap) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -