image_types.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include "bmpblk_header.h"
  7. #include "bmpblk_font.h"
  8. #include "image_types.h"
  9. /* BMP header, used to validate image requirements
  10. * See http://en.wikipedia.org/wiki/BMP_file_format
  11. */
  12. typedef struct {
  13. uint8_t CharB; // must be 'B'
  14. uint8_t CharM; // must be 'M'
  15. uint32_t Size;
  16. uint16_t Reserved[2];
  17. uint32_t ImageOffset;
  18. uint32_t HeaderSize;
  19. uint32_t PixelWidth;
  20. uint32_t PixelHeight;
  21. uint16_t Planes; // Must be 1 for x86
  22. uint16_t BitPerPixel; // 1, 4, 8, or 24 for x86
  23. uint32_t CompressionType; // 0 (none) for x86, 1 (RLE) for arm
  24. uint32_t ImageSize;
  25. uint32_t XPixelsPerMeter;
  26. uint32_t YPixelsPerMeter;
  27. uint32_t NumberOfColors;
  28. uint32_t ImportantColors;
  29. } __attribute__((packed)) BMP_IMAGE_HEADER;
  30. ImageFormat identify_image_type(const void *buf, uint32_t bufsize,
  31. ImageInfo *info) {
  32. if (info)
  33. info->format = FORMAT_INVALID;
  34. if (bufsize < sizeof(BMP_IMAGE_HEADER) &&
  35. bufsize < sizeof(FontArrayHeader)) {
  36. return FORMAT_INVALID;
  37. }
  38. const BMP_IMAGE_HEADER *bhdr = buf;
  39. if (bhdr->CharB == 'B' && bhdr->CharM == 'M' &&
  40. bhdr->Planes == 1 &&
  41. (bhdr->CompressionType == 0 || bhdr->CompressionType == 1) &&
  42. (bhdr->BitPerPixel == 1 || bhdr->BitPerPixel == 4 ||
  43. bhdr->BitPerPixel == 8 || bhdr->BitPerPixel == 24)) {
  44. if (info) {
  45. info->format = FORMAT_BMP;
  46. info->width = bhdr->PixelWidth;
  47. info->height = bhdr->PixelHeight;
  48. }
  49. return FORMAT_BMP;
  50. }
  51. const FontArrayHeader *fhdr = buf;
  52. if (0 == memcmp(&fhdr->signature, FONT_SIGNATURE, FONT_SIGNATURE_SIZE) &&
  53. fhdr->num_entries > 0) {
  54. if (info)
  55. info->format = FORMAT_FONT;
  56. return FORMAT_FONT;
  57. }
  58. return FORMAT_INVALID;
  59. }