Skip to content

Instantly share code, notes, and snippets.

@martell
Created September 21, 2012 10:17
Show Gist options
  • Save martell/3760774 to your computer and use it in GitHub Desktop.
Save martell/3760774 to your computer and use it in GitHub Desktop.
Webp implementation for cocos2dx by martell malone
bool CCImage::_initWithWebpData(void * data, int nSize)
{
WebPMuxError mux_err;
WebPDecoderConfig config;
WebPData defdata;
WebPMux* mux;
WebPDecBuffer* pic;
int has_animation;
uint32_t flags=0;
int loop_count=1;
int frame_num=1;
int frame_max=0;
if (!WebPInitDecoderConfig(&config)) {
fprintf(stderr, "Library version mismatch!\n");
return false;
}
defdata.bytes_ = (uint8_t*)data;
defdata.size_ = (size_t)nSize;
mux = WebPMuxCreate(&defdata, 0);
if (mux == NULL) {
fprintf(stderr, "Could not create demuxing object!\n");
return false;
}
mux_err = WebPMuxGetFeatures(mux, &flags);
if (mux_err != WEBP_MUX_OK) printf("Error in WebPMuxGetFeatures!\n");
if (flags & TILE_FLAG) printf("Tiling is not supported for now!\n");
has_animation = !!(flags & ANIMATION_FLAG);
if (has_animation) {
mux_err = WebPMuxGetLoopCount(mux, &loop_count);
if (mux_err != WEBP_MUX_OK && mux_err != WEBP_MUX_NOT_FOUND) printf("Error in WebPMuxGetLoopCount!\n");
mux_err = WebPMuxNumChunks(mux, WEBP_CHUNK_IMAGE,&frame_max);
if (mux_err != WEBP_MUX_OK) printf("Error in WebPMuxNumChunks!\n");
printf("VP8X: Found %d images in file (loop count = %d)\n",frame_max, loop_count);
}
// Decode first frame
int duration;
WebPData *pdata, image_data;
int x_off = 0, y_off = 0;
int ok = 0;
if (has_animation) {
if (WebPMuxGetFrame(mux, frame_num, &image_data,
&x_off, &y_off, &duration) != WEBP_MUX_OK) {
goto end;
}
if (x_off != 0 || y_off != 0) {
printf("Frame offsets not yet supported! Forcing offset to 0,0\n");
x_off = y_off = 0;
}
pdata = &image_data;
} else {
pdata = &defdata;
}
config.output.colorspace = MODE_RGBA;
ok = (WebPDecode(pdata->bytes_, pdata->size_, &config) == VP8_STATUS_OK);
end:
if (!ok) {
fprintf(stderr, "Decoding of frame #%d failed!\n", frame_num);
} else {
m_pData = new unsigned char[config.output.u.RGBA.size];
memcpy(m_pData, config.output.u.RGBA.rgba, config.output.u.RGBA.size);
}
m_nBitsPerComponent = 4;
m_nHeight = (short)config.output.height;
m_nWidth = (short)config.output.width;
m_bHasAlpha = true;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment