Skip to content

Instantly share code, notes, and snippets.

@romamik
Created January 11, 2017 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romamik/cefe4ad27ffd0da3b7b02985b12233fb to your computer and use it in GitHub Desktop.
Save romamik/cefe4ad27ffd0da3b7b02985b12233fb to your computer and use it in GitHub Desktop.
Create kha image from encoded (png, jpg, etc...) bytes
package;
import haxe.io.Bytes;
import kha.Image;
#if sys_kore
@:cppFileCode('
#include <Kore/Graphics/stb_image.h>
#ifndef INCLUDED_kha_graphics4_TextureFormat
#include <kha/graphics4/TextureFormat.h>
#endif
')
#end
class KhaImageFromEncodedBytes {
#if sys_kore
@:functionCode('
int width, height;
int comp;
Kore::u8 * imageData = stbi_load_from_memory((Kore::u8*)bytes->b->GetBase(), bytes.GetPtr()->length, &width, &height, &comp, 4);
if (imageData == nullptr) {
errorCallback(::String(stbi_failure_reason()));
}
else {
::kha::Image image = ::kha::Image_obj::create2(width, height, ::kha::graphics4::TextureFormat_obj::RGBA32, readable, false, 0, 0);
Kore::Texture* texture = image.GetPtr()->texture;
Kore::u8* texData = texture->lock();
int stride = texture->stride();
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
Kore::u8 a = imageData[(y * width + x) * 4 + 3];
float af = a / 255.0f;
Kore::u8 r = imageData[(y * width + x) * 4 + 0] * af;
Kore::u8 g = imageData[(y * width + x) * 4 + 1] * af;
Kore::u8 b = imageData[(y * width + x) * 4 + 2] * af;
#ifdef DIRECT3D
//BGRA
texData[y * stride + x * 4 + 0] = b;
texData[y * stride + x * 4 + 1] = g;
texData[y * stride + x * 4 + 2] = r;
texData[y * stride + x * 4 + 3] = a;
#else
//RGBA
texData[y * stride + x * 4 + 0] = r;
texData[y * stride + x * 4 + 1] = g;
texData[y * stride + x * 4 + 2] = b;
texData[y * stride + x * 4 + 3] = a;
#endif
}
}
texture->unlock();
stbi_image_free(imageData);
doneCallback(image);
}
')
#end
public static function create(bytes:Bytes, doneCallback:Image->Void, errorCallback:String->Void, readable:Bool = false):Void {
#if sys_flash
{
function handleError(e:flash.events.Event) errorCallback(e.toString());
var loader = new flash.display.Loader();
loader.contentLoaderInfo.addEventListener(flash.events.Event.COMPLETE, function(_) doneCallback(Image.fromBitmap(loader.content, readable)));
loader.contentLoaderInfo.addEventListener(flash.events.IOErrorEvent.IO_ERROR, handleError);
loader.contentLoaderInfo.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, handleError);
loader.contentLoaderInfo.addEventListener(flash.events.AsyncErrorEvent.ASYNC_ERROR, handleError);
loader.loadBytes(bytes.getData());
}
#elseif sys_html5
{
var dataUrl = "data:image;base64," + haxe.crypto.Base64.encode(bytes);
var imageElement = cast(js.Browser.document.createElement('img'), js.html.ImageElement);
imageElement.onload = function() doneCallback(Image.fromImage(imageElement, readable));
imageElement.onerror = function() errorCallback("Image was not created");
imageElement.src = dataUrl;
}
#elseif sys_kore
//see @:functionCode metadata
#else
#error
#end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment