| 1 |
/* |
| 2 |
* Imaging tools for cacaview and img2irc |
| 3 |
* Copyright © 2002—2021 Sam Hocevar <[email protected]> |
| 4 |
* All Rights Reserved |
| 5 |
* |
| 6 |
* This program is free software. It comes without any warranty, to |
| 7 |
* the extent permitted by applicable law. You can redistribute it |
| 8 |
* and/or modify it under the terms of the Do What the Fuck You Want |
| 9 |
* to Public License, Version 2, as published by the WTFPL Task Force. |
| 10 |
* See http://www.wtfpl.net/ for more details. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "config.h" |
| 14 |
|
| 15 |
#if !defined(__KERNEL__) |
| 16 |
# include <string.h> |
| 17 |
# include <stdlib.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
#if defined(USE_IMLIB2) |
| 21 |
# include <Imlib2.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "caca.h" |
| 25 |
|
| 26 |
#include "common-image.h" |
| 27 |
|
| 28 |
#if !defined(USE_IMLIB2) |
| 29 |
static unsigned int u32fread(caca_file_t *); |
| 30 |
static unsigned int u16fread(caca_file_t *); |
| 31 |
static unsigned int u8fread(caca_file_t *); |
| 32 |
#endif |
| 33 |
|
| 34 |
struct image * load_image(char const * name) |
| 35 |
{ |
| 36 |
struct image * im = malloc(sizeof(struct image)); |
| 37 |
unsigned int rmask, gmask, bmask, amask; |
| 38 |
|
| 39 |
#if defined(USE_IMLIB2) |
| 40 |
Imlib_Image image; |
| 41 |
|
| 42 |
/* Load the new image */ |
| 43 |
image = imlib_load_image(name); |
| 44 |
|
| 45 |
if(!image) |
| 46 |
{ |
| 47 |
free(im); |
| 48 |
return NULL; |
| 49 |
} |
| 50 |
|
| 51 |
imlib_context_set_image(image); |
| 52 |
im->pixels = (char *)imlib_image_get_data_for_reading_only(); |
| 53 |
im->w = imlib_image_get_width(); |
| 54 |
im->h = imlib_image_get_height(); |
| 55 |
rmask = 0x00ff0000; |
| 56 |
gmask = 0x0000ff00; |
| 57 |
bmask = 0x000000ff; |
| 58 |
amask = 0xff000000; |
| 59 |
uint32_t bpp = 32; |
| 60 |
uint32_t depth = 4; |
| 61 |
|
| 62 |
/* Create the libcaca dither */ |
| 63 |
im->dither = caca_create_dither(bpp, im->w, im->h, depth * im->w, |
| 64 |
rmask, gmask, bmask, amask); |
| 65 |
if(!im->dither) |
| 66 |
{ |
| 67 |
imlib_free_image(); |
| 68 |
free(im); |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
im->priv = (void *)image; |
| 73 |
|
| 74 |
#else |
| 75 |
/* Try to load a BMP file */ |
| 76 |
uint32_t red[256], green[256], blue[256], alpha[256]; |
| 77 |
unsigned int tmp; |
| 78 |
|
| 79 |
caca_file_t *f = caca_file_open(name, "rb"); |
| 80 |
if (!f) |
| 81 |
{ |
| 82 |
free(im); |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
|
| 86 |
if (u16fread(f) != 0x4d42) |
| 87 |
{ |
| 88 |
caca_file_close(f); |
| 89 |
free(im); |
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
u32fread(f); /* size */ |
| 94 |
u16fread(f); /* reserved 1 */ |
| 95 |
u16fread(f); /* reserved 2 */ |
| 96 |
|
| 97 |
uint32_t offset = u32fread(f); |
| 98 |
uint32_t header_size = u32fread(f); /* header size */ |
| 99 |
|
| 100 |
im->w = u32fread(f); |
| 101 |
im->h = u32fread(f); |
| 102 |
uint32_t planes = u16fread(f); |
| 103 |
uint32_t bpp = u16fread(f); |
| 104 |
uint32_t colors = 0; |
| 105 |
|
| 106 |
/* Sanity check */ |
| 107 |
if (planes != 1) |
| 108 |
{ |
| 109 |
caca_file_close(f); |
| 110 |
free(im); |
| 111 |
return NULL; |
| 112 |
} |
| 113 |
|
| 114 |
if (header_size == 40) |
| 115 |
{ |
| 116 |
if (u32fread(f) != 0) /* compression */ |
| 117 |
{ |
| 118 |
caca_file_close(f); |
| 119 |
free(im); |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
u32fread(f); /* sizeimage */ |
| 124 |
u32fread(f); /* xpelspermeter */ |
| 125 |
u32fread(f); /* ypelspermeter */ |
| 126 |
u32fread(f); /* biclrused */ |
| 127 |
u32fread(f); /* biclrimportantn */ |
| 128 |
|
| 129 |
colors = (offset - 54) / 4; |
| 130 |
for (uint32_t i = 0; i < colors && i < 256; i++) |
| 131 |
{ |
| 132 |
blue[i] = u8fread(f) * 16; |
| 133 |
green[i] = u8fread(f) * 16; |
| 134 |
red[i] = u8fread(f) * 16; |
| 135 |
alpha[i] = 0; |
| 136 |
u8fread(f); |
| 137 |
} |
| 138 |
} |
| 139 |
else if (header_size == 12) |
| 140 |
{ |
| 141 |
colors = (offset - 26) / 3; |
| 142 |
for (uint32_t i = 0; i < colors && i < 256; i++) |
| 143 |
{ |
| 144 |
blue[i] = u8fread(f); |
| 145 |
green[i] = u8fread(f); |
| 146 |
red[i] = u8fread(f); |
| 147 |
alpha[i] = 0; |
| 148 |
} |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
caca_file_close(f); |
| 153 |
free(im); |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
|
| 157 |
/* Fill the rest of the palette */ |
| 158 |
for (uint32_t i = colors; i < 256; i++) |
| 159 |
blue[i] = green[i] = red[i] = alpha[i] = 0; |
| 160 |
|
| 161 |
uint32_t depth = (bpp + 7) / 8; |
| 162 |
|
| 163 |
/* Allocate the pixel buffer */ |
| 164 |
im->pixels = _caca_alloc2d(im->w, im->h, depth); |
| 165 |
if (!im->pixels) |
| 166 |
{ |
| 167 |
caca_file_close(f); |
| 168 |
free(im); |
| 169 |
return NULL; |
| 170 |
} |
| 171 |
|
| 172 |
memset(im->pixels, 0, im->w * im->h * depth); |
| 173 |
|
| 174 |
/* Read the bitmap data */ |
| 175 |
for (size_t y = im->h; y--; ) |
| 176 |
{ |
| 177 |
uint32_t bits = 0; |
| 178 |
|
| 179 |
switch (bpp) |
| 180 |
{ |
| 181 |
case 1: |
| 182 |
for (size_t x = 0; x < im->w; x++) |
| 183 |
{ |
| 184 |
size_t k = x % 32; |
| 185 |
if (k == 0) |
| 186 |
bits = u32fread(f); |
| 187 |
im->pixels[im->w * y * depth + x] = |
| 188 |
(bits >> ((k & ~0xf) + 0xf - (k & 0xf))) & 0x1; |
| 189 |
} |
| 190 |
break; |
| 191 |
case 4: |
| 192 |
for (size_t x = 0; x < im->w; x++) |
| 193 |
{ |
| 194 |
size_t k = x % 8; |
| 195 |
if (k == 0) |
| 196 |
bits = u32fread(f); |
| 197 |
im->pixels[im->w * y * depth + x] = |
| 198 |
(bits >> (4 * ((k & ~0x1) + 0x1 - (k & 0x1)))) & 0xf; |
| 199 |
} |
| 200 |
break; |
| 201 |
default: |
| 202 |
/* Works for 8bpp, but also for 16, 24 etc. */ |
| 203 |
caca_file_read(f, im->pixels + im->w * y * depth, |
| 204 |
im->w * depth); |
| 205 |
/* Pad reads to 4 bytes */ |
| 206 |
tmp = (im->w * depth) % 4; |
| 207 |
tmp = (4 - tmp) % 4; |
| 208 |
while (tmp--) |
| 209 |
u8fread(f); |
| 210 |
break; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
switch(depth) |
| 215 |
{ |
| 216 |
case 3: |
| 217 |
rmask = 0xff0000; |
| 218 |
gmask = 0x00ff00; |
| 219 |
bmask = 0x0000ff; |
| 220 |
amask = 0x000000; |
| 221 |
break; |
| 222 |
case 2: /* XXX: those are the 16 bits values */ |
| 223 |
rmask = 0x7c00; |
| 224 |
gmask = 0x03e0; |
| 225 |
bmask = 0x001f; |
| 226 |
amask = 0x0000; |
| 227 |
break; |
| 228 |
case 1: |
| 229 |
default: |
| 230 |
bpp = 8; |
| 231 |
rmask = gmask = bmask = amask = 0; |
| 232 |
break; |
| 233 |
} |
| 234 |
|
| 235 |
caca_file_close(f); |
| 236 |
|
| 237 |
/* Create the libcaca dither */ |
| 238 |
im->dither = caca_create_dither(bpp, im->w, im->h, depth * im->w, |
| 239 |
rmask, gmask, bmask, amask); |
| 240 |
if(!im->dither) |
| 241 |
{ |
| 242 |
free(im->pixels); |
| 243 |
free(im); |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
|
| 247 |
if (bpp == 8) |
| 248 |
caca_set_dither_palette(im->dither, red, green, blue, alpha); |
| 249 |
#endif |
| 250 |
|
| 251 |
return im; |
| 252 |
} |
| 253 |
|
| 254 |
void unload_image(struct image * im) |
| 255 |
{ |
| 256 |
#if defined(USE_IMLIB2) |
| 257 |
/* Imlib_Image image = (Imlib_Image)im->priv; */ |
| 258 |
imlib_free_image(); |
| 259 |
#else |
| 260 |
free(im->pixels); |
| 261 |
#endif |
| 262 |
caca_free_dither(im->dither); |
| 263 |
} |
| 264 |
|
| 265 |
#if !defined(USE_IMLIB2) |
| 266 |
static unsigned int u32fread(caca_file_t * f) |
| 267 |
{ |
| 268 |
uint8_t buffer[4] = { 0 }; |
| 269 |
caca_file_read(f, buffer, 4); |
| 270 |
return ((unsigned int)buffer[3] << 24) | ((unsigned int)buffer[2] << 16) |
| 271 |
| ((unsigned int)buffer[1] << 8) | ((unsigned int)buffer[0]); |
| 272 |
} |
| 273 |
|
| 274 |
static unsigned int u16fread(caca_file_t * f) |
| 275 |
{ |
| 276 |
uint8_t buffer[2] = { 0 }; |
| 277 |
caca_file_read(f, buffer, 2); |
| 278 |
return ((unsigned int)buffer[1] << 8) | ((unsigned int)buffer[0]); |
| 279 |
} |
| 280 |
|
| 281 |
static unsigned int u8fread(caca_file_t * f) |
| 282 |
{ |
| 283 |
uint8_t buffer[1] = { 0 }; |
| 284 |
caca_file_read(f, buffer, 1); |
| 285 |
return (unsigned int)buffer[0]; |
| 286 |
} |
| 287 |
#endif |
| 288 |
|