Image* FW::importLodePngImage(InputStream& stream) { // Read the entire input stream. Array<U8> dataBuffer; int blockSize = 4096; for (;;) { int pos = dataBuffer.getSize(); dataBuffer.resize(pos + blockSize); int num = stream.read(dataBuffer.getPtr(pos), blockSize); if (num < blockSize) { dataBuffer.resize(pos + num); break; } } if (hasError()) return NULL; // Decode image info. LodePNG::Decoder decoder; decoder.inspect(dataBuffer.getPtr(), dataBuffer.getSize()); Vec2i size(decoder.getWidth(), decoder.getHeight()); bool hasAlpha = (LodePNG_InfoColor_canHaveAlpha(&decoder.getInfoPng().color) != 0); if (decoder.hasError()) setError("importLodePngImage(): LodePNG error %d!", decoder.getError()); if (min(size) <= 0) setError("importLodePngImage(): Invalid image size!"); if (hasError()) return NULL; // Decode image data. int numBytes = size.x * size.y * ((hasAlpha) ? 4 : 3); std::vector<U8> pixelBuffer; pixelBuffer.reserve(numBytes); decoder.getInfoRaw().color.colorType = (hasAlpha) ? 6 : 2; decoder.decode(pixelBuffer, dataBuffer.getPtr(), dataBuffer.getSize()); if (decoder.hasError()) setError("importLodePngImage(): LodePNG error %d!", decoder.getError()); if ((int)pixelBuffer.size() != numBytes) setError("importLodePngImage(): Incorrect amount of pixel data!"); if (hasError()) return NULL; // Create image. Image* image = new Image(size, (hasAlpha) ? ImageFormat::R8_G8_B8_A8 : ImageFormat::R8_G8_B8); image->getBuffer().set(&pixelBuffer[0], numBytes); return image; }
//lodepng용 추가 함수 static void PrintLog(LodePNG::Decoder &decoder) { if(decoder.hasError()) { std::cout << "error " << decoder.getError() << ": " << LodePNG_error_text(decoder.getError()) << std::endl; } else { std::cout << "\n" << "w: " << decoder.getWidth() << "\n" << "h: " << decoder.getHeight() << "\n" << "bitDepth: " << decoder.getInfoPng().color.bitDepth << "\n" << "bpp: " << decoder.getBpp() << "\n" << "colorChannels: " << decoder.getChannels() << "\n" << "paletteSize: " << decoder.getInfoPng().color.palettesize / 4 << "\n" << "colorType: " << decoder.getInfoPng().color.colorType << "\n" << "compressionMethod: " << decoder.getInfoPng().compressionMethod << "\n" << "filterMethod: " << decoder.getInfoPng().filterMethod << "\n" << "interlaceMethod: " << decoder.getInfoPng().interlaceMethod << "\n"; for(size_t i = 0; i < decoder.getInfoPng().text.num; i++) { std::cout << decoder.getInfoPng().text.keys[i] << ": " << decoder.getInfoPng().text.strings[i] << "\n"; } } }
static bool Load(Image *img, ImageDesc *desc, uchar *data, int data_size) { //lodepng를 기반으로 적절히 로딩하기 SR_ASSERT(data != NULL); SR_ASSERT(data_size > 0); img->image_data_.clear(); LodePNG::Decoder decoder; decoder.decode(img->image_data(), data, data_size); if(decoder.hasError()) { return false; } else { desc->width = decoder.getWidth(); desc->height = decoder.getHeight(); desc->bit_depth = decoder.getInfoPng().color.bitDepth; desc->bpp = decoder.getBpp(); desc->color_channels = decoder.getChannels(); desc->is_grayscale = decoder.isGreyscaleType() > 0 ? true : false; desc->is_alpha = decoder.isAlphaType() > 0 ? true : false; return true; } }