img_internal.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Internal helpers shared by the image decoders. */
  2. #ifndef IMG_INTERNAL_H
  3. #define IMG_INTERNAL_H
  4. #include "img.h"
  5. static inline unsigned int img_be16(const unsigned char *p)
  6. {
  7. return ((unsigned int)p[0] << 8) | p[1];
  8. }
  9. static inline unsigned int img_be32(const unsigned char *p)
  10. {
  11. return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
  12. ((unsigned int)p[2] << 8) | p[3];
  13. }
  14. static inline unsigned int img_le16(const unsigned char *p)
  15. {
  16. return ((unsigned int)p[1] << 8) | p[0];
  17. }
  18. static inline unsigned int img_le32(const unsigned char *p)
  19. {
  20. return ((unsigned int)p[3] << 24) | ((unsigned int)p[2] << 16) |
  21. ((unsigned int)p[1] << 8) | p[0];
  22. }
  23. static inline unsigned short img_rgb565(unsigned int r, unsigned int g,
  24. unsigned int b)
  25. {
  26. if (r > 255) r = 255;
  27. if (g > 255) g = 255;
  28. if (b > 255) b = 255;
  29. return (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
  30. }
  31. /* Allocate the output image (and alpha plane when requested) from the arena.
  32. * Pixels are not cleared. */
  33. int img_alloc(struct img_mem *m, unsigned int w, unsigned int h,
  34. unsigned int format, int alpha, struct img_image *out);
  35. /* Decoder entry points (each re-validates its own header). */
  36. int img_decode_png(const unsigned char *d, unsigned int n,
  37. const struct img_options *opt, struct img_mem *m,
  38. struct img_image *out);
  39. int img_decode_jpeg(const unsigned char *d, unsigned int n,
  40. const struct img_options *opt, struct img_mem *m,
  41. struct img_image *out);
  42. int img_decode_gif(const unsigned char *d, unsigned int n,
  43. const struct img_options *opt, struct img_mem *m,
  44. struct img_image *out);
  45. int img_decode_bmp(const unsigned char *d, unsigned int n,
  46. const struct img_options *opt, struct img_mem *m,
  47. struct img_image *out);
  48. int img_decode_tga(const unsigned char *d, unsigned int n,
  49. const struct img_options *opt, struct img_mem *m,
  50. struct img_image *out);
  51. int img_decode_pnm(const unsigned char *d, unsigned int n,
  52. const struct img_options *opt, struct img_mem *m,
  53. struct img_image *out);
  54. int img_decode_ico(const unsigned char *d, unsigned int n,
  55. const struct img_options *opt, struct img_mem *m,
  56. struct img_image *out);
  57. int img_decode_pam(const unsigned char *d, unsigned int n,
  58. const struct img_options *opt, struct img_mem *m,
  59. struct img_image *out);
  60. /* Header-only probes used by img_sniff(). Return IMG_FMT_* or UNKNOWN. */
  61. int img_sniff_png(const unsigned char *d, unsigned int n, struct img_info *out);
  62. int img_sniff_jpeg(const unsigned char *d, unsigned int n, struct img_info *out);
  63. int img_sniff_gif(const unsigned char *d, unsigned int n, struct img_info *out);
  64. int img_sniff_bmp(const unsigned char *d, unsigned int n, struct img_info *out);
  65. int img_sniff_tga(const unsigned char *d, unsigned int n, struct img_info *out);
  66. int img_sniff_pnm(const unsigned char *d, unsigned int n, struct img_info *out);
  67. int img_sniff_ico(const unsigned char *d, unsigned int n, struct img_info *out);
  68. int img_sniff_pam(const unsigned char *d, unsigned int n, struct img_info *out);
  69. /* True when an image of w*h pixels is acceptable for the given options. */
  70. int img_size_ok(unsigned int w, unsigned int h,
  71. const struct img_options *opt);
  72. #endif