bool ImageWriter::convert( Image* image ) { #pragma TODO("Fix the LodePNG image writing code to the new API") #if defined(ENABLE_IMAGE_WRITER_) LodePNG::Encoder encoder; LodePNG_InfoColor& png = encoder.getInfoPng().color; LodePNG_InfoColor& raw = encoder.getInfoRaw().color; switch(image->getPixelFormat()) { case PixelFormat::Depth: png.colorType = 0; raw.colorType = 0; break; case PixelFormat::R8G8B8: png.colorType = 2; raw.colorType = 2; break; case PixelFormat::R8G8B8A8: png.colorType = 6; raw.colorType = 6; break; default: LogError("Pixel format is not supported"); return false; } raw.bitDepth = 8; encoder.encode(output, image->getBuffer(), image->getWidth(), image->getHeight()); if( encoder.hasError() ) { LogError("Could not encode image to PNG"); return false; } return true; #else return false; #endif }
void FW::exportLodePngImage(OutputStream& stream, const Image* image) { // Select format and convert. FW_ASSERT(image); const Vec2i& size = image->getSize(); bool hasAlpha = image->getFormat().hasChannel(ImageFormat::ChannelType_A); ImageFormat::ID format = (hasAlpha) ? ImageFormat::R8_G8_B8_A8 : ImageFormat::R8_G8_B8; Image* converted = NULL; if (image->getFormat().getID() != format) { converted = new Image(size, format); *converted = *image; } // Encode image. LodePNG::Encoder encoder; int colorType = (hasAlpha) ? 6 : 2; encoder.getSettings().autoLeaveOutAlphaChannel = false; encoder.getInfoRaw().color.colorType = colorType; encoder.getInfoPng().color.colorType = colorType; std::vector<U8> dataBuffer; encoder.encode(dataBuffer, (U8*)((converted) ? converted : image)->getPtr(), size.x, size.y); if (encoder.hasError()) setError("exportLodePngImage(): LodePNG error %d!", encoder.getError()); // Write to the output stream. if (!hasError() && !dataBuffer.empty()) stream.write(&dataBuffer[0], (int)dataBuffer.size()); // Clean up. delete converted; }