| 1 |
#include "allegro5/allegro.h" |
| 2 |
#include "allegro5/allegro_image.h" |
| 3 |
#include "allegro5/internal/aintern_image.h" |
| 4 |
|
| 5 |
#include "iio.h" |
| 6 |
|
| 7 |
ALLEGRO_DEBUG_CHANNEL("image") |
| 8 |
|
| 9 |
/* Do NOT simplify this to just (x), it doesn't work in MSVC. */ |
| 10 |
#define INT_TO_BOOL(x) ((x) != 0) |
| 11 |
|
| 12 |
ALLEGRO_BITMAP *_al_load_pcx_f(ALLEGRO_FILE *f, int flags) |
| 13 |
{ |
| 14 |
ALLEGRO_BITMAP *b; |
| 15 |
int c; |
| 16 |
int width, height; |
| 17 |
int bpp, bytes_per_line; |
| 18 |
int x, xx, y; |
| 19 |
char ch; |
| 20 |
ALLEGRO_LOCKED_REGION *lr; |
| 21 |
unsigned char *buf; |
| 22 |
PalEntry pal[256]; |
| 23 |
bool keep_index; |
| 24 |
ASSERT(f); |
| 25 |
|
| 26 |
al_fgetc(f); /* skip manufacturer ID */ |
| 27 |
al_fgetc(f); /* skip version flag */ |
| 28 |
al_fgetc(f); /* skip encoding flag */ |
| 29 |
|
| 30 |
char color_plane = al_fgetc(f); |
| 31 |
if (color_plane != 8) { /* we like 8 bit color planes */ |
| 32 |
ALLEGRO_ERROR("Invalid color plane %d.\n", color_plane); |
| 33 |
return NULL; |
| 34 |
} |
| 35 |
|
| 36 |
width = -(al_fread16le(f)); /* xmin */ |
| 37 |
height = -(al_fread16le(f)); /* ymin */ |
| 38 |
width += al_fread16le(f) + 1; /* xmax */ |
| 39 |
height += al_fread16le(f) + 1; /* ymax */ |
| 40 |
|
| 41 |
al_fread32le(f); /* skip DPI values */ |
| 42 |
|
| 43 |
for (c = 0; c < 16 * 3; c++) { /* skip the 16 color palette */ |
| 44 |
al_fgetc(f); |
| 45 |
} |
| 46 |
|
| 47 |
al_fgetc(f); |
| 48 |
|
| 49 |
bpp = al_fgetc(f) * 8; /* how many color planes? */ |
| 50 |
|
| 51 |
if ((bpp != 8) && (bpp != 24)) { |
| 52 |
ALLEGRO_ERROR("Invalid bpp %d.\n", color_plane); |
| 53 |
return NULL; |
| 54 |
} |
| 55 |
|
| 56 |
bytes_per_line = al_fread16le(f); |
| 57 |
/* It can only be this because we only support 8 bit planes */ |
| 58 |
if (bytes_per_line < width) { |
| 59 |
ALLEGRO_ERROR("Invalid bytes per line %d\n", bytes_per_line); |
| 60 |
return NULL; |
| 61 |
} |
| 62 |
|
| 63 |
for (c = 0; c < 60; c++) /* skip some more junk */ |
| 64 |
al_fgetc(f); |
| 65 |
|
| 66 |
if (al_feof(f) || al_ferror(f)) { |
| 67 |
ALLEGRO_ERROR("Unexpected EOF/error.\n"); |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
b = al_create_bitmap(width, height); |
| 72 |
if (!b) { |
| 73 |
ALLEGRO_ERROR("Failed to create bitmap.\n"); |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
|
| 77 |
keep_index = INT_TO_BOOL(flags & ALLEGRO_KEEP_INDEX); |
| 78 |
|
| 79 |
al_set_errno(0); |
| 80 |
|
| 81 |
if (bpp == 8) { |
| 82 |
/* The palette comes after the image data. We need to to keep the |
| 83 |
* whole image in a temporary buffer before mapping the final colours. |
| 84 |
*/ |
| 85 |
buf = (unsigned char *)al_malloc(bytes_per_line * height); |
| 86 |
} |
| 87 |
else { |
| 88 |
/* We can read one line at a time. */ |
| 89 |
buf = (unsigned char *)al_malloc(bytes_per_line * 3); |
| 90 |
} |
| 91 |
|
| 92 |
if (bpp == 8 && keep_index) { |
| 93 |
lr = al_lock_bitmap(b, ALLEGRO_PIXEL_FORMAT_SINGLE_CHANNEL_8, ALLEGRO_LOCK_WRITEONLY); |
| 94 |
} |
| 95 |
else { |
| 96 |
lr = al_lock_bitmap(b, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_WRITEONLY); |
| 97 |
} |
| 98 |
if (!lr) { |
| 99 |
ALLEGRO_ERROR("Failed to lock bitmap.\n"); |
| 100 |
al_free(buf); |
| 101 |
return NULL; |
| 102 |
} |
| 103 |
|
| 104 |
xx = 0; /* index into buf, only for bpp = 8 */ |
| 105 |
|
| 106 |
for (y = 0; y < height; y++) { /* read RLE encoded PCX data */ |
| 107 |
|
| 108 |
x = 0; |
| 109 |
|
| 110 |
while (x < bytes_per_line * bpp / 8) { |
| 111 |
ch = al_fgetc(f); |
| 112 |
if ((ch & 0xC0) == 0xC0) { /* a run */ |
| 113 |
c = (ch & 0x3F); |
| 114 |
ch = al_fgetc(f); |
| 115 |
} |
| 116 |
else { |
| 117 |
c = 1; /* single pixel */ |
| 118 |
} |
| 119 |
|
| 120 |
if (bpp == 8) { |
| 121 |
while (c--) { |
| 122 |
if (x < width) /* ignore padding */ |
| 123 |
buf[xx++] = ch; |
| 124 |
x++; |
| 125 |
} |
| 126 |
} |
| 127 |
else { |
| 128 |
while (c--) { |
| 129 |
if (x < width * 3) /* ignore padding */ |
| 130 |
buf[x] = ch; |
| 131 |
x++; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
if (bpp == 24) { |
| 136 |
char *dest = (char*)lr->data + y*lr->pitch; |
| 137 |
for (x = 0; x < width; x++) { |
| 138 |
dest[x*4 ] = buf[x]; |
| 139 |
dest[x*4 + 1] = buf[x + width]; |
| 140 |
dest[x*4 + 2] = buf[x + width * 2]; |
| 141 |
dest[x*4 + 3] = 255; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (bpp == 8) { /* look for a 256 color palette */ |
| 147 |
while ((c = al_fgetc(f)) != EOF) { |
| 148 |
if (c == 12) { |
| 149 |
for (c = 0; c < 256; c++) { |
| 150 |
pal[c].r = al_fgetc(f); |
| 151 |
pal[c].g = al_fgetc(f); |
| 152 |
pal[c].b = al_fgetc(f); |
| 153 |
} |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
for (y = 0; y < height; y++) { |
| 158 |
char *dest = (char*)lr->data + y*lr->pitch; |
| 159 |
for (x = 0; x < width; x++) { |
| 160 |
int index = buf[y * width + x]; |
| 161 |
if (keep_index) { |
| 162 |
dest[x] = index; |
| 163 |
} |
| 164 |
else { |
| 165 |
dest[x*4 ] = pal[index].r; |
| 166 |
dest[x*4 + 1] = pal[index].g; |
| 167 |
dest[x*4 + 2] = pal[index].b; |
| 168 |
dest[x*4 + 3] = 255; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
al_unlock_bitmap(b); |
| 175 |
|
| 176 |
al_free(buf); |
| 177 |
|
| 178 |
if (al_get_errno()) { |
| 179 |
ALLEGRO_ERROR("Error detected: %d.\n", al_get_errno()); |
| 180 |
al_destroy_bitmap(b); |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
|
| 184 |
return b; |
| 185 |
} |
| 186 |
|
| 187 |
bool _al_save_pcx_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp) |
| 188 |
{ |
| 189 |
int c; |
| 190 |
int x, y; |
| 191 |
int i; |
| 192 |
int w, h; |
| 193 |
unsigned char *buf; |
| 194 |
ASSERT(f); |
| 195 |
ASSERT(bmp); |
| 196 |
|
| 197 |
al_set_errno(0); |
| 198 |
|
| 199 |
w = al_get_bitmap_width(bmp); |
| 200 |
h = al_get_bitmap_height(bmp); |
| 201 |
|
| 202 |
al_fputc(f, 10); /* manufacturer */ |
| 203 |
al_fputc(f, 5); /* version */ |
| 204 |
al_fputc(f, 1); /* run length encoding */ |
| 205 |
al_fputc(f, 8); /* 8 bits per pixel */ |
| 206 |
al_fwrite16le(f, 0); /* xmin */ |
| 207 |
al_fwrite16le(f, 0); /* ymin */ |
| 208 |
al_fwrite16le(f, w - 1); /* xmax */ |
| 209 |
al_fwrite16le(f, h - 1); /* ymax */ |
| 210 |
al_fwrite16le(f, 320); /* HDpi */ |
| 211 |
al_fwrite16le(f, 200); /* VDpi */ |
| 212 |
|
| 213 |
for (c = 0; c < 16 * 3; c++) { |
| 214 |
al_fputc(f, 0); |
| 215 |
} |
| 216 |
|
| 217 |
al_fputc(f, 0); /* reserved */ |
| 218 |
al_fputc(f, 3); /* color planes */ |
| 219 |
al_fwrite16le(f, w); /* number of bytes per scanline */ |
| 220 |
al_fwrite16le(f, 1); /* color palette */ |
| 221 |
al_fwrite16le(f, w); /* hscreen size */ |
| 222 |
al_fwrite16le(f, h); /* vscreen size */ |
| 223 |
for (c = 0; c < 54; c++) /* filler */ |
| 224 |
al_fputc(f, 0); |
| 225 |
|
| 226 |
buf = al_malloc(w * 3); |
| 227 |
|
| 228 |
al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY); |
| 229 |
|
| 230 |
for (y = 0; y < h; y++) { /* for each scanline... */ |
| 231 |
for (x = 0; x < w; x++) { |
| 232 |
ALLEGRO_COLOR c = al_get_pixel(bmp, x, y); |
| 233 |
unsigned char r, g, b; |
| 234 |
al_unmap_rgb(c, &r, &g, &b); |
| 235 |
buf[x] = r; |
| 236 |
buf[x + w] = g; |
| 237 |
buf[x + w * 2] = b; |
| 238 |
} |
| 239 |
|
| 240 |
for (i = 0; i < 3; i++) { |
| 241 |
int color; |
| 242 |
int count; |
| 243 |
x = 0; |
| 244 |
for (;;) { |
| 245 |
count = 0; |
| 246 |
color = buf[x + w * i]; |
| 247 |
do { |
| 248 |
count++; |
| 249 |
x++; |
| 250 |
} while ((count < 63) && (x < w) && (color == buf[x + w * i])); |
| 251 |
al_fputc(f, count | 0xC0); |
| 252 |
al_fputc(f, color); |
| 253 |
if (x >= w) |
| 254 |
break; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
al_free(buf); |
| 260 |
|
| 261 |
al_unlock_bitmap(bmp); |
| 262 |
|
| 263 |
if (al_get_errno()) { |
| 264 |
ALLEGRO_ERROR("Error detected: %d.\n", al_get_errno()); |
| 265 |
return false; |
| 266 |
} |
| 267 |
else |
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
ALLEGRO_BITMAP *_al_load_pcx(const char *filename, int flags) |
| 272 |
{ |
| 273 |
ALLEGRO_FILE *f; |
| 274 |
ALLEGRO_BITMAP *bmp; |
| 275 |
ASSERT(filename); |
| 276 |
|
| 277 |
f = al_fopen(filename, "rb"); |
| 278 |
if (!f) { |
| 279 |
ALLEGRO_ERROR("Unable to open %s for reading.\n", filename); |
| 280 |
return NULL; |
| 281 |
} |
| 282 |
|
| 283 |
bmp = _al_load_pcx_f(f, flags); |
| 284 |
|
| 285 |
al_fclose(f); |
| 286 |
|
| 287 |
return bmp; |
| 288 |
} |
| 289 |
|
| 290 |
bool _al_save_pcx(const char *filename, ALLEGRO_BITMAP *bmp) |
| 291 |
{ |
| 292 |
ALLEGRO_FILE *f; |
| 293 |
bool retsave; |
| 294 |
bool retclose; |
| 295 |
ASSERT(filename); |
| 296 |
|
| 297 |
f = al_fopen(filename, "wb"); |
| 298 |
if (!f) { |
| 299 |
ALLEGRO_ERROR("Unable to open %s for writing.\n", filename); |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
retsave = _al_save_pcx_f(f, bmp); |
| 304 |
|
| 305 |
retclose = al_fclose(f); |
| 306 |
|
| 307 |
return retsave && retclose; |
| 308 |
} |
| 309 |
|
| 310 |
bool _al_identify_pcx(ALLEGRO_FILE *f) |
| 311 |
{ |
| 312 |
uint8_t x[4]; |
| 313 |
al_fread(f, x, 4); |
| 314 |
|
| 315 |
if (x[0] != 0x0a) // PCX must start with 0x0a |
| 316 |
return false; |
| 317 |
if (x[1] == 1 || x[1] > 5) // version must be 0, 2, 3, 4, 5 |
| 318 |
return false; |
| 319 |
if (x[2] > 1) // compression must be 0 or 1 |
| 320 |
return false; |
| 321 |
if (x[3] != 8) // only 8-bit PCX files supported by Allegro! |
| 322 |
return false; |
| 323 |
return true; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
/* vim: set sts=3 sw=3 et: */ |