From eb8ca97878f44e040f14eb10a84c0f88bae30d9a Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 30 Dec 2014 10:38:32 +0300 Subject: [PATCH] add support of loading PNG images with de_image --- examples/example1/example1.visualdproj | 2 +- examples/helloworld/helloworld.visualdproj | 2 +- examples/tetris/tetris.visualdproj | 2 +- src/dlangui/graphics/drawbuf.d | 4 +++ src/dlangui/graphics/images.d | 40 ++++++++++++++-------- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/examples/example1/example1.visualdproj b/examples/example1/example1.visualdproj index f03b868b..84d680e3 100644 --- a/examples/example1/example1.visualdproj +++ b/examples/example1/example1.visualdproj @@ -47,7 +47,7 @@ 0 0 $(DMDInstallDir)windows\bin\dmd.exe - $(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../DerelictSDL2/source + $(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../DerelictSDL2/source $(SolutionDir)/../de_image/source/interfaces $(SolutionDir)/../de_image/source/png $(ConfigurationName) $(OutDir) diff --git a/examples/helloworld/helloworld.visualdproj b/examples/helloworld/helloworld.visualdproj index ce83160b..22d389df 100644 --- a/examples/helloworld/helloworld.visualdproj +++ b/examples/helloworld/helloworld.visualdproj @@ -47,7 +47,7 @@ 0 0 $(DMDInstallDir)windows\bin\dmd.exe - $(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../DerelictSDL2/source + $(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../DerelictSDL2/source $(SolutionDir)/../de_image/source/interfaces $(SolutionDir)/../de_image/source/png $(ConfigurationName) $(OutDir) diff --git a/examples/tetris/tetris.visualdproj b/examples/tetris/tetris.visualdproj index ea9451c0..d369a7af 100644 --- a/examples/tetris/tetris.visualdproj +++ b/examples/tetris/tetris.visualdproj @@ -47,7 +47,7 @@ 0 0 $(DMDInstallDir)windows\bin\dmd.exe - $(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../DerelictSDL2/source + $(SolutionDir)/src $(SolutionDir)/3rdparty $(SolutionDir)/3rdparty/libpng/source $(SolutionDir)/../DerelictGL3/source $(SolutionDir)/../DerelictUtil/source $(SolutionDir)/../DerelictFT/source $(SolutionDir)/../DerelictSDL2/source $(SolutionDir)/../de_image/source/interfaces $(SolutionDir)/../de_image/source/png $(ConfigurationName) $(OutDir) diff --git a/src/dlangui/graphics/drawbuf.d b/src/dlangui/graphics/drawbuf.d index 6e8fd19f..1e52c5a8 100644 --- a/src/dlangui/graphics/drawbuf.d +++ b/src/dlangui/graphics/drawbuf.d @@ -23,6 +23,10 @@ import dlangui.core.logger; immutable uint COLOR_TRANSFORM_OFFSET_NONE = 0x80808080; immutable uint COLOR_TRANSFORM_MULTIPLY_NONE = 0x40404040; +uint makeRGBA(T)(T r, T g, T b, T a) { + return (cast(uint)a << 24)|(cast(uint)r << 16)|(cast(uint)g << 8)|(cast(uint)b); +} + /// blend two RGB pixels using alpha uint blendARGB(uint dst, uint src, uint alpha) { uint dstalpha = dst >> 24; diff --git a/src/dlangui/graphics/images.d b/src/dlangui/graphics/images.d index 36d567e1..3348300a 100644 --- a/src/dlangui/graphics/images.d +++ b/src/dlangui/graphics/images.d @@ -20,15 +20,16 @@ Authors: Vadim Lopatin, coolreader.org@gmail.com */ module dlangui.graphics.images; -immutable bool USE_FREEIMAGE = true; +//immutable bool USE_FREEIMAGE = true; +//version = USE_FREEIMAGE; -//version = USE_DEIMAGE; +version = USE_DEIMAGE; version (USE_DEIMAGE) { - import devisualization.image.creation; - import devisualization.image.image; + import devisualization.image; + import devisualization.image.png; } import dlangui.core.logger; @@ -43,11 +44,20 @@ ColorDrawBuf loadImage(string filename) { version (USE_DEIMAGE) { try { Image image = imageFromFile(filename); - ColorDrawBuf buf = new ColorDrawBuf(image.width, image.height); + int w = cast(int)image.width; + int h = cast(int)image.height; + ColorDrawBuf buf = new ColorDrawBuf(w, h); Color_RGBA[] pixels = image.rgba.allPixels; - for (int y = 0; y < image.height; y++) { - // TODO: convert pixels + int index = 0; + for (int y = 0; y < h; y++) { + uint * dstLine = buf.scanLine(y); + for (int x = 0; x < w; x++) { + Color_RGBA * pixel = &pixels[index + x]; + dstLine[x] = makeRGBA(pixel.r_ubyte, pixel.g_ubyte, pixel.b_ubyte, pixel.a_ubyte); + } + index += w; } + //destroy(image); return buf; } catch (NotAnImageException e) { Log.e("Failed to load image from file ", filename, " using de_image"); @@ -68,13 +78,13 @@ ColorDrawBuf loadImage(string filename) { } -/// load and decode image from stream to ColorDrawBuf, returns null if loading or decoding is failed -ColorDrawBuf loadImage(InputStream stream) { - if (stream is null || !stream.isOpen) - return null; - static if (USE_FREEIMAGE) { - return loadFreeImage(stream); - } +version (USE_FREEIMAGE) { + /// load and decode image from stream to ColorDrawBuf, returns null if loading or decoding is failed + ColorDrawBuf loadImage(InputStream stream) { + if (stream is null || !stream.isOpen) + return null; + return loadFreeImage(stream); + } } class ImageDecodingException : Exception { @@ -88,7 +98,7 @@ shared static this() { //DerelictFI.load(); } -static if (USE_FREEIMAGE) { +version (USE_FREEIMAGE) { ColorDrawBuf loadFreeImage(InputStream stream) { import derelict.freeimage.freeimage;