bmp support

This commit is contained in:
RandomityGuy 2023-02-15 19:10:11 +05:30
parent c7da2c2de6
commit 70daee42f7
3 changed files with 153 additions and 48 deletions

View file

@ -329,31 +329,83 @@ class ResourceLoader {
public static function getTextureRealpath(path:String) {
if (zipFilesystem.exists(path.toLowerCase())) {
var img = new hxd.res.Image(zipFilesystem.get(path.toLowerCase()));
Image.setupTextureFlags = (texObj) -> {
texObj.flags.set(MipMapped);
}
var tex = img.toTexture();
tex.mipMap = Nearest;
var textureresource = new Resource(tex, path, textureCache, tex -> tex.dispose());
textureCache.set(path, textureresource);
if (StringTools.endsWith(path.toLowerCase(), ".bmp")) { // Handle bmp specially
var bmpContents = zipFilesystem.get(path.toLowerCase());
var bmpreader = new format.bmp.Reader(new haxe.io.BytesInput(bmpContents.getBytes()));
var bmpdata = bmpreader.read();
return textureresource;
var bbuf = new haxe.io.BytesBuffer();
var i = 0;
while (i < bmpdata.pixels.length) {
bbuf.addByte(bmpdata.pixels.get(i));
bbuf.addByte(bmpdata.pixels.get(i + 1));
bbuf.addByte(bmpdata.pixels.get(i + 2));
bbuf.addByte(1);
i += 3;
}
var pixs = new hxd.Pixels(bmpdata.header.width, bmpdata.header.height, bbuf.getBytes(), hxd.PixelFormat.BGRA);
var tex = h3d.mat.Texture.fromPixels(pixs);
tex.mipMap = Nearest;
tex.flags.set(MipMapped);
var textureresource = new Resource(tex, path, textureCache, tex -> tex.dispose());
textureCache.set(path, textureresource);
return textureresource;
} else {
var img = new hxd.res.Image(zipFilesystem.get(path.toLowerCase()));
Image.setupTextureFlags = (texObj) -> {
texObj.flags.set(MipMapped);
}
var tex = img.toTexture();
tex.mipMap = Nearest;
var textureresource = new Resource(tex, path, textureCache, tex -> tex.dispose());
textureCache.set(path, textureresource);
return textureresource;
}
}
if (textureCache.exists(path))
return textureCache.get(path);
if (fileSystem.exists(path)) {
var img = loader.load(path).toImage();
Image.setupTextureFlags = (texObj) -> {
texObj.flags.set(MipMapped);
}
var tex = img.toTexture();
tex.mipMap = Nearest;
// tex.filter = Nearest;
var textureresource = new Resource(tex, path, textureCache, tex -> tex.dispose());
textureCache.set(path, textureresource);
if (StringTools.endsWith(path.toLowerCase(), ".bmp")) { // Handle bmp specially
var bmpContents = zipFilesystem.get(path.toLowerCase());
var bmpreader = new format.bmp.Reader(new haxe.io.BytesInput(bmpContents.getBytes()));
var bmpdata = bmpreader.read();
return textureresource;
var bbuf = new haxe.io.BytesBuffer();
var i = 0;
while (i < bmpdata.pixels.length) {
bbuf.addByte(bmpdata.pixels.get(i));
bbuf.addByte(bmpdata.pixels.get(i + 1));
bbuf.addByte(bmpdata.pixels.get(i + 2));
bbuf.addByte(1);
i += 3;
}
var pixs = new hxd.Pixels(bmpdata.header.width, bmpdata.header.height, bbuf.getBytes(), hxd.PixelFormat.BGRA);
pixs.setFlip(true);
var tex = h3d.mat.Texture.fromPixels(pixs);
tex.mipMap = Nearest;
tex.flags.set(MipMapped);
var textureresource = new Resource(tex, path, textureCache, tex -> tex.dispose());
textureCache.set(path, textureresource);
return textureresource;
} else {
var img = loader.load(path).toImage();
Image.setupTextureFlags = (texObj) -> {
texObj.flags.set(MipMapped);
}
var tex = img.toTexture();
tex.mipMap = Nearest;
// tex.filter = Nearest;
var textureresource = new Resource(tex, path, textureCache, tex -> tex.dispose());
textureCache.set(path, textureresource);
return textureresource;
}
}
return null;
}

View file

@ -94,6 +94,7 @@ class Sky extends Object {
}
var worker = new ResourceLoaderWorker(() -> {
var fnames = [];
for (i in 0...6) {
var line = StringTools.trim(lines[i]);
var filenames = ResourceLoader.getFullNamesOf(dmlDirectory + '/' + line);
@ -102,10 +103,13 @@ class Sky extends Object {
skyboxImages.push(pixels);
// var tex = new h3d.mat.Texture();
// skyboxImages.push(new BitmapData(128, 128));
fnames.push("");
Console.error("Skybox image " + filenames[0] + " does not exist");
} else {
var image = ResourceLoader.getResource(filenames[0], ResourceLoader.getImage, this.imageResources).toBitmap();
var pixels = image.getPixels();
var pixels = ResourceLoader.getTexture(filenames[0]).resource.capturePixels(0, 0);
fnames.push(filenames[0]);
// var image = ResourceLoader.getResource(filenames[0], ResourceLoader.getImage, this.imageResources).toBitmap();
// var pixels = image.getPixels();
skyboxImages.push(pixels);
}
}
@ -118,16 +122,37 @@ class Sky extends Object {
maxwidth = texture.width;
}
Util.flipImage(skyboxImages[0], true, false);
Util.flipImage(skyboxImages[4], true, false);
// Handle the bmp files specially to flip y
if (StringTools.endsWith(fnames[0].toLowerCase(), ".bmp")) {
Util.flipImage(skyboxImages[0], true, true);
} else
Util.flipImage(skyboxImages[0], true, false);
if (StringTools.endsWith(fnames[4].toLowerCase(), ".bmp")) {
Util.flipImage(skyboxImages[4], true, true);
} else
Util.flipImage(skyboxImages[4], true, false);
Util.rotateImage(skyboxImages[5], Math.PI);
Util.flipImage(skyboxImages[5], true, false);
if (StringTools.endsWith(fnames[5].toLowerCase(), ".bmp")) {
Util.flipImage(skyboxImages[5], true, true);
} else
Util.flipImage(skyboxImages[5], true, false);
Util.rotateImage(skyboxImages[1], -Math.PI / 2);
Util.flipImage(skyboxImages[1], true, false);
if (StringTools.endsWith(fnames[1].toLowerCase(), ".bmp")) {
// Util.flipImage(skyboxImages[1], true, true);
} else
Util.flipImage(skyboxImages[1], true, false);
Util.rotateImage(skyboxImages[2], Math.PI);
Util.flipImage(skyboxImages[2], true, false);
if (StringTools.endsWith(fnames[2].toLowerCase(), ".bmp")) {
Util.flipImage(skyboxImages[2], true, true);
// Util.flipImage(skyboxImages[2], true, false);
} else
Util.flipImage(skyboxImages[2], true, false);
Util.rotateImage(skyboxImages[3], Math.PI / 2);
Util.flipImage(skyboxImages[3], true, false);
if (StringTools.endsWith(fnames[3].toLowerCase(), ".bmp")) {
// Util.flipImage(skyboxImages[3], true, false);
// Util.flipImage(skyboxImages[3], false, true);
} else
Util.flipImage(skyboxImages[3], true, false);
var cubemaptexture = new Texture(maxheight, maxwidth, [Cube]);
for (i in 0...6) {

View file

@ -124,10 +124,13 @@ class Util {
public static function flipImage(bitmap:hxd.Pixels, hflip:Bool, vflip:Bool) {
var curpixels = bitmap.clone();
if (hflip)
if (hflip && vflip) {
for (x in 0...curpixels.width) {
for (y in 0...curpixels.height) {
var psrc = ((curpixels.width - x - 1) + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + curpixels.offset;
var psrc = ((curpixels.width - x - 1)
+ (curpixels.width - y - 1) * curpixels.width) * @:privateAccess curpixels.bytesPerPixel
+ curpixels.offset;
var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
@ -147,29 +150,54 @@ class Util {
}
}
}
if (vflip)
for (x in 0...curpixels.width) {
for (y in 0...curpixels.height) {
var psrc = (x + (curpixels.width - y - 1) * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + curpixels.offset;
} else {
if (hflip)
for (x in 0...curpixels.width) {
for (y in 0...curpixels.height) {
var psrc = ((curpixels.width - x - 1) + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + curpixels.offset;
var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
switch (curpixels.format) {
case R8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
case BGRA | RGBA | ARGB:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2));
bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3));
case RG8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
default:
null;
switch (curpixels.format) {
case R8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
case BGRA | RGBA | ARGB:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2));
bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3));
case RG8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
default:
null;
}
}
}
}
if (vflip)
for (x in 0...curpixels.width) {
for (y in 0...curpixels.height) {
var psrc = (x + (curpixels.width - y - 1) * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + curpixels.offset;
var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
switch (curpixels.format) {
case R8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
case BGRA | RGBA | ARGB:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2));
bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3));
case RG8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
default:
null;
}
}
}
}
}
public static function splitIgnoreStringLiterals(str:String, splitter:String, strLiteralToken = '"') {