p2png.cpp
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:5k
- #include "p2png.h"
- #include <string>
- int interlace = 0;
- png_color_16 color_values;
- static BOOL read_bmp(char *, IMAGE *);
- static const char *read_rgb_bits(IMAGE *, FILE *);
- static unsigned long mgetdwl(void *);
- static unsigned int mgetwl(void *);
- static BOOL write_png(char *, IMAGE *);
- void start_toPNG(char *NameBmp, char *NamePng)
- {
- std::string namepng;
- namepng=NameBmp;
- namepng +=".png";
- IMAGE image;
- read_bmp(NameBmp, &image);
- write_png((char *)namepng.c_str(), &image);
- }
- static BOOL read_bmp(char *fn, IMAGE *img)
- {
- BYTE bfh[138];
- BYTE *const bih = bfh + 14;
- BYTE rgbq[4];
- DWORD offbits, bihsize, skip;
- UINT palette_size, true_pixdepth;
- PALETTE *pal;
- FILE *fp;
- int i;
- imgbuf_init(img);
- if (fn == NULL) goto error_abort; else fp = fopen(fn, "rb");
- fread(bfh,18,1, fp);
- offbits = mgetdwl(bfh + 10);
- bihsize = mgetdwl(bfh + 14);
- skip = offbits - bihsize - 14;
- // if (bihsize < 12 || bihsize > 124 ||
- // offbits < (bihsize + 14)) goto error_abort;
- if (fread((bih + 4), (bihsize - 4), 1, fp) != 1)
- goto error_abort;
- img->width = mgetdwl(bih + 4);
- img->height = mgetdwl(bih + 8);
- img->pixdepth = mgetwl(bih + 14);
- img->topdown = FALSE;
- img->alpha = FALSE;
- img->palnum = 0;
- if (!imgbuf_alloc(img)) goto error_abort;
- for (pal = img->palette, i = img->palnum; i > 0; pal++, i--) {
- if (fread(rgbq, 4, 1, fp) != 1)
- goto error_abort;
- pal->red = rgbq[2];
- pal->green = rgbq[1];
- pal->blue = rgbq[0];
- }
- for ( ; skip > 0; skip--) {
- if (fgetc(fp) == EOF)
- goto error_abort;
- }
- img->sigbit.red = img->sigbit.green = img->sigbit.blue = 8;
- img->sigbit.gray = img->sigbit.alpha = 8;
- read_rgb_bits(img, fp);
-
- if (fp != stdin) fclose(fp);
- return TRUE;
- error_abort: /* error */
- if (fp != stdin && fp != NULL) fclose(fp);
- imgbuf_free(img);
- return FALSE;
- }
- static const char *read_rgb_bits(IMAGE *img, FILE *fp)
- {
- DWORD rd = 16*1024*1024;
- DWORD num = img->imgbytes;
- BYTE *ptr = img->bmpbits;
- while (num > 0) {
- if (rd > num) rd = num;
- if (fread(ptr, rd, 1, fp) != 1)
- return "ERROR";
- ptr += rd; num -= rd;
- }
- return NULL;
- }
- static unsigned long mgetdwl(void *ptr)
- {
- unsigned char *p = (unsigned char *)ptr;
- return ((unsigned long)p[0] ) + ((unsigned long)p[1] << 8) +
- ((unsigned long)p[2] << 16) + ((unsigned long)p[3] << 24);
- }
- static unsigned int mgetwl(void *ptr)
- {
- unsigned char *p = (unsigned char *)ptr;
- return ((unsigned int)p[0]) + ((unsigned int)p[1] << 8);
- }
- static BOOL write_png(char *fn, IMAGE *img)
- {
- png_structp png_ptr;
- png_infop info_ptr;
- int bit_depth;
- int color_type;
- int interlace_type;
- png_byte trans[256];
- unsigned i;
- FILE *fp;
- if (fn == NULL) goto error_abort; else fp = fopen(fn, "wb");
- png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, fn,
- png_my_error, png_my_warning);
- info_ptr = png_create_info_struct(png_ptr);
- png_init_io(png_ptr, fp);
- png_set_compression_level(png_ptr, 9);
- bit_depth = 8;
- color_type = (img->pixdepth == 32 && img->alpha) ?
- PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB;
- png_set_compression_mem_level(png_ptr, MAX_MEM_LEVEL);
- interlace_type = 0 ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE;
- png_set_IHDR(png_ptr, info_ptr, img->width, img->height, bit_depth,
- color_type, interlace_type, PNG_COMPRESSION_TYPE_DEFAULT,
- PNG_FILTER_TYPE_DEFAULT);
- for (i = 0; i < img->palnum; i++) {
- if (img->palette[i].red == color_values.red &&
- img->palette[i].green == color_values.green &&
- img->palette[i].blue == color_values.blue) {
- trans[i++] = 0x00;
- break;
- }
- trans[i] = 0xFF;
- }
- png_write_info(png_ptr, info_ptr);
- png_set_bgr(png_ptr);
- png_write_image(png_ptr, img->rowptr);
- png_write_end(png_ptr, info_ptr);
- png_destroy_write_struct(&png_ptr, &info_ptr);
- fflush(fp);
- if (fp != stdout) fclose(fp);
- imgbuf_free(img);
- return TRUE;
- error_abort:
- if (fp != stdout && fp != NULL) fclose(fp);
- imgbuf_free(img);
- return FALSE;
- }
- void png_my_error(png_structp png_ptr, png_const_charp message)
- {
- longjmp(png_jmpbuf(png_ptr), 1);
- }
- void png_my_warning(png_structp png_ptr, png_const_charp message){}
- BOOL imgbuf_alloc(IMAGE *img)
- {
- BYTE *bp, **rp;
- LONG n;
- if (img->palnum > 0) {
- img->palette =(PALETTE *) malloc((size_t)img->palnum * sizeof(PALETTE));
- if (img->palette == NULL) { imgbuf_init(img); return FALSE; }
- } else {
- img->palette = NULL;
- }
- img->rowbytes = ((DWORD)img->width * img->pixdepth + 31) / 32 * 4;
- img->imgbytes = img->rowbytes * img->height;
- img->rowptr =(BYTE **)malloc((size_t)img->height * sizeof(BYTE *));
- img->bmpbits =(BYTE *)malloc((size_t)img->imgbytes);
- if (img->rowptr == NULL || img->bmpbits == NULL) {
- imgbuf_free(img); imgbuf_init(img); return FALSE;
- }
- n = img->height;
- rp = img->rowptr;
- bp = img->bmpbits;
- if (img->topdown) {
- while (--n >= 0) {
- *(rp++) = bp;
- bp += img->rowbytes;
- }
- } else {
- bp += img->imgbytes;
- while (--n >= 0) {
- ((DWORD *)bp)[-1] = 0;
- bp -= img->rowbytes;
- *(rp++) = bp;
- }
- }
- return TRUE;
- }
- void imgbuf_free(IMAGE *img)
- {
- free(img->palette);
- free(img->rowptr);
- free(img->bmpbits);
- }
- void imgbuf_init(IMAGE *img)
- {
- img->palette = NULL;
- img->rowptr = NULL;
- img->bmpbits = NULL;
- }