Save texture as PNG From Shazz/71M - thanks guys!
| 100 | // From Shazz/71M - thanks guys! |
| 101 | //***************************************************************************** |
| 102 | void PngSaveImage( DataSink * sink, const void * data, const void * palette, ETextureFormat pixelformat, s32 pitch, u32 width, u32 height, bool use_alpha ) |
| 103 | { |
| 104 | #ifdef DAEDALUS_ENABLE_ASSERTS |
| 105 | DAEDALUS_ASSERT( !IsTextureFormatPalettised( pixelformat ) || palette, "No palette specified" ); |
| 106 | #endif |
| 107 | png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 108 | if (!png_ptr) |
| 109 | return; |
| 110 | |
| 111 | png_infop info_ptr = png_create_info_struct(png_ptr); |
| 112 | if (!info_ptr) |
| 113 | { |
| 114 | png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | png_set_write_fn(png_ptr, sink, PngWrite, PngFlush); |
| 119 | png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 120 | png_write_info(png_ptr, info_ptr); |
| 121 | |
| 122 | u8* line = (u8*) malloc(width * 4); |
| 123 | |
| 124 | const u8 * p = reinterpret_cast< const u8 * >( data ); |
| 125 | const NativePf8888 * pal8888 = reinterpret_cast< const NativePf8888 * >( palette ); |
| 126 | |
| 127 | // If the pitch is negative (i.e for a screenshot), start at the last row and work backwards. |
| 128 | if (pitch < 0) |
| 129 | { |
| 130 | p += -pitch * (height-1); |
| 131 | } |
| 132 | |
| 133 | for ( u32 y = 0; y < height; y++ ) |
| 134 | { |
| 135 | switch (pixelformat) |
| 136 | { |
| 137 | case TexFmt_5650: WritePngRow< NativePf5650 >( line, p, width ); break; |
| 138 | case TexFmt_5551: WritePngRow< NativePf5551 >( line, p, width ); break; |
| 139 | case TexFmt_4444: WritePngRow< NativePf4444 >( line, p, width ); break; |
| 140 | case TexFmt_8888: WritePngRow< NativePf8888 >( line, p, width ); break; |
| 141 | case TexFmt_CI4_8888: WritePngRowPal4( line, p, width, pal8888 ); break; |
| 142 | case TexFmt_CI8_8888: WritePngRowPal8( line, p, width, pal8888 ); break; |
| 143 | } |
| 144 | |
| 145 | // |
| 146 | // Set alpha to full if it's not required. |
| 147 | // Should really create an PNG_COLOR_TYPE_RGB image instead |
| 148 | // |
| 149 | if( !use_alpha ) |
| 150 | { |
| 151 | for( u32 x = 0; x < width; ++x ) |
| 152 | { |
| 153 | line[ x*4 + 3 ] = 0xff; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | p += pitch; |
| 158 | png_write_row(png_ptr, line); |
| 159 | } |
no test coverage detected