Skip to content

Commit

Permalink
Merge pull request #31 from mike42/bugfix/30-bmp
Browse files Browse the repository at this point in the history
BMP file output corrections
  • Loading branch information
mike42 authored Jun 10, 2018
2 parents 2bd89a7 + f6dfacb commit c5d0e95
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/Mike42/GfxPhp/Codec/BmpCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,20 @@
class BmpCodec implements ImageEncoder
{
protected static $instance = null;
const INFO_HEADER_SIZE = 40;
const FILE_HEADER_SIZE = 14;

public function encode(RasterImage $image, string $format): string
{
if (!($image instanceof RgbRasterImage)) {
// Convert if necessary
$image = $image -> toRgb();
}
// Output uncompressed 24 bit BMP file
$header = pack(
"C2V3",
0x42,
0x4d, // 'BM' magic number
0, // File size
0, // Reserved
0
); // Offset
$width = $image -> getWidth();
$height = $image -> getHeight();
$infoHeader = pack(
"V3v2V6",
40,
self::INFO_HEADER_SIZE,
$width, // Width
$height, // Height
1, // Planes
Expand All @@ -38,8 +31,8 @@ public function encode(RasterImage $image, string $format): string
1, // Horizontal res
1, // Vertical res
0, // Number of colors
0
); // Number of important colors
0 // Number of important colors
);
$colorTable = "";
// Transform RGB ordering to BGR ordering
$pixels = str_split($image -> getRasterData(), 3);
Expand All @@ -51,13 +44,25 @@ public function encode(RasterImage $image, string $format): string
$padding = str_repeat("\x00", $paddingLength);
$lines = str_split($rasterData, $originalWidth);
$lines = array_reverse($lines, false);
// Uncompressed 24 bit BMP file
$pixelData = implode($padding, $lines) . $padding;
// Return bitmap & header
$fileSize = strlen($pixelData) + strlen($colorTable) + self::INFO_HEADER_SIZE + self::FILE_HEADER_SIZE;
$header = pack(
"C2Vv2V",
0x42, // 'BM' magic number
0x4d,
$fileSize, // file size
0, // Reserved
0, // Reserved
self::INFO_HEADER_SIZE + self::FILE_HEADER_SIZE // Offset
);
return $header . $infoHeader . $colorTable . $pixelData;
}

protected function transformRevString(&$item, $key)
{
// Convert RGB to BGR
$item = strrev($item);
}

Expand Down

0 comments on commit c5d0e95

Please sign in to comment.