bool ImageReaderWebP::readImage()

in imagecore/formats/internal/webp.cpp [121:203]


bool ImageReaderWebP::readImage(Image* destImage)
{
	if( !supportsOutputColorModel(destImage->getColorModel()) ) {
		return false;
	}

	unsigned int destWidth = destImage->getWidth();
	unsigned int destHeight = destImage->getHeight();

	m_DecoderConfig.output.width = destWidth;
	m_DecoderConfig.output.height = destHeight;
	m_DecoderConfig.output.is_external_memory = true;
	m_DecoderConfig.options.use_threads = 0;
	m_DecoderConfig.options.no_fancy_upsampling = 0;

	if( destWidth != m_Width || destHeight != m_Height ) {
		m_DecoderConfig.options.use_scaling = 1;
		m_DecoderConfig.options.scaled_width = destWidth;
		m_DecoderConfig.options.scaled_height = destHeight;
	}

	EImageColorModel colorModel = destImage->getColorModel();

	bool success = false;

	if( Image::colorModelIsRGBA(colorModel)) {
		ImageInterleaved* image = destImage->asInterleaved();
		unsigned int destPitch = 0;

		uint8_t* destBuffer = image->lockRect(destWidth, destHeight, destPitch);

		SECURE_ASSERT(SafeUMul(destWidth, image->getComponentSize()) <= destPitch);
		m_DecoderConfig.output.colorspace = MODE_RGBA;
		m_DecoderConfig.output.u.RGBA.rgba = destBuffer;
		m_DecoderConfig.output.u.RGBA.size = image->getImageSize();
		m_DecoderConfig.output.u.RGBA.stride = destPitch;
		START_CLOCK(decodeRGB);
		if( WebPDecode(m_DecodeBuffer, (size_t)m_DecodeLength, &m_DecoderConfig) == VP8_STATUS_OK ) {
			success = true;
		}
		END_CLOCK(decodeRGB);
	} else if( Image::colorModelIsYUV(colorModel) ) {
		ImageYUV* image = destImage->asYUV();
		ImagePlane8* planeY = image->getPlaneY();
		ImagePlane8* planeU = image->getPlaneU();
		ImagePlane8* planeV = image->getPlaneV();
		EYUVRange desiredRange = image->getRange();

		unsigned int pitchY = 0;
		uint8_t* bufferY = planeY->lockRect(0, 0, planeY->getWidth(), planeY->getHeight(), pitchY);
		unsigned int pitchU = 0;
		uint8_t* bufferU = planeU->lockRect(0, 0, planeU->getWidth(), planeU->getHeight(), pitchU);
		unsigned int pitchV = 0;
		uint8_t* bufferV = planeV->lockRect(0, 0, planeV->getWidth(), planeV->getHeight(), pitchV);

		m_DecoderConfig.output.colorspace = MODE_YUV;
		m_DecoderConfig.output.u.YUVA.y = bufferY;
		m_DecoderConfig.output.u.YUVA.y_size = planeY->getImageSize();
		m_DecoderConfig.output.u.YUVA.y_stride = pitchY;
		m_DecoderConfig.output.u.YUVA.u = bufferU;
		m_DecoderConfig.output.u.YUVA.u_size = planeU->getImageSize();
		m_DecoderConfig.output.u.YUVA.u_stride = pitchU;
		m_DecoderConfig.output.u.YUVA.v = bufferV;
		m_DecoderConfig.output.u.YUVA.v_size = planeV->getImageSize();
		m_DecoderConfig.output.u.YUVA.v_stride = pitchV;

		START_CLOCK(decodeYUV);
		if( WebPDecode(m_DecodeBuffer, (size_t)m_DecodeLength, &m_DecoderConfig) == VP8_STATUS_OK ) {
			success = true;
		}
		END_CLOCK(decodeYUV);

		START_CLOCK(remap);
		image->setRange(kYUVRange_Compressed);
		if( desiredRange == kYUVRange_Full ) {
			image->expandRange(image);
		}
		END_CLOCK(remap);
	}

	m_TotalRowsRead += m_Height;
	return success;
}