img.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_FMT_PAM 8
  25. #define IMG_FLAG_ALPHA 1
  26. /* Errors are negative. */
  27. #define IMG_ERR_IO (-1)
  28. #define IMG_ERR_FORMAT (-2)
  29. #define IMG_ERR_CORRUPT (-3)
  30. #define IMG_ERR_NOMEM (-4)
  31. #define IMG_ERR_TOOBIG (-5)
  32. #define IMG_ERR_UNSUPPORTED (-6)
  33. #define IMG_ERR_EMPTY (-7)
  34. /* Whole-file limit used by the viewer's file buffer. */
  35. #define IMG_FILE_MAX (4u * 1024u * 1024u)
  36. struct img_info {
  37. unsigned int format;
  38. unsigned int width, height;
  39. unsigned int has_alpha;
  40. unsigned int progressive; /* JPEG only */
  41. unsigned int components; /* JPEG only */
  42. unsigned int max_h, max_v; /* JPEG sampling factors */
  43. };
  44. struct img_options {
  45. /* 0 = no additional limit beyond the arena / sanity checks. */
  46. unsigned int max_pixels;
  47. };
  48. struct img_mem {
  49. unsigned char *base;
  50. unsigned int size;
  51. unsigned int used;
  52. };
  53. struct img_image {
  54. unsigned int format;
  55. unsigned int w, h;
  56. unsigned short *px; /* RGB565, w*h */
  57. unsigned char *alpha; /* w*h, or NULL when fully opaque */
  58. unsigned int flags;
  59. };
  60. struct img_blit {
  61. unsigned short *dst;
  62. int dst_w, dst_h;
  63. int clip_x, clip_y, clip_w, clip_h;
  64. int x, y; /* destination top-left of the image */
  65. unsigned int scale; /* 16.16, > 0 */
  66. unsigned int bg; /* RGB565 background for alpha */
  67. unsigned int bg2; /* second checker colour */
  68. unsigned int flags;
  69. unsigned char *dst_a; /* optional destination alpha plane */
  70. };
  71. #define IMG_BLIT_CHECKER 1 /* alpha over a checkered background */
  72. #define IMG_BLIT_OVER_DST 2 /* alpha over the existing destination pixels */
  73. #define IMG_BLIT_FAST 4 /* nearest sampling while dragging */
  74. #define IMG_BLIT_SAVE_ALPHA 8 /* write source alpha to dst_a, keep RGB raw */
  75. void img_mem_init(struct img_mem *m, void *base, unsigned int size);
  76. unsigned int img_mem_mark(const struct img_mem *m);
  77. int img_mem_rewind(struct img_mem *m, unsigned int mark);
  78. void *img_mem_alloc(struct img_mem *m, unsigned int bytes, unsigned int align);
  79. int img_sniff(const void *data, unsigned int len, struct img_info *out);
  80. int img_decode(const void *data, unsigned int len,
  81. const struct img_options *opt, struct img_mem *m,
  82. struct img_image *out);
  83. /* Animated GIF: count frames / decode one composited frame. The first call
  84. * with an empty out image allocates it; later calls may reuse the same image. */
  85. int img_gif_frames(const void *data, unsigned int len, unsigned int *frames,
  86. unsigned int *first_delay_ms, int *has_alpha);
  87. int img_decode_gif_frame(const void *data, unsigned int len,
  88. unsigned int frame, struct img_mem *m,
  89. struct img_image *out, unsigned int *delay_ms);
  90. unsigned int img_estimate(const struct img_info *info);
  91. const char *img_format_name(unsigned int format);
  92. const char *img_error_string(int err);
  93. unsigned int img_fit_scale(unsigned int iw, unsigned int ih,
  94. unsigned int bw, unsigned int bh);
  95. void img_draw(const struct img_image *im, const struct img_blit *b);
  96. #endif