img_internal.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /* Header-only probes used by img_sniff(). Return IMG_FMT_* or UNKNOWN. */
  55. int img_sniff_png(const unsigned char *d, unsigned int n, struct img_info *out);
  56. int img_sniff_jpeg(const unsigned char *d, unsigned int n, struct img_info *out);
  57. int img_sniff_gif(const unsigned char *d, unsigned int n, struct img_info *out);
  58. int img_sniff_bmp(const unsigned char *d, unsigned int n, struct img_info *out);
  59. int img_sniff_tga(const unsigned char *d, unsigned int n, struct img_info *out);
  60. int img_sniff_pnm(const unsigned char *d, unsigned int n, struct img_info *out);
  61. /* True when an image of w*h pixels is acceptable for the given options. */
  62. int img_size_ok(unsigned int w, unsigned int h,
  63. const struct img_options *opt);
  64. #endif