blob: 8fcd94ed66e00e811ae9561244925359f1b26bc9 [file] [log] [blame]
Bill Richardson0a9977e2011-08-22 16:03:59 -07001// 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
5#include <stdint.h>
6#include <string.h>
7
8#include "bmpblk_header.h"
9#include "bmpblk_font.h"
10#include "image_types.h"
11
12/* BMP header, used to validate image requirements
13 * See http://en.wikipedia.org/wiki/BMP_file_format
14 */
15typedef struct {
16 uint8_t CharB; // must be 'B'
17 uint8_t CharM; // must be 'M'
18 uint32_t Size;
19 uint16_t Reserved[2];
20 uint32_t ImageOffset;
21 uint32_t HeaderSize;
22 uint32_t PixelWidth;
23 uint32_t PixelHeight;
24 uint16_t Planes; // Must be 1 for x86
25 uint16_t BitPerPixel; // 1, 4, 8, or 24 for x86
26 uint32_t CompressionType; // 0 (none) for x86, 1 (RLE) for arm
27 uint32_t ImageSize;
28 uint32_t XPixelsPerMeter;
29 uint32_t YPixelsPerMeter;
30 uint32_t NumberOfColors;
31 uint32_t ImportantColors;
32} __attribute__((packed)) BMP_IMAGE_HEADER;
33
34
35ImageFormat identify_image_type(const void *buf, uint32_t bufsize,
36 ImageInfo *info) {
37
38 if (info)
39 info->format = FORMAT_INVALID;
40
41 if (bufsize < sizeof(BMP_IMAGE_HEADER) &&
42 bufsize < sizeof(FontArrayHeader)) {
43 return FORMAT_INVALID;
44 }
45
46 const BMP_IMAGE_HEADER *bhdr = buf;
47 if (bhdr->CharB == 'B' && bhdr->CharM == 'M' &&
48 bhdr->Planes == 1 &&
49 (bhdr->CompressionType == 0 || bhdr->CompressionType == 1) &&
50 (bhdr->BitPerPixel == 1 || bhdr->BitPerPixel == 4 ||
51 bhdr->BitPerPixel == 8 || bhdr->BitPerPixel == 24)) {
52 if (info) {
53 info->format = FORMAT_BMP;
54 info->width = bhdr->PixelWidth;
55 info->height = bhdr->PixelHeight;
56 }
57 return FORMAT_BMP;
58 }
59
60 const FontArrayHeader *fhdr = buf;
61 if (0 == memcmp(&fhdr->signature, FONT_SIGNATURE, FONT_SIGNATURE_SIZE) &&
62 fhdr->num_entries > 0) {
63 if (info)
64 info->format = FORMAT_FONT;
65 return FORMAT_FONT;
66 }
67
Bill Richardson209166a2011-09-08 10:14:14 -070068 return FORMAT_INVALID;
Bill Richardson0a9977e2011-08-22 16:03:59 -070069}
70
71