📄 ximapng.cpp
字号:
delete[] row_pointers; /* read the rest of the file, getting any additional chunks in info_ptr */ png_read_end(png_ptr, info_ptr); /* clean up after the read, and free any memory allocated - REQUIRED */ png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); } catch (const char *message) { if (strcmp(message,"")) strncpy(info.szLastError,message,255); return FALSE; } /* that's it */ return TRUE;}////////////////////////////////////////////////////////////////////////////////#if CXIMAGE_SUPPORT_ENCODE////////////////////////////////////////////////////////////////////////////////bool CxImagePNG::Encode(CxFile *hFile){ if (EncodeSafeCheck(hFile)) return false; CImageIterator iter(this); BYTE trans[256]; //for transparency (don't move) png_struct *png_ptr; png_info *info_ptr; try{ /* Create and initialize the png_struct with the desired error handler * functions. If you want to use the default stderr and longjump method, * you can supply NULL for the last three parameters. We also check that * the library version is compatible with the one used at compile time, * in case we are using dynamically linked libraries. REQUIRED. */ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,(void *)NULL,NULL,NULL); if (png_ptr == NULL) throw "Failed to create PNG structure"; /* Allocate/initialize the image information data. REQUIRED */ info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL){ png_destroy_write_struct(&png_ptr, (png_infopp)NULL); throw "Failed to initialize PNG info structure"; } /* Set error handling. REQUIRED if you aren't supplying your own * error hadnling functions in the png_create_write_struct() call. */ if (setjmp(png_ptr->jmpbuf)){ /* If we get here, we had a problem reading the file */ if (info_ptr->palette) free(info_ptr->palette); png_destroy_write_struct(&png_ptr, (png_infopp)&info_ptr); throw "Error saving PNG file"; } int row_stride = info.dwEffWidth; /* set up the output control */ //png_init_io(png_ptr, hFile); // use custom I/O functions png_set_write_fn(png_ptr,hFile,(png_rw_ptr)user_write_data,(png_flush_ptr)user_flush_data); /* set the file information here */ info_ptr->width = GetWidth(); info_ptr->height = GetHeight(); info_ptr->pixel_depth = (BYTE)GetBpp(); info_ptr->channels = (GetBpp()>8) ? (BYTE)3: (BYTE)1; info_ptr->bit_depth = (BYTE)(GetBpp()/info_ptr->channels); info_ptr->color_type = GetColorType(); info_ptr->compression_type = info_ptr->filter_type = 0; info_ptr->valid = 0; info_ptr->rowbytes = row_stride; switch(GetCodecOption(CXIMAGE_FORMAT_PNG)){ case 1: info_ptr->interlace_type = PNG_INTERLACE_ADAM7; break; default: info_ptr->interlace_type = PNG_INTERLACE_NONE; } /* set compression level */ //png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); /* set background */ png_color_16 image_background={ 0, 255, 255, 255, 0 }; if (info.nBkgndIndex!=-1) { image_background.blue = info.nBkgndColor.rgbBlue; image_background.green = info.nBkgndColor.rgbGreen; image_background.red = info.nBkgndColor.rgbRed; } png_set_bKGD(png_ptr, info_ptr, &image_background); /* set metrics */ png_set_pHYs(png_ptr, info_ptr, head.biXPelsPerMeter, head.biYPelsPerMeter, PNG_RESOLUTION_METER); /* set the palette if there is one */ if (GetPalette()){ png_set_IHDR(png_ptr, info_ptr, info_ptr->width, info_ptr->height, info_ptr->bit_depth, PNG_COLOR_TYPE_PALETTE, info_ptr->interlace_type, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); info_ptr->valid |= PNG_INFO_PLTE; //<DP> simple transparency if (info.nBkgndIndex != -1){ trans[0]=0; info_ptr->num_trans = 1; info_ptr->valid |= PNG_INFO_tRNS; info_ptr->trans = trans; // the transparency indexes start from 0 if (info.nBkgndIndex){ SwapIndex(0,(BYTE)info.nBkgndIndex); // the ghost must set the changed attributes in the body if (info.pGhost) info.pGhost->SetTransIndex(0); } } int nc = GetNumColors(); /* We not need to write unused colors! <Basara>*/ /* only for small images <DP>*/ if ((nc>2)&&((head.biWidth*head.biHeight)<65536)){ nc = 0; for (DWORD y=0;y<GetHeight();y++){ for (DWORD x=0;x<GetWidth();x++){ if (GetPixelIndex(x,y)>nc){ nc=GetPixelIndex(x,y); } } } nc++; } if (info.bAlphaPaletteEnabled){ for(WORD ip=0; ip<nc;ip++) trans[ip]=GetPaletteColor((BYTE)ip).rgbReserved; info_ptr->num_trans = (WORD)nc; info_ptr->valid |= PNG_INFO_tRNS; info_ptr->trans = trans; } // copy the palette colors info_ptr->palette = new png_color[nc]; info_ptr->num_palette = (png_uint_16) nc; for (int i=0; i<nc; i++) GetPaletteColor(i, &info_ptr->palette[i].red, &info_ptr->palette[i].green, &info_ptr->palette[i].blue); } #if CXIMAGE_SUPPORT_ALPHA // <vho> //Merge the transparent color with the alpha channel bool bNeedTempAlpha = false; if (head.biBitCount==24 && info.nBkgndIndex>=0){ if (!AlphaIsValid()){ bNeedTempAlpha = true; AlphaCreate(); } RGBQUAD c,ct=GetTransColor(); for(long y=0; y < head.biHeight; y++){ for(long x=0; x < head.biWidth ; x++){ c=GetPixelColor(x,y,false); if (*(long*)&c==*(long*)&ct) AlphaSet(x,y,0); }} }#endif // CXIMAGE_SUPPORT_ALPHA // <vho>#if CXIMAGE_SUPPORT_ALPHA // <vho> if (AlphaIsValid()){ row_stride = 4 * head.biWidth; info_ptr->pixel_depth = 32; info_ptr->channels = 4; info_ptr->bit_depth = 8; info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; info_ptr->rowbytes = row_stride; /* write the file information */ png_write_info(png_ptr, info_ptr); //<Ranger> "10+row_stride" fix heap deallocation problem during debug??? BYTE *row_pointers = new BYTE[10+row_stride]; //interlace handling int num_pass = png_set_interlace_handling(png_ptr); for (int pass = 0; pass < num_pass; pass++){ //write image iter.Upset(); long ay=head.biHeight-1; RGBQUAD c; do { for (long ax=head.biWidth-1; ax>=0;ax--){ c=GetPixelColor(ax,ay); row_pointers[ax*4+3]=(BYTE)((AlphaGet(ax,ay)*info.nAlphaMax)/255); row_pointers[ax*4+2]=c.rgbBlue; row_pointers[ax*4+1]=c.rgbGreen; row_pointers[ax*4]=c.rgbRed; } png_write_row(png_ptr, row_pointers); ay--; } while(iter.PrevRow()); } delete [] row_pointers; } else#endif //CXIMAGE_SUPPORT_ALPHA // <vho> { /* write the file information */ png_write_info(png_ptr, info_ptr); /* If you are only writing one row at a time, this works */ BYTE *row_pointers = new BYTE[10+row_stride]; //interlace handling int num_pass = png_set_interlace_handling(png_ptr); for (int pass = 0; pass < num_pass; pass++){ //write image iter.Upset(); do { iter.GetRow(row_pointers, row_stride); //HACK BY OP if (info_ptr->color_type == 2 /*COLORTYPE_COLOR*/) RGBtoBGR(row_pointers, row_stride); png_write_row(png_ptr, row_pointers); } while(iter.PrevRow()); } delete [] row_pointers; }#if CXIMAGE_SUPPORT_ALPHA // <vho> /* remove the temporary alpha channel*/ if (bNeedTempAlpha) AlphaDelete();#endif // CXIMAGE_SUPPORT_ALPHA // <vho> /* It is REQUIRED to call this to finish writing the rest of the file */ png_write_end(png_ptr, info_ptr); /* if you malloced the palette, free it here */ if (info_ptr->palette) delete[] (info_ptr->palette); /* clean up after the write, and free any memory allocated */ png_destroy_write_struct(&png_ptr, (png_infopp)&info_ptr); } catch (const char *message) { strncpy(info.szLastError,message,255); return FALSE; } /* that's it */ return TRUE;}////////////////////////////////////////////////////////////////////////////////#endif // CXIMAGE_SUPPORT_ENCODE////////////////////////////////////////////////////////////////////////////////#endif // CXIMAGE_SUPPORT_PNG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -