File: webp.c

package info (click to toggle)
allegro5 2%3A5.2.6.0-3%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 24,212 kB
  • sloc: ansic: 125,319; cpp: 15,781; objc: 4,579; python: 2,802; java: 2,254; javascript: 1,204; sh: 1,002; makefile: 51; perl: 37; xml: 25; pascal: 24
file content (188 lines) | stat: -rw-r--r-- 4,377 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Allegro wrapper routines for libwebp
 * by Sebastian Krzyszkowiak (dos@dosowisko.net).
 */


#include <webp/decode.h>
#include <webp/encode.h>

#include "allegro5/allegro.h"
#include "allegro5/allegro_image.h"
#include "allegro5/internal/aintern_image.h"

#include "iio.h"

ALLEGRO_DEBUG_CHANNEL("image")


/*****************************************************************************
 * Loading routines
 ****************************************************************************/


static ALLEGRO_BITMAP *load_from_buffer(const uint8_t* data, size_t data_size,
   int flags)
{
   ALLEGRO_BITMAP *bmp;
   ALLEGRO_LOCKED_REGION *lock;
   bool premul = !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA);

   WebPDecoderConfig config;
   WebPInitDecoderConfig(&config);

   if (WebPGetFeatures(data, data_size, &config.input) != VP8_STATUS_OK) {
      ALLEGRO_ERROR("Could not read WebP stream info\n");
      return NULL;
   }

   bmp = al_create_bitmap(config.input.width, config.input.height);
   if (!bmp) {
      ALLEGRO_ERROR("al_create_bitmap failed while loading WebP.\n");
      return NULL;
   }

   lock = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE,
      ALLEGRO_LOCK_WRITEONLY);

   config.output.colorspace = premul ? MODE_rgbA : MODE_RGBA;
   config.output.u.RGBA.rgba = (uint8_t*)lock->data;
   config.output.u.RGBA.stride = lock->pitch;
   config.output.u.RGBA.size = lock->pitch * config.input.height;
   config.output.is_external_memory = 1;

   if (WebPDecode(data, data_size, &config) != VP8_STATUS_OK) {
      ALLEGRO_ERROR("Could not decode WebP stream\n");
      al_destroy_bitmap(bmp);
      return NULL;
   }

   al_unlock_bitmap(bmp);

   return bmp;
}


ALLEGRO_BITMAP *_al_load_webp_f(ALLEGRO_FILE *fp, int flags)
{
   ALLEGRO_ASSERT(fp);
   ALLEGRO_BITMAP *bmp;

   size_t data_size = al_fsize(fp);
   uint8_t *data = al_malloc(data_size * sizeof(uint8_t));

   if (al_fread(fp, data, data_size) != data_size) {
      ALLEGRO_ERROR("Could not read WebP file\n");
      free(data);
      return NULL;
   }

   bmp = load_from_buffer(data, data_size, flags);

   free(data);

   return bmp;
}





ALLEGRO_BITMAP *_al_load_webp(const char *filename, int flags)
{
   ALLEGRO_FILE *fp;
   ALLEGRO_BITMAP *bmp;

   ALLEGRO_ASSERT(filename);

   fp = al_fopen(filename, "rb");
   if (!fp) {
      ALLEGRO_ERROR("Unable to open %s for reading.\n", filename);
      return NULL;
   }

   bmp = _al_load_webp_f(fp, flags);

   al_fclose(fp);

   return bmp;
}




/*****************************************************************************
 * Saving routines
 ****************************************************************************/


bool _al_save_webp_f(ALLEGRO_FILE *fp, ALLEGRO_BITMAP *bmp)
{

   ALLEGRO_LOCKED_REGION *lock;
   bool ret = false;

   lock = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE,
      ALLEGRO_LOCK_READONLY);
   if (!lock) {
      ALLEGRO_ERROR("Failed to lock bitmap.\n");
      return false;
   }

   uint8_t *output = NULL;
   size_t size;

   const char* level = al_get_config_value(al_get_system_config(), "image", "webp_quality_level");
   if (!level || strcmp(level, "lossless") == 0) {
      size = WebPEncodeLosslessRGBA((const uint8_t*)lock->data,
         al_get_bitmap_width(bmp), al_get_bitmap_height(bmp),
         lock->pitch, &output);
   } else {
      size = WebPEncodeRGBA((const uint8_t*)lock->data,
         al_get_bitmap_width(bmp), al_get_bitmap_height(bmp),
         lock->pitch, level ? strtol(level, NULL, 10) : 75, &output);
   }

   if (al_fwrite(fp, output, size) == size) {
       ret = true;
   }

#if WEBP_DECODER_ABI_VERSION >= 0x0206
   WebPFree(output);
#else
   free(output);
#endif
   al_unlock_bitmap(bmp);

   return ret;

}


bool _al_save_webp(const char *filename, ALLEGRO_BITMAP *bmp)
{
   ALLEGRO_FILE *fp;
   bool retsave;
   bool retclose;

   ALLEGRO_ASSERT(filename);
   ALLEGRO_ASSERT(bmp);

   fp = al_fopen(filename, "wb");
   if (!fp) {
      ALLEGRO_ERROR("Unable to open file %s for writing\n", filename);
      return false;
   }

   retsave = _al_save_webp_f(fp, bmp);
   if (!retsave) {
      ALLEGRO_ERROR("Unable to store WebP bitmap in file %s\n", filename);
   }
   retclose = al_fclose(fp);
   if (!retclose) {
      ALLEGRO_ERROR("Unable to close file %s\n", filename);
   }

   return retsave && retclose;
}

/* vim: set sts=3 sw=3 et: */