📄 bmp.c
字号:
fileheader->bfSize= ReadInt(f+2); fileheader->bfReserved1=ReadShortInt(f+6) ; fileheader->bfReserved2= ReadShortInt(f+8); fileheader->bfOffBits= ReadInt(f+10); if (fileheader->bfType != 19778) return -1; return 0;}static void image_4bit_save (DilloBmp *bmp,guchar *f, guint Bufsize){ int j; guchar pixel,temp0,temp1; guchar *line; guchar *index; index=f+14+bmp->infoheader.biSize; do { line = bmp->Data+bmp->infoheader.biWidth*bmp->linerec*3; for (j = 0; j < bmp->infoheader.biWidth/2; j++) { pixel = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+j); temp0=pixel&0x0f; temp1=(pixel>>4)&0x0f; line [2] = index[temp0*4+red]; line [1] = index[temp0*4+1]; line [0] = index[temp0*4+green]; line += 3; line [2] = index[temp0*4+red]; line [1] = index[temp0*4+1]; line [0] = index[temp0*4+green]; line += 3; } if(bmp->infoheader.biWidth&0x01) { pixel = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+(++j)); temp0=pixel&0x0f; line [2] = index[temp0*4+red]; line [1] = index[temp0*4+1]; line [0] = index[temp0*4+green]; line += 3; } if((bmp->bytesdelt+j)>Bufsize-bmp->fileheader.bfOffBits) return; bmp->bytesdelt+=bmp->pitch; bmp->linerec++; } while(bmp->linerec < bmp->infoheader.biHeight-1);}/* read_8bit_image: * For reading the 8-bit BMP image format. * This only support bit masks specific to Windows 95. */static void image_8bit_save (DilloBmp *bmp,guchar *f, guint Bufsize){ int j; guchar pixel; guchar *line; guchar *index; index=f+14+bmp->infoheader.biSize; do { line = bmp->Data+bmp->infoheader.biWidth*bmp->linerec*3; for (j = 0; j < bmp->infoheader.biWidth; j++) { pixel = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+j); line [2] = index[pixel*4+red]; line [1] = index[pixel*4+1]; line [0] = index[pixel*4+green]; line += 3; if((bmp->bytesdelt+j)>Bufsize-bmp->fileheader.bfOffBits) return; } bmp->bytesdelt+=bmp->pitch; bmp->linerec++; } while(bmp->linerec < bmp->infoheader.biHeight-1);}/* read_16bit_image: * For reading the 16-bit BMP image format. * This only support bit masks specific to Windows 95. */static void image_16bit_save (DilloBmp *bmp,guchar *f,guint Bufsize){ int j; WORD pixel; guchar *line; do { line = bmp->Data+bmp->infoheader.biWidth*bmp->linerec*3; for (j = 0; j < bmp->infoheader.biWidth; j++) { pixel = ReadShortInt(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+2*j); if (bmp->gmask == 0x03e0) /* 5-5-5 */ { line [red] = ((pixel >> 10) & 0x1f) << 3; line [1] = ((pixel >> 5) & 0x1f) << 3; line [green] = (pixel & 0x1f) << 3; } else /* 5-6-5 */ { line [red] = ((pixel >> 11) & 0x1f) << 3; line [1] = ((pixel >> 5) & 0x3f) << 2; line [green] = (pixel & 0x1f) << 3; } line += 3; if((bmp->bytesdelt+2*j)>Bufsize-bmp->fileheader.bfOffBits) return; }#if 0 if (bmp->infoheader.biWidth & 0x01) bmp->bytesdelt=bmp->infoheader.biWidth*2+2; else bmp->bytesdelt=bmp->infoheader.biWidth*2;#endif bmp->bytesdelt+=bmp->pitch; bmp->linerec++; } while(bmp->linerec < bmp->infoheader.biHeight-1);}/* read_24bit_image: * For reading the 24-bit BMP image format. * This only support bit masks specific to Windows 95. */static void image_24bit_save (DilloBmp *bmp,guchar *f,guint Bufsize){ guchar *line,*orig; guint i; do { line = bmp->Data+bmp->infoheader.biWidth*bmp->linerec*3; orig=f+bmp->fileheader.bfOffBits+bmp->bytesdelt; for(i=0;i<bmp->infoheader.biWidth;i++) { line [2] = orig[red]; line [1] = orig[1]; line [0] = orig[green]; line+=3; orig+=3; } // memcpy(line,orig,bmp->infoheader.biWidth*3); if((bmp->bytesdelt+bmp->infoheader.biWidth*3)>Bufsize-bmp->fileheader.bfOffBits) return; bmp->bytesdelt+=bmp->pitch; bmp->linerec++; } while(bmp->linerec < bmp->infoheader.biHeight-1);}/* read_RLE8_compressed_image: * For reading the 8 bit RLE compressed BMP image format. */#if 0static void RLE8_compressed_image_save(DilloBmp *bmp,guchar *f ){ unsigned char count, val, val0; int j,i, pos; int eolflag, eopicflag; guchar *index; guchar *lines; eopicflag = 0; bmp->line = bmp->infoheader.biHeight - 1; i=0; index=f+14+bmp->infoheader.biSize; while (eopicflag == 0) { pos = 0; /* x position in bitmap */ eolflag = 0; /* end of line flag */ lines = bmp->Data+bmp->pitch*bmp->linerec; while ((eolflag == 0) && (eopicflag == 0)) { count = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+i); i++; val = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+i); i++; if (count > 0) { /* repeat pixel count times */ for (j=0;j<count;j++) { lines[0] = index[val*4]; lines[1] = index[val*4+1]; lines[2] = index[val*4+2]; lines+=3; pos++; } } else { switch (val) { case 0: /* end of line flag */ bmp->linerec+=1; eolflag=1; bmp->bytesdelt+=i; i=0; break; case 1: /* end of picture flag */ eopicflag=1; bmp->linerec+=1; break; case 2: /* displace picture */ count = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+i); i++; val = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+i); i++; pos += count;//? bmp->line -= val;//? break; default: /* read in absolute mode */ for (j=0; j<val; j++) { val0 = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+i); i++; lines[0] = index[val*4]; lines[1] = index[val*4+1]; lines[2] = index[val*4+2]; lines+=3; pos++; } if (j%2 == 1) i++; break; } } if (pos-1 > (int)bmp->infoheader.biWidth) eolflag=1; } if((bmp->bytesdelt+i)>bmp->bytessaved) return; // bits += pitch; bmp->line--; if (bmp->line < 0) eopicflag = 1; }}/* read_RLE4_compressed_image: * For reading the 4 bit RLE compressed BMP image format. */static void read_RLE4_compressed_image (DilloBmp *bmp,guchar *f ){ unsigned char b[8]; unsigned char count; unsigned short val0, val; int j, k, pos, line; int eolflag, eopicflag; guchar *index; guchar *lines; eopicflag = 0; /* end of picture flag */ bmp->line =bmp->infoheader->biHeight - 1; i=0; index=f+14+bmp->infoheader.biSize; lines = bmp->Data+bmp->pitch*bmp->linerec; while (eopicflag == 0) { pos = 0; eolflag = 0; /* end of line flag */ while ((eolflag == 0) && (eopicflag == 0)) { count = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+i); i++; val =ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+i); i++; if (count > 0) { /* repeat pixels count times */ b[1] = val & 15; b[0] = (val >> 4) & 15; for (j=0; j<count; j++) { if (pos % 2 == 0) { lines[0] = index[b[j%2]*4]; lines[1] = index[b[j%2]+1]; lines[2] = index[b[j%2]+2]; lines+=3; } else { lines[0] = index[val*4]; lines[1] = index[val*4+1]; lines[2] = index[val*4+2]; lines+=3; } pos++; } } else { switch (val) { case 0: /* end of line */ bmp->linerec+=1; eolflag=1; bmp->bytesdelt+=i; i=0; break; case 1: /* end of picture */ eopicflag=1; bmp->linerec+=1; break; case 2: /* displace image */ count = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+i); i++; val = ReadGuchar(f+bmp->fileheader.bfOffBits+bmp->bytesdelt+i); i++; pos += count; bmp->line -= val; break; default: /* read in absolute mode */ for (j=0; j<val; j++) { if ((j%4) == 0) { val0 = fp_igetw(f); for (k=0; k<2; k++) { b[2*k+1] = val0 & 15; val0 = val0 >> 4; b[2*k] = val0 & 15; val0 = val0 >> 4; } } if (pos % 2 == 0) bits [pos/2] = b[j%4] << 4; else bits [pos/2] = bits [pos/2] | b[j%4]; pos++; } break; } } if (pos-1 > (int)infoheader->biWidth) eolflag=1; } bits += pitch; line--; if (line < 0) eopicflag = 1; }}#endif#endif /* ENABLE_BMP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -