10a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson// Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 20a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson// Use of this source code is governed by a BSD-style license that can be 30a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson// found in the LICENSE file. 40a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 50a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson#include <stdint.h> 60a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson#include <string.h> 70a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 80a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson#include "bmpblk_header.h" 90a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson#include "bmpblk_font.h" 100a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson#include "image_types.h" 110a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 120a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson/* BMP header, used to validate image requirements 130a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson * See http://en.wikipedia.org/wiki/BMP_file_format 140a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson */ 150a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardsontypedef struct { 160a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint8_t CharB; // must be 'B' 170a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint8_t CharM; // must be 'M' 180a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t Size; 190a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint16_t Reserved[2]; 200a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t ImageOffset; 210a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t HeaderSize; 220a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t PixelWidth; 230a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t PixelHeight; 240a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint16_t Planes; // Must be 1 for x86 250a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint16_t BitPerPixel; // 1, 4, 8, or 24 for x86 260a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t CompressionType; // 0 (none) for x86, 1 (RLE) for arm 270a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t ImageSize; 280a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t XPixelsPerMeter; 290a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t YPixelsPerMeter; 300a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t NumberOfColors; 310a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson uint32_t ImportantColors; 320a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson} __attribute__((packed)) BMP_IMAGE_HEADER; 330a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 340a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 350a9977e161ce6ad6b11935dfb05ae840bf758078Bill RichardsonImageFormat identify_image_type(const void *buf, uint32_t bufsize, 360a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson ImageInfo *info) { 370a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 380a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson if (info) 390a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson info->format = FORMAT_INVALID; 400a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 410a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson if (bufsize < sizeof(BMP_IMAGE_HEADER) && 420a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson bufsize < sizeof(FontArrayHeader)) { 430a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson return FORMAT_INVALID; 440a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson } 450a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 460a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson const BMP_IMAGE_HEADER *bhdr = buf; 470a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson if (bhdr->CharB == 'B' && bhdr->CharM == 'M' && 480a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson bhdr->Planes == 1 && 490a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson (bhdr->CompressionType == 0 || bhdr->CompressionType == 1) && 500a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson (bhdr->BitPerPixel == 1 || bhdr->BitPerPixel == 4 || 510a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson bhdr->BitPerPixel == 8 || bhdr->BitPerPixel == 24)) { 520a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson if (info) { 530a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson info->format = FORMAT_BMP; 540a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson info->width = bhdr->PixelWidth; 550a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson info->height = bhdr->PixelHeight; 560a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson } 570a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson return FORMAT_BMP; 580a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson } 590a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 600a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson const FontArrayHeader *fhdr = buf; 610a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson if (0 == memcmp(&fhdr->signature, FONT_SIGNATURE, FONT_SIGNATURE_SIZE) && 620a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson fhdr->num_entries > 0) { 630a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson if (info) 640a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson info->format = FORMAT_FONT; 650a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson return FORMAT_FONT; 660a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson } 670a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 68209166af0d6e8e21f6c462ea19fbcfc6c933ea78Bill Richardson return FORMAT_INVALID; 690a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson} 700a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 710a9977e161ce6ad6b11935dfb05ae840bf758078Bill Richardson 72