| 1 |
/* Allegro wrapper routines for libwebp |
| 2 |
* by Sebastian Krzyszkowiak ([email protected]). |
| 3 |
*/ |
| 4 |
|
| 5 |
|
| 6 |
#include <webp/decode.h> |
| 7 |
#include <webp/encode.h> |
| 8 |
|
| 9 |
#include "allegro5/allegro.h" |
| 10 |
#include "allegro5/allegro_image.h" |
| 11 |
#include "allegro5/internal/aintern_image.h" |
| 12 |
|
| 13 |
#include "iio.h" |
| 14 |
|
| 15 |
ALLEGRO_DEBUG_CHANNEL("image") |
| 16 |
|
| 17 |
|
| 18 |
/***************************************************************************** |
| 19 |
* Loading routines |
| 20 |
****************************************************************************/ |
| 21 |
|
| 22 |
|
| 23 |
static ALLEGRO_BITMAP *load_from_buffer(const uint8_t* data, size_t data_size, |
| 24 |
int flags) |
| 25 |
{ |
| 26 |
ALLEGRO_BITMAP *bmp; |
| 27 |
ALLEGRO_LOCKED_REGION *lock; |
| 28 |
bool premul = !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA); |
| 29 |
|
| 30 |
WebPDecoderConfig config; |
| 31 |
WebPInitDecoderConfig(&config); |
| 32 |
|
| 33 |
if (WebPGetFeatures(data, data_size, &config.input) != VP8_STATUS_OK) { |
| 34 |
ALLEGRO_ERROR("Could not read WebP stream info\n"); |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
|
| 38 |
bmp = al_create_bitmap(config.input.width, config.input.height); |
| 39 |
if (!bmp) { |
| 40 |
ALLEGRO_ERROR("al_create_bitmap failed while loading WebP.\n"); |
| 41 |
return NULL; |
| 42 |
} |
| 43 |
|
| 44 |
lock = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, |
| 45 |
ALLEGRO_LOCK_WRITEONLY); |
| 46 |
|
| 47 |
config.output.colorspace = premul ? MODE_rgbA : MODE_RGBA; |
| 48 |
config.output.u.RGBA.rgba = (uint8_t*)lock->data; |
| 49 |
config.output.u.RGBA.stride = lock->pitch; |
| 50 |
config.output.u.RGBA.size = lock->pitch * config.input.height; |
| 51 |
config.output.is_external_memory = 1; |
| 52 |
|
| 53 |
if (WebPDecode(data, data_size, &config) != VP8_STATUS_OK) { |
| 54 |
ALLEGRO_ERROR("Could not decode WebP stream\n"); |
| 55 |
al_destroy_bitmap(bmp); |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
al_unlock_bitmap(bmp); |
| 60 |
|
| 61 |
return bmp; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
ALLEGRO_BITMAP *_al_load_webp_f(ALLEGRO_FILE *fp, int flags) |
| 66 |
{ |
| 67 |
ALLEGRO_ASSERT(fp); |
| 68 |
ALLEGRO_BITMAP *bmp; |
| 69 |
|
| 70 |
size_t data_size = al_fsize(fp); |
| 71 |
uint8_t *data = al_malloc(data_size * sizeof(uint8_t)); |
| 72 |
|
| 73 |
if (al_fread(fp, data, data_size) != data_size) { |
| 74 |
ALLEGRO_ERROR("Could not read WebP file\n"); |
| 75 |
free(data); |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
bmp = load_from_buffer(data, data_size, flags); |
| 80 |
|
| 81 |
free(data); |
| 82 |
|
| 83 |
return bmp; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
ALLEGRO_BITMAP *_al_load_webp(const char *filename, int flags) |
| 91 |
{ |
| 92 |
ALLEGRO_FILE *fp; |
| 93 |
ALLEGRO_BITMAP *bmp; |
| 94 |
|
| 95 |
ALLEGRO_ASSERT(filename); |
| 96 |
|
| 97 |
fp = al_fopen(filename, "rb"); |
| 98 |
if (!fp) { |
| 99 |
ALLEGRO_ERROR("Unable to open %s for reading.\n", filename); |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
|
| 103 |
bmp = _al_load_webp_f(fp, flags); |
| 104 |
|
| 105 |
al_fclose(fp); |
| 106 |
|
| 107 |
return bmp; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
/***************************************************************************** |
| 114 |
* Saving routines |
| 115 |
****************************************************************************/ |
| 116 |
|
| 117 |
|
| 118 |
bool _al_save_webp_f(ALLEGRO_FILE *fp, ALLEGRO_BITMAP *bmp) |
| 119 |
{ |
| 120 |
|
| 121 |
ALLEGRO_LOCKED_REGION *lock; |
| 122 |
bool ret = false; |
| 123 |
|
| 124 |
lock = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, |
| 125 |
ALLEGRO_LOCK_READONLY); |
| 126 |
if (!lock) { |
| 127 |
ALLEGRO_ERROR("Failed to lock bitmap.\n"); |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
uint8_t *output = NULL; |
| 132 |
size_t size; |
| 133 |
|
| 134 |
const char* level = al_get_config_value(al_get_system_config(), "image", "webp_quality_level"); |
| 135 |
if (!level || strcmp(level, "lossless") == 0) { |
| 136 |
size = WebPEncodeLosslessRGBA((const uint8_t*)lock->data, |
| 137 |
al_get_bitmap_width(bmp), al_get_bitmap_height(bmp), |
| 138 |
lock->pitch, &output); |
| 139 |
} else { |
| 140 |
size = WebPEncodeRGBA((const uint8_t*)lock->data, |
| 141 |
al_get_bitmap_width(bmp), al_get_bitmap_height(bmp), |
| 142 |
lock->pitch, level ? strtol(level, NULL, 10) : 75, &output); |
| 143 |
} |
| 144 |
|
| 145 |
if (al_fwrite(fp, output, size) == size) { |
| 146 |
ret = true; |
| 147 |
} |
| 148 |
|
| 149 |
#if WEBP_DECODER_ABI_VERSION >= 0x0206 |
| 150 |
WebPFree(output); |
| 151 |
#else |
| 152 |
free(output); |
| 153 |
#endif |
| 154 |
al_unlock_bitmap(bmp); |
| 155 |
|
| 156 |
return ret; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
bool _al_save_webp(const char *filename, ALLEGRO_BITMAP *bmp) |
| 162 |
{ |
| 163 |
ALLEGRO_FILE *fp; |
| 164 |
bool retsave; |
| 165 |
bool retclose; |
| 166 |
|
| 167 |
ALLEGRO_ASSERT(filename); |
| 168 |
ALLEGRO_ASSERT(bmp); |
| 169 |
|
| 170 |
fp = al_fopen(filename, "wb"); |
| 171 |
if (!fp) { |
| 172 |
ALLEGRO_ERROR("Unable to open file %s for writing\n", filename); |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
retsave = _al_save_webp_f(fp, bmp); |
| 177 |
if (!retsave) { |
| 178 |
ALLEGRO_ERROR("Unable to store WebP bitmap in file %s\n", filename); |
| 179 |
} |
| 180 |
retclose = al_fclose(fp); |
| 181 |
if (!retclose) { |
| 182 |
ALLEGRO_ERROR("Unable to close file %s\n", filename); |
| 183 |
} |
| 184 |
|
| 185 |
return retsave && retclose; |
| 186 |
} |
| 187 |
|
| 188 |
/* vim: set sts=3 sw=3 et: */ |