mirror of https://github.com/buggins/dlangui.git
use Derelict/FreeImage instead of libpng to fix library version issues
This commit is contained in:
parent
a3d195fb8b
commit
81a71fab33
|
|
@ -190,13 +190,10 @@
|
||||||
</Config>
|
</Config>
|
||||||
<Folder name="dlangui">
|
<Folder name="dlangui">
|
||||||
<Folder name="3rdparty">
|
<Folder name="3rdparty">
|
||||||
<Folder name="DerelictUtil">
|
<Folder name="DerelictFT">
|
||||||
<File path="..\DerelictUtil\source\derelict\util\exception.d" />
|
<File path="..\DerelictFT\source\derelict\freetype\ft.d" />
|
||||||
<File path="..\DerelictUtil\source\derelict\util\loader.d" />
|
<File path="..\DerelictFT\source\derelict\freetype\functions.d" />
|
||||||
<File path="..\DerelictUtil\source\derelict\util\sharedlib.d" />
|
<File path="..\DerelictFT\source\derelict\freetype\types.d" />
|
||||||
<File path="..\DerelictUtil\source\derelict\util\system.d" />
|
|
||||||
<File path="..\DerelictUtil\source\derelict\util\wintypes.d" />
|
|
||||||
<File path="..\DerelictUtil\source\derelict\util\xtypes.d" />
|
|
||||||
</Folder>
|
</Folder>
|
||||||
<Folder name="DerelictGL3">
|
<Folder name="DerelictGL3">
|
||||||
<File path="..\DerelictGL3\source\derelict\opengl3\arb.d" />
|
<File path="..\DerelictGL3\source\derelict\opengl3\arb.d" />
|
||||||
|
|
@ -215,10 +212,18 @@
|
||||||
<File path="..\DerelictGL3\source\derelict\opengl3\wgl.d" />
|
<File path="..\DerelictGL3\source\derelict\opengl3\wgl.d" />
|
||||||
<File path="..\DerelictGL3\source\derelict\opengl3\wglext.d" />
|
<File path="..\DerelictGL3\source\derelict\opengl3\wglext.d" />
|
||||||
</Folder>
|
</Folder>
|
||||||
<Folder name="DerelictFT">
|
<Folder name="DerelictUtil">
|
||||||
<File path="..\DerelictFT\source\derelict\freetype\ft.d" />
|
<File path="..\DerelictUtil\source\derelict\util\exception.d" />
|
||||||
<File path="..\DerelictFT\source\derelict\freetype\functions.d" />
|
<File path="..\DerelictUtil\source\derelict\util\loader.d" />
|
||||||
<File path="..\DerelictFT\source\derelict\freetype\types.d" />
|
<File path="..\DerelictUtil\source\derelict\util\sharedlib.d" />
|
||||||
|
<File path="..\DerelictUtil\source\derelict\util\system.d" />
|
||||||
|
<File path="..\DerelictUtil\source\derelict\util\wintypes.d" />
|
||||||
|
<File path="..\DerelictUtil\source\derelict\util\xtypes.d" />
|
||||||
|
</Folder>
|
||||||
|
<Folder name="DerelictFI">
|
||||||
|
<File path="..\DerelictFI\source\derelict\freeimage\freeimage.d" />
|
||||||
|
<File path="..\DerelictFI\source\derelict\freeimage\functions.d" />
|
||||||
|
<File path="..\DerelictFI\source\derelict\freeimage\types.d" />
|
||||||
</Folder>
|
</Folder>
|
||||||
<Folder name="gl3n">
|
<Folder name="gl3n">
|
||||||
<File path="..\gl3n\gl3n\aabb.d" />
|
<File path="..\gl3n\gl3n\aabb.d" />
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
|
|
@ -216,9 +216,12 @@ ColorDrawBuf loadImage(string filename) {
|
||||||
ColorDrawBuf loadImage(InputStream stream) {
|
ColorDrawBuf loadImage(InputStream stream) {
|
||||||
if (stream is null || !stream.isOpen)
|
if (stream is null || !stream.isOpen)
|
||||||
return null;
|
return null;
|
||||||
// TODO: support more image types
|
static if (USE_FREEIMAGE) {
|
||||||
|
return loadFreeImage(stream);
|
||||||
|
} else static if (USE_LIBPNG) {
|
||||||
return loadPngImage(stream);
|
return loadPngImage(stream);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class ImageDecodingException : Exception {
|
class ImageDecodingException : Exception {
|
||||||
this(string msg) {
|
this(string msg) {
|
||||||
|
|
@ -226,6 +229,100 @@ class ImageDecodingException : Exception {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
immutable bool USE_LIBPNG = false;
|
||||||
|
immutable bool USE_FREEIMAGE = true;
|
||||||
|
|
||||||
|
shared static this() {
|
||||||
|
//import derelict.freeimage.freeimage;
|
||||||
|
//DerelictFI.load();
|
||||||
|
}
|
||||||
|
|
||||||
|
static if (USE_FREEIMAGE) {
|
||||||
|
ColorDrawBuf loadFreeImage(InputStream stream) {
|
||||||
|
import derelict.freeimage.freeimage;
|
||||||
|
|
||||||
|
static bool FREE_IMAGE_LOADED;
|
||||||
|
if (!FREE_IMAGE_LOADED) {
|
||||||
|
DerelictFI.load();
|
||||||
|
FREE_IMAGE_LOADED = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ubyte imagebuf[];
|
||||||
|
ubyte readbuf[4096];
|
||||||
|
for (;;) {
|
||||||
|
size_t bytesRead = stream.read(readbuf);
|
||||||
|
if (!bytesRead)
|
||||||
|
break;
|
||||||
|
imagebuf ~= readbuf[0..bytesRead];
|
||||||
|
}
|
||||||
|
//pointer to the image, once loaded
|
||||||
|
FIBITMAP *dib = null; //image format
|
||||||
|
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
|
||||||
|
// attach the binary data to a memory stream
|
||||||
|
FIMEMORY *hmem = FreeImage_OpenMemory(imagebuf.ptr, imagebuf.length);
|
||||||
|
fif = FreeImage_GetFileTypeFromMemory(hmem);
|
||||||
|
//check that the plugin has reading capabilities and load the file
|
||||||
|
if(!FreeImage_FIFSupportsReading(fif)) {
|
||||||
|
FreeImage_CloseMemory(hmem);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// load an image from the memory stream
|
||||||
|
dib = FreeImage_LoadFromMemory(fif, hmem, 0);
|
||||||
|
|
||||||
|
//if the image failed to load, return failure
|
||||||
|
if (!dib) {
|
||||||
|
Log.e("Failed to decode image");
|
||||||
|
FreeImage_CloseMemory(hmem);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
//retrieve the image data
|
||||||
|
ubyte * data = cast(ubyte*)FreeImage_GetBits(dib);
|
||||||
|
//get the image width and height, and size per pixel
|
||||||
|
int width = FreeImage_GetWidth(dib);
|
||||||
|
int height = FreeImage_GetHeight(dib);
|
||||||
|
int pixelSize = FreeImage_GetBPP(dib)/8;
|
||||||
|
int size = width*height*pixelSize;
|
||||||
|
|
||||||
|
ColorDrawBuf res = new ColorDrawBuf(width, height);
|
||||||
|
|
||||||
|
//swap R and B and invert image while copying
|
||||||
|
ubyte* src;
|
||||||
|
uint* dst;
|
||||||
|
uint r, g, b, a;
|
||||||
|
for( int i = 0, ii = height-1; i < height ; ++i, --ii ) {
|
||||||
|
dst = res.scanLine(i);
|
||||||
|
src = data + (ii * width) * pixelSize;
|
||||||
|
for( int j = 0; j < width; ++j, ++dst, src += pixelSize ) {
|
||||||
|
a = 0;
|
||||||
|
switch (pixelSize) {
|
||||||
|
case 4:
|
||||||
|
a = src[3] ^ 255;
|
||||||
|
case 3:
|
||||||
|
r = src[2];
|
||||||
|
g = src[1];
|
||||||
|
b = src[0];
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
// todo: do something better
|
||||||
|
r = g = src[1];
|
||||||
|
b = src[0];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
case 1:
|
||||||
|
r = g = b = src[0];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
dst[0] = (a << 24) | (r << 16) | (g << 8) | b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FreeImage_CloseMemory(hmem);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static if (USE_LIBPNG) {
|
||||||
|
|
||||||
extern (C) void lvpng_error_func (png_structp png, png_const_charp msg)
|
extern (C) void lvpng_error_func (png_structp png, png_const_charp msg)
|
||||||
{
|
{
|
||||||
string s = fromStringz(msg);
|
string s = fromStringz(msg);
|
||||||
|
|
@ -333,3 +430,4 @@ ColorDrawBuf loadPngImage(InputStream stream)
|
||||||
//return( !png_sig_cmp((unsigned char *)buf, (png_size_t)0, 4) );
|
//return( !png_sig_cmp((unsigned char *)buf, (png_size_t)0, 4) );
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue