refactor ByteStream out of iso-engine Bmp.hx

This commit is contained in:
2022-06-09 18:27:00 +00:00
parent 988c837fc7
commit e6239bf93b
2 changed files with 62 additions and 56 deletions

View File

@@ -0,0 +1,38 @@
package kiss;
import sys.io.File;
import haxe.io.Bytes;
class ByteStream {
var bytes = Bytes.alloc(0);
var position = 0;
function new () {}
public static function fromFile(file) {
var s = new ByteStream();
s.bytes = File.getBytes(file);
return s;
}
public function readByte():Int {
return bytes.get(position++);
}
public function readInt32():Int {
var int = bytes.getInt32(position);
position += 4;
return int;
}
public function readUInt32():Int {
var int = readInt32();
return if (int < 0) cast (4294967296 + cast int) else int;
}
public function readUInt16():Int {
var int = bytes.getUInt16(position);
position += 2;
return int;
}
}

View File

@@ -1,82 +1,50 @@
package; package;
import sys.io.File; import kiss.ByteStream;
import haxe.io.Bytes;
import flash.display.BitmapData; import flash.display.BitmapData;
import flixel.util.FlxColor; import flixel.util.FlxColor;
typedef ByteStream = {
bytes:Bytes,
position:Int
};
class Bmp { class Bmp {
static function getByteStream(file:String):ByteStream {
return {
bytes: File.getBytes(file),
position:0
};
}
static function readByte(bytes:ByteStream):Int {
return bytes.bytes.get(bytes.position++);
}
static function readInt32(bytes:ByteStream):Int {
var int = bytes.bytes.getInt32(bytes.position);
bytes.position += 4;
return int;
}
static function readUInt32(bytes:ByteStream):Int {
var int = readInt32(bytes);
return if (int < 0) cast (4294967296 + cast int) else int;
}
static function readUInt16(bytes:ByteStream):Int {
var int = bytes.bytes.getUInt16(bytes.position);
bytes.position += 2;
return int;
}
static function checkHeader(bytes:ByteStream) { static function checkHeader(bytes:ByteStream) {
// 42 4D // 42 4D
if (readByte(bytes) != 66 || readByte(bytes) != 77) if (bytes.readByte() != 66 || bytes.readByte() != 77)
throw 'Bad bmp header!'; throw 'Bad bmp header!';
} }
public static function loadBitmapData(bmpFile:String) { public static function loadBitmapData(bmpFile:String) {
var stream = getByteStream(bmpFile); var stream = ByteStream.fromFile(bmpFile);
checkHeader(stream); checkHeader(stream);
var fileSize = readUInt32(stream); var fileSize = stream.readUInt32();
var fileReserved = readUInt32(stream); var fileReserved = stream.readUInt32();
var fileOffBits = readUInt32(stream); var fileOffBits = stream.readUInt32();
var imgSize = readUInt32(stream); var imgSize = stream.readUInt32();
var imgWidth = readInt32(stream); var imgWidth = stream.readInt32();
var imgHeight = readInt32(stream); var imgHeight = stream.readInt32();
var planes = readUInt16(stream); var planes = stream.readUInt16();
var bitsPerPixel = readUInt16(stream); var bitsPerPixel = stream.readUInt16();
var compression = readUInt32(stream); var compression = stream.readUInt32();
var sizeImage = readUInt32(stream); var sizeImage = stream.readUInt32();
var xPixelsPerMeter = readInt32(stream); var xPixelsPerMeter = stream.readInt32();
var yPixelsPerMeter = readInt32(stream); var yPixelsPerMeter = stream.readInt32();
var colorsUsed = readUInt32(stream); var colorsUsed = stream.readUInt32();
var colorsImportant = readUInt32(stream); var colorsImportant = stream.readUInt32();
var colors:Array<FlxColor> = []; var colors:Array<FlxColor> = [];
for (c in 0...colorsUsed) { for (c in 0...colorsUsed) {
var blue = readByte(stream); var blue = stream.readByte();
var green = readByte(stream); var green = stream.readByte();
var red = readByte(stream); var red = stream.readByte();
var _ = readByte(stream); var _ = stream.readByte();
colors.push(FlxColor.fromRGB(red, green, blue)); colors.push(FlxColor.fromRGB(red, green, blue));
} }
@@ -88,11 +56,11 @@ class Bmp {
var rowPadding:Int = cast (sizeImage - (realHeight * imgWidth)) / realHeight; var rowPadding:Int = cast (sizeImage - (realHeight * imgWidth)) / realHeight;
for (_ in 0... realHeight) { for (_ in 0... realHeight) {
for (x in 0... imgWidth) { for (x in 0... imgWidth) {
var colorIdx = readByte(stream); var colorIdx = stream.readByte();
data.setPixel(x, realHeight-1-y, colors[colorIdx]); data.setPixel(x, realHeight-1-y, colors[colorIdx]);
} }
for (_ in 0... rowPadding) { for (_ in 0... rowPadding) {
readByte(stream); stream.readByte();
} }
y += dy; y += dy;