Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/FreeImage/PluginWebP.cpp view on Meta::CPAN
// ==========================================================
// Google WebP Loader & Writer
//
// Design and implementation by
// - Herve Drolon (drolon@infonie.fr)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
#include "FreeImage.h"
#include "Utilities.h"
#include "../Metadata/FreeImageTag.h"
#include "../LibWebP/src/webp/decode.h"
#include "../LibWebP/src/webp/encode.h"
#include "../LibWebP/src/enc/vp8enci.h"
#include "../LibWebP/src/webp/mux.h"
// ==========================================================
// Plugin Interface
// ==========================================================
static int s_format_id;
// ----------------------------------------------------------
// Helpers for the load function
// ----------------------------------------------------------
/**
Read the whole file into memory
*/
static BOOL
ReadFileToWebPData(FreeImageIO *io, fi_handle handle, WebPData * const bitstream) {
uint8_t *raw_data = NULL;
try {
// Read the input file and put it in memory
long start_pos = io->tell_proc(handle);
io->seek_proc(handle, 0, SEEK_END);
size_t file_length = (size_t)(io->tell_proc(handle) - start_pos);
io->seek_proc(handle, start_pos, SEEK_SET);
raw_data = (uint8_t*)malloc(file_length * sizeof(uint8_t));
if(!raw_data) {
throw FI_MSG_ERROR_MEMORY;
}
if(io->read_proc(raw_data, 1, (unsigned)file_length, handle) != file_length) {
throw "Error while reading input stream";
}
// copy pointers (must be released later using free)
bitstream->bytes = raw_data;
bitstream->size = file_length;
return TRUE;
} catch(const char *text) {
if(raw_data) {
free(raw_data);
}
memset(bitstream, 0, sizeof(WebPData));
if(NULL != text) {
FreeImage_OutputMessageProc(s_format_id, text);
}
return FALSE;
}
}
// ----------------------------------------------------------
// Helpers for the save function
// ----------------------------------------------------------
/**
Output function. Should return 1 if writing was successful.
data/data_size is the segment of data to write, and 'picture' is for
reference (and so one can make use of picture->custom_ptr).
*/
static int
WebP_MemoryWriter(const BYTE *data, size_t data_size, const WebPPicture* const picture) {
FIMEMORY *hmem = (FIMEMORY*)picture->custom_ptr;
return data_size ? (FreeImage_WriteMemory(data, 1, (unsigned)data_size, hmem) == data_size) : 0;
}
// ==========================================================
// Plugin Implementation
// ==========================================================
static const char * DLL_CALLCONV
Format() {
return "WebP";
}
static const char * DLL_CALLCONV
Description() {
return "Google WebP image format";
}
static const char * DLL_CALLCONV
Extension() {
return "webp";
}
static const char * DLL_CALLCONV
RegExpr() {
return NULL;
}
static const char * DLL_CALLCONV
MimeType() {
return "image/webp";
}
static BOOL DLL_CALLCONV
( run in 0.484 second using v1.01-cache-2.11-cpan-119454b85a5 )