📄 devimage.c
字号:
if (ReadColorMap(src, bitPixel, localColorMap, &grayScale)) { EPRINTF("LoadGIF: bad local colormap\n"); goto done; } ok = ReadImage(src, pimage, LM_to_uint(buf[4], buf[5]), LM_to_uint(buf[6], buf[7]), bitPixel, localColorMap, grayScale, BitSet(buf[8], INTERLACE), imageCount != imageNumber); } else { ok = ReadImage(src, pimage, LM_to_uint(buf[4], buf[5]), LM_to_uint(buf[6], buf[7]), GifScreen.BitPixel, GifScreen.ColorMap, GifScreen.GrayScale, BitSet(buf[8], INTERLACE), imageCount != imageNumber); } } while (ok == 0); /* set transparent color, if any*/ pimage->transcolor = Gif89.transparent; if (ok) return 1; /* image load ok*/done: if (pimage->imagebits) free(pimage->imagebits); if (pimage->palette) free(pimage->palette); return 2; /* image load error*/}static intReadColorMap(buffer_t *src, int number, unsigned char buffer[3][MAXCOLORMAPSIZE], int *gray){ int i; unsigned char rgb[3]; int flag; flag = TRUE; for (i = 0; i < number; ++i) { if (!ReadOK(src, rgb, sizeof(rgb))) return 1; buffer[CM_RED][i] = rgb[0]; buffer[CM_GREEN][i] = rgb[1]; buffer[CM_BLUE][i] = rgb[2]; flag &= (rgb[0] == rgb[1] && rgb[1] == rgb[2]); }#if 0 if (flag) *gray = (number == 2) ? PBM_TYPE : PGM_TYPE; else *gray = PPM_TYPE;#else *gray = 0;#endif return FALSE;}static intDoExtension(buffer_t *src, int label){ static unsigned char buf[256]; switch (label) { case 0x01: /* Plain Text Extension */ break; case 0xff: /* Application Extension */ break; case 0xfe: /* Comment Extension */ while (GetDataBlock(src, (unsigned char *) buf) != 0); return FALSE; case 0xf9: /* Graphic Control Extension */ GetDataBlock(src, (unsigned char *) buf); Gif89.disposal = (buf[0] >> 2) & 0x7; Gif89.inputFlag = (buf[0] >> 1) & 0x1; Gif89.delayTime = LM_to_uint(buf[1], buf[2]); if ((buf[0] & 0x1) != 0) Gif89.transparent = buf[3]; while (GetDataBlock(src, (unsigned char *) buf) != 0); return FALSE; default: break; } while (GetDataBlock(src, (unsigned char *) buf) != 0); return FALSE;}static int ZeroDataBlock = FALSE;static intGetDataBlock(buffer_t *src, unsigned char *buf){ unsigned char count; if (!ReadOK(src, &count, 1)) return -1; ZeroDataBlock = count == 0; if ((count != 0) && (!ReadOK(src, buf, count))) return -1; return count;}static intGetCode(buffer_t *src, int code_size, int flag){ static unsigned char buf[280]; static int curbit, lastbit, done, last_byte; int i, j, ret; unsigned char count; if (flag) { curbit = 0; lastbit = 0; done = FALSE; return 0; } if ((curbit + code_size) >= lastbit) { if (done) { if (curbit >= lastbit) EPRINTF("LoadGIF: bad decode\n"); return -1; } buf[0] = buf[last_byte - 2]; buf[1] = buf[last_byte - 1]; if ((count = GetDataBlock(src, &buf[2])) == 0) done = TRUE; last_byte = 2 + count; curbit = (curbit - lastbit) + 16; lastbit = (2 + count) * 8; } ret = 0; for (i = curbit, j = 0; j < code_size; ++i, ++j) ret |= ((buf[i / 8] & (1 << (i % 8))) != 0) << j; curbit += code_size; return ret;}static intLWZReadByte(buffer_t *src, int flag, int input_code_size){ int code, incode; register int i; static int fresh = FALSE; static int code_size, set_code_size; static int max_code, max_code_size; static int firstcode, oldcode; static int clear_code, end_code; static int table[2][(1 << MAX_LWZ_BITS)]; static int stack[(1 << (MAX_LWZ_BITS)) * 2], *sp; if (flag) { set_code_size = input_code_size; code_size = set_code_size + 1; clear_code = 1 << set_code_size; end_code = clear_code + 1; max_code_size = 2 * clear_code; max_code = clear_code + 2; GetCode(src, 0, TRUE); fresh = TRUE; for (i = 0; i < clear_code; ++i) { table[0][i] = 0; table[1][i] = i; } for (; i < (1 << MAX_LWZ_BITS); ++i) table[0][i] = table[1][0] = 0; sp = stack; return 0; } else if (fresh) { fresh = FALSE; do { firstcode = oldcode = GetCode(src, code_size, FALSE); } while (firstcode == clear_code); return firstcode; } if (sp > stack) return *--sp; while ((code = GetCode(src, code_size, FALSE)) >= 0) { if (code == clear_code) { for (i = 0; i < clear_code; ++i) { table[0][i] = 0; table[1][i] = i; } for (; i < (1 << MAX_LWZ_BITS); ++i) table[0][i] = table[1][i] = 0; code_size = set_code_size + 1; max_code_size = 2 * clear_code; max_code = clear_code + 2; sp = stack; firstcode = oldcode = GetCode(src, code_size, FALSE); return firstcode; } else if (code == end_code) { int count; unsigned char buf[260]; if (ZeroDataBlock) return -2; while ((count = GetDataBlock(src, buf)) > 0); if (count != 0) { /* * EPRINTF("missing EOD in data stream (common occurence)"); */ } return -2; } incode = code; if (code >= max_code) { *sp++ = firstcode; code = oldcode; } while (code >= clear_code) { *sp++ = table[1][code]; if (code == table[0][code]) EPRINTF("LoadGIF: circular table entry\n"); code = table[0][code]; } *sp++ = firstcode = table[1][code]; if ((code = max_code) < (1 << MAX_LWZ_BITS)) { table[0][code] = oldcode; table[1][code] = firstcode; ++max_code; if ((max_code >= max_code_size) && (max_code_size < (1 << MAX_LWZ_BITS))) { max_code_size *= 2; ++code_size; } } oldcode = incode; if (sp > stack) return *--sp; } return code;}static intReadImage(buffer_t* src, PMWIMAGEHDR pimage, int len, int height, int cmapSize, unsigned char cmap[3][MAXCOLORMAPSIZE], int gray, int interlace, int ignore){ unsigned char c; int i, v; int xpos = 0, ypos = 0, pass = 0; /* * Initialize the compression routines */ if (!ReadOK(src, &c, 1)) { EPRINTF("LoadGIF: EOF on image data\n"); return 0; } if (LWZReadByte(src, TRUE, c) < 0) { EPRINTF("LoadGIF: error reading image\n"); return 0; } /* * If this is an "uninteresting picture" ignore it. */ if (ignore) { while (LWZReadByte(src, FALSE, c) >= 0); return 0; } /*image = ImageNewCmap(len, height, cmapSize);*/ pimage->width = len; pimage->height = height; pimage->planes = 1; pimage->bpp = 8; ComputePitch(8, len, &pimage->pitch, &pimage->bytesperpixel); pimage->compression = 0; pimage->palsize = cmapSize; pimage->palette = malloc(256*sizeof(MWPALENTRY)); pimage->imagebits = malloc(height*pimage->pitch); if(!pimage->imagebits || !pimage->palette) return 0; for (i = 0; i < cmapSize; i++) { /*ImageSetCmap(image, i, cmap[CM_RED][i], cmap[CM_GREEN][i], cmap[CM_BLUE][i]);*/ pimage->palette[i].r = cmap[CM_RED][i]; pimage->palette[i].g = cmap[CM_GREEN][i]; pimage->palette[i].b = cmap[CM_BLUE][i]; } while ((v = LWZReadByte(src, FALSE, c)) >= 0) { pimage->imagebits[ypos * pimage->pitch + xpos] = v; ++xpos; if (xpos == len) { xpos = 0; if (interlace) { switch (pass) { case 0: case 1: ypos += 8; break; case 2: ypos += 4; break; case 3: ypos += 2; break; } if (ypos >= height) { ++pass; switch (pass) { case 1: ypos = 4; break; case 2: ypos = 2; break; case 3: ypos = 1; break; default: goto fini; } } } else { ++ypos; } } if (ypos >= height) break; }fini: return 1;}#endif /* defined(HAVE_GIF_SUPPORT)*/#if defined(HAVE_PNM_SUPPORT)enum { PNM_TYPE_NOTPNM, PNM_TYPE_PBM, PNM_TYPE_PGM, PNM_TYPE_PPM};static int LoadPNM(buffer_t *src, PMWIMAGEHDR pimage){ char buf[256], *p; int type = PNM_TYPE_NOTPNM, binary = 0, gothdrs = 0, scale = 0; int ch, x = 0, y = 0, i, n, mask, col1, col2, col3; bseek(src, 0L, SEEK_SET); if(!bgets(src,buf, 4)) return 0; if(!strcmp("P1\n", buf)) type = PNM_TYPE_PBM; else if(!strcmp("P2\n", buf)) type = PNM_TYPE_PGM; else if(!strcmp("P3\n", buf)) type = PNM_TYPE_PPM; else if(!strcmp("P4\n", buf)) { type = PNM_TYPE_PBM; binary = 1; } else if(!strcmp("P5\n", buf)) { type = PNM_TYPE_PGM; binary = 1; } else if(!strcmp("P6\n", buf)) { type = PNM_TYPE_PPM; binary = 1; } if(type == PNM_TYPE_NOTPNM) return 0; n = 0; while((p = bgets(src, buf, 256))) { if(*buf == '#') continue; if(type == PNM_TYPE_PBM) { if(sscanf(buf, "%i %i", &pimage->width, &pimage->height) == 2) { pimage->bpp = 1; gothdrs = 1; if(!(pimage->palette = malloc( sizeof(MWPALENTRY) * 2))) { EPRINTF("Out of memory\n"); return 2; } pimage->palsize = 2; pimage->palette[0].r = 0xff; pimage->palette[0].g = 0xff; pimage->palette[0].b = 0xff; pimage->palette[1].r = 0; pimage->palette[1].g = 0; pimage->palette[1].b = 0; } break; } if((type == PNM_TYPE_PGM) || (type == PNM_TYPE_PPM)) { if(!n++) { if(sscanf(buf, "%i %i", &pimage->width, &pimage->height) != 2) break; } else { if(sscanf(buf, "%i", &i) != 1) break; pimage->bpp = 24; if(i > 255) { EPRINTF("LoadPNM: PPM files must be " "24bpp\n"); return 2; } for(scale = 7, n = 2; scale; scale--, n *= 2) if(i < n) break; gothdrs = 1; break; } } } if(!gothdrs) { EPRINTF("LoadPNM: bad image headers\n"); if(pimage->palette) free(pimage->palette); return 2; } pimage->planes = 1; ComputePitch(pimage->bpp, pimage->width, &pimage->pitch, &pimage->bytesperpixel); pimage->compression = MWIMAGE_RGB; if(!(pimage->imagebits = malloc(pimage->pitch * pimage->height))) { EPRINTF("LoadPNM: couldn't allocate memory for image\n"); if(pimage->palette) free(pimage->palette); return 2; } p = pimage->imagebits; if(type == PNM_TYPE_PBM) { if(binary) { x = 0; y = 0; while((ch = bgetc(src)) != EOF) { for(i = 0; i < 8; i++) { mask = 0x80 >> i; if(ch & mask) *p |= mask; else *p &= ~mask; if(++x == pimage->width) { if(++y == pimage->height) return 1; p = pimage->imagebits - 1 + (y * pimage->pitch); x = 0; break; } } p++; } } else { n = 0; while((ch = bgetc(src)) != EOF) { if(isspace(ch)) continue; mask = 0x80 >> n; if(ch == '1') *p |= mask; else if(ch == '0') *p &= ~mask; else goto baddata; if(++n == 8) { n = 0; p++; } if(++x == pimage->width) { if(++y == pimage->height) return 1; p = pimage->imagebits + (y * pimage->pitch); n = 0; x = 0; } } } } else { while(1) { if(type == PNM_TYPE_PGM) { if(binary) { if((ch = bgetc(src)) == EOF) goto baddata; } else { /*if(fscanf(fp, "%i", &ch) != 1)*/ goto baddata; } *p++ = ch << scale; *p++ = ch << scale; *p++ = ch << scale; } else { if(binary) { if(((col1 = bgetc(src)) == EOF) || ((col2 = bgetc(src)) == EOF) || ((col3 = bgetc(src)) == EOF)) goto baddata; } else { /*if(fscanf(fp, "%i %i %i", &col1, &col2, &col3) != 3)*/ goto baddata; } *p++ = col1 << scale; *p++ = col2 << scale; *p++ = col3 << scale; } if(++x == pimage->width) { if(++y == pimage->height) return 1; p = pimage->imagebits + (y * pimage->pitch); x = 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -