/*
- * Copyright (c) 2010 ARM Limited
+ * Copyright (c) 2010, 2015 ARM Limited
* All rights reserved
*
* The license below extends only to copyright in the software and shall
*
* Authors: William Wang
* Ali Saidi
+ * Chris Emmons
+ * Andreas Sandberg
*/
+#include "base/bitmap.hh"
+
#include <cassert>
-#include "base/bitmap.hh"
#include "base/misc.hh"
// bitmap class ctor
-Bitmap::Bitmap(VideoConvert::Mode _mode, uint16_t w, uint16_t h, uint8_t *d)
- : mode(_mode), height(h), width(w), data(d),
- vc(mode, VideoConvert::rgb8888, width, height)
+Bitmap::Bitmap(const FrameBuffer *_fb)
+ : fb(*_fb)
{
}
-void
-Bitmap::write(std::ostream *bmp)
+Bitmap::~Bitmap()
{
- assert(data);
+}
- // For further information see: http://en.wikipedia.org/wiki/BMP_file_format
- Magic magic = {{'B','M'}};
- Header header = {sizeof(VideoConvert::Rgb8888) * width * height , 0, 0, 54};
- Info info = {sizeof(Info), width, height, 1,
- sizeof(VideoConvert::Rgb8888) * 8, 0,
- sizeof(VideoConvert::Rgb8888) * width * height, 1, 1, 0, 0};
+const Bitmap::CompleteV1Header
+Bitmap::getCompleteHeader() const
+{
+ const uint32_t pixel_array_size(sizeof(PixelType) * fb.area());
+ const uint32_t file_size(sizeof(CompleteV1Header) + pixel_array_size);
- bmp->write(reinterpret_cast<char*>(&magic), sizeof(magic));
- bmp->write(reinterpret_cast<char*>(&header), sizeof(header));
- bmp->write(reinterpret_cast<char*>(&info), sizeof(info));
+ const CompleteV1Header header = {
+ // File header
+ {
+ {'B','M'}, /* Magic */
+ file_size,
+ 0, 0, /* Reserved */
+ sizeof(CompleteV1Header) /* Offset to pixel array */
+ },
+ // Info/DIB header
+ {
+ sizeof(InfoHeaderV1),
+ fb.width(),
+ fb.height(),
+ 1, /* Color planes */
+ 32, /* Bits per pixel */
+ 0, /* No compression */
+ pixel_array_size, /* Image size in bytes */
+ 2835, /* x pixels per meter (assume 72 DPI) */
+ 2835, /* y pixels per meter (assume 72 DPI) */
+ 0, /* Colors in color table */
+ 0 /* Important color count (0 == all are important) */
+ }
+ };
+
+ return header;
+}
+
+void
+Bitmap::write(std::ostream &bmp) const
+{
+ const CompleteV1Header header(getCompleteHeader());
- uint8_t *tmp = vc.convert(data);
- uint32_t *tmp32 = (uint32_t*)tmp;
+ // 1. write the header
+ bmp.write(reinterpret_cast<const char *>(&header), sizeof(header));
+ // 2. write the bitmap data
// BMP start store data left to right starting with the bottom row
// so we need to do some creative flipping
- for (int i = height - 1; i >= 0; i--)
- for (int j = 0; j < width; j++)
- bmp->write((char*)&tmp32[i * width + j], sizeof(uint32_t));
+ std::vector<PixelType> line_buffer(fb.width());
+ for (int y = 0; y < fb.height(); ++y) {
+ for (unsigned x = 0; x < fb.width(); ++x)
+ line_buffer[x] = fb.pixel(x, fb.height() - y - 1);
- bmp->flush();
+ bmp.write(reinterpret_cast<const char *>(line_buffer.data()),
+ line_buffer.size() * sizeof(line_buffer[0]));
+ }
- delete [] tmp;
+ bmp.flush();
}