img.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* img: freestanding image decoding + RGB565 compositing.
  2. *
  3. * The library is deliberately independent of the OS and of the GUI toolkit:
  4. * it takes a memory buffer and an arena, and returns RGB565 pixels plus an
  5. * optional 8-bit alpha plane. That makes it usable by the image viewer now
  6. * and by a browser (or any other app) later, and lets it be tested on the
  7. * host.
  8. *
  9. * There is no malloc and no global mutable state. All scratch and output
  10. * memory comes from struct img_mem. The caller should take an img_mem_mark()
  11. * before a decode and rewind to it when img_decode() fails; a decoder cannot
  12. * always reclaim scratch allocated before its output image.
  13. */
  14. #ifndef IMG_H
  15. #define IMG_H
  16. #define IMG_FMT_UNKNOWN 0
  17. #define IMG_FMT_PNG 1
  18. #define IMG_FMT_JPEG 2
  19. #define IMG_FMT_GIF 3
  20. #define IMG_FMT_BMP 4
  21. #define IMG_FMT_TGA 5
  22. #define IMG_FMT_PNM 6
  23. #define IMG_FMT_ICO 7
  24. #define IMG_FLAG_ALPHA 1
  25. /* Errors are negative. */
  26. #define IMG_ERR_IO (-1)
  27. #define IMG_ERR_FORMAT (-2)
  28. #define IMG_ERR_CORRUPT (-3)
  29. #define IMG_ERR_NOMEM (-4)
  30. #define IMG_ERR_TOOBIG (-5)
  31. #define IMG_ERR_UNSUPPORTED (-6)
  32. #define IMG_ERR_EMPTY (-7)
  33. /* Whole-file limit used by the viewer's file buffer. */
  34. #define IMG_FILE_MAX (4u * 1024u * 1024u)
  35. struct img_info {
  36. unsigned int format;
  37. unsigned int width, height;
  38. unsigned int has_alpha;
  39. };
  40. struct img_options {
  41. /* 0 = no additional limit beyond the arena / sanity checks. */
  42. unsigned int max_pixels;
  43. };
  44. struct img_mem {
  45. unsigned char *base;
  46. unsigned int size;
  47. unsigned int used;
  48. };
  49. struct img_image {
  50. unsigned int format;
  51. unsigned int w, h;
  52. unsigned short *px; /* RGB565, w*h */
  53. unsigned char *alpha; /* w*h, or NULL when fully opaque */
  54. unsigned int flags;
  55. };
  56. struct img_blit {
  57. unsigned short *dst;
  58. int dst_w, dst_h;
  59. int clip_x, clip_y, clip_w, clip_h;
  60. int x, y; /* destination top-left of the image */
  61. unsigned int scale; /* 16.16, > 0 */
  62. unsigned int bg; /* RGB565 background for alpha */
  63. unsigned int bg2; /* second checker colour */
  64. unsigned int flags;
  65. };
  66. #define IMG_BLIT_CHECKER 1 /* alpha over a checkered background */
  67. #define IMG_BLIT_OVER_DST 2 /* alpha over the existing destination pixels */
  68. #define IMG_BLIT_FAST 4 /* nearest sampling while dragging */
  69. void img_mem_init(struct img_mem *m, void *base, unsigned int size);
  70. unsigned int img_mem_mark(const struct img_mem *m);
  71. int img_mem_rewind(struct img_mem *m, unsigned int mark);
  72. void *img_mem_alloc(struct img_mem *m, unsigned int bytes, unsigned int align);
  73. int img_sniff(const void *data, unsigned int len, struct img_info *out);
  74. int img_decode(const void *data, unsigned int len,
  75. const struct img_options *opt, struct img_mem *m,
  76. struct img_image *out);
  77. unsigned int img_estimate(const struct img_info *info);
  78. const char *img_format_name(unsigned int format);
  79. const char *img_error_string(int err);
  80. unsigned int img_fit_scale(unsigned int iw, unsigned int ih,
  81. unsigned int bw, unsigned int bh);
  82. void img_draw(const struct img_image *im, const struct img_blit *b);
  83. #endif