URL: https://mrcoles.com/media/js/bitmap.js
/*!
* Generate Bitmap Data URL
* http://mrcoles.com/low-res-paint/
*
* Copyright 2010, Peter Coles
* Licensed under the MIT licenses.
* http://mrcoles.com/media/mit-license.txt
*
* Date: Tue Oct 26 00:00:00 2010 -0500
*/
/*
* Code to generate Bitmap images (using data urls) from rows of RGB arrays.
* Specifically for use with http://mrcoles.com/low-rest-paint/
*
* Research:
*
* RFC 2397 data URL
* http://www.xs4all.nl/~wrb/Articles/Article_IMG_RFC2397_P1_01.htm
*
* BMP file Format
* http://en.wikipedia.org/wiki/BMP_file_format#Example_of_a_2.C3.972_Pixel.2C_24-Bit_Bitmap_.28Windows_V3_DIB.29
*
* BMP Notes
*
* - Integer values are little-endian, including RGB pixels, e.g., (255, 0, 0) -> \x00\x00\xFF
* - Bitmap data starts at lower left (and reads across rows)
* - In the BMP data, padding bytes are inserted in order to keep the lines of data in multiples of four,
* e.g., a 24-bit bitmap with width 1 would have 3 bytes of data per row (R, G, B) + 1 byte of padding
*/
(function() {
function _asLittleEndianHex(value, bytes) {
// Convert value into little endian hex bytes
// value - the number as a decimal integer (representing bytes)
// bytes - the number of bytes that this value takes up in a string
// Example:
// _asLittleEndianHex(2835, 4)
// > '\x13\x0b\x00\x00'
var result = [];
for (; bytes>0; bytes--) {
result.push(String.fromCharCode(value & 255));
value >>= 8;
}
return result.join('');
}
function _collapseData(rows, row_padding) {
// Convert rows of RGB arrays into BMP data
var i,
rows_len = rows.length,
j,
pixels_len = rows_len ? rows[0].length : 0,
pixel,
padding = '',
result = [];
for (; row_padding > 0; row_padding--) {
padding += '\x00';
}
for (i=0; i