You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

296 lines
6.8 KiB

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