From 202bd9fe6a5b6500b474cafa8f73fa1e17e5a8a8 Mon Sep 17 00:00:00 2001 From: Elias Batek Date: Sun, 26 May 2024 18:28:55 +0200 Subject: [PATCH 1/3] Add Gamut integration Ref: --- pixmappaint.d | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/pixmappaint.d b/pixmappaint.d index 48ad2db..10a76b4 100644 --- a/pixmappaint.d +++ b/pixmappaint.d @@ -280,9 +280,95 @@ private struct OriginRectangle { } } +// ==== Gamut integration ==== + +/++ + Reports whether the integration functionality for + the $(B Gamut) image decoding/encoding library by $(I AuburnSounds) + is available. + + Compile-time setting that is automatically enabled + if importing `Image` from module `gamut` succeeds. + + See_Also: + + +/ +enum bool hasGamutIntegration = __traits(compiles, () { import gamut : GamutImage = Image; }); + +//static if (hasGamutIntegration) { +// pragma(msg, "arsd.pixmappaint: Gamut integration enabled"); +//} + +/++ + This means that something went wrong using the $(I Gamut) integration of Pixmap Paint. ++/ +class GamutIntegrationException : ArsdExceptionBase { + this(string operation, string file = __FILE__, size_t line = __LINE__, Throwable next = null) @trusted { + super(operation, file, line, next); + } +} + +static if (hasGamutIntegration) { + import gamut : GamutImage = Image; + + /++ + Creates a Pixmap copying the pixel data from the provided [GamutImage]. + +/ + bool toPixmap(ref GamutImage source, out Pixmap result) @trusted nothrow + in (source.isValid) { + import gamut; + + enum supportedLayout = (LAYOUT_GAPLESS | LAYOUT_VERT_STRAIGHT); + enum supportedFormat = PixelType.rgba8; + + immutable bool needsConversion = ( + (source.layoutConstraints != supportedLayout) + || (source.type != supportedFormat) + ); + + if (needsConversion) { + source = source.clone(); + + immutable conversionSuccessful = source.convertTo(supportedFormat, supportedLayout); + if (!conversionSuccessful) { + return false; + } + } + + Pixel[] data = source.allPixelsAtOnce() + .castTo!(void[]) + .castTo!(Pixel[]) + .dup; + + result = Pixmap(data, source.width); + return true; + } + + /++ + Creates a Pixmap copying the pixel data from the provided [GamutImage]. + +/ + Pixmap toPixmap(ref GamutImage source) @safe { + if (source.isError) { + import std.format : format; + + throw new GamutIntegrationException(format!"Invalid source image. Gamut reports: \"%s\""(source.errorMessage)); + } + + Pixmap result; + const success = toPixmap(source, result); + + if (!success) { + throw new GamutIntegrationException("Failed to convert `gamut`.`Image` to a supported format."); + } + + return result; + } +} + @safe pure nothrow @nogc: -// misc +// ==== Misc functions ==== + private { Point pos(Rectangle r) => r.upperLeft; From 6c9fa81e6860b6147f97c2a7a682e7a78eb1de27 Mon Sep 17 00:00:00 2001 From: Elias Batek Date: Sun, 26 May 2024 18:32:29 +0200 Subject: [PATCH 2/3] Rename image import functions of pixmappaint's arsd.color integration --- pixmappaint.d | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/pixmappaint.d b/pixmappaint.d index 10a76b4..99db70d 100644 --- a/pixmappaint.d +++ b/pixmappaint.d @@ -135,26 +135,6 @@ struct Pixmap { this.width = width; } - static { - /++ - Creates a Pixmap wrapping the pixel data from the provided `TrueColorImage`. - - Interoperability function: `arsd.color` - +/ - Pixmap fromTrueColorImage(TrueColorImage source) @nogc { - return Pixmap(source.imageData.colors, source.width); - } - - /++ - Creates a Pixmap wrapping the pixel data from the provided `MemoryImage`. - - Interoperability function: `arsd.color` - +/ - Pixmap fromMemoryImage(MemoryImage source) { - return fromTrueColorImage(source.getAsTrueColorImage()); - } - } - @safe pure nothrow @nogc: /// Height of the buffer, i.e. the number of lines @@ -280,6 +260,24 @@ private struct OriginRectangle { } } +/++ + Creates a Pixmap wrapping the pixel data of the provided `TrueColorImage`. + + Interoperability function: `arsd.color` + +/ +Pixmap toPixmap(TrueColorImage source) @nogc { + return Pixmap(source.imageData.colors, source.width); +} + +/++ + Creates a Pixmap wrapping the pixel data from the provided `MemoryImage`. + + Interoperability function: `arsd.color` + +/ +Pixmap toPixmap(MemoryImage source) { + return source.getAsTrueColorImage().toPixmap(); +} + // ==== Gamut integration ==== /++ From 2a6044643c8966737c3d0712c32411644f732544 Mon Sep 17 00:00:00 2001 From: Elias Batek Date: Sun, 26 May 2024 18:40:15 +0200 Subject: [PATCH 3/3] Add example to doc comment of Gamut integration --- pixmappaint.d | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pixmappaint.d b/pixmappaint.d index 99db70d..c2370ae 100644 --- a/pixmappaint.d +++ b/pixmappaint.d @@ -311,6 +311,16 @@ static if (hasGamutIntegration) { /++ Creates a Pixmap copying the pixel data from the provided [GamutImage]. + + --- + // Load image using Gamut + import gamut; + Image img; + img.loadFromFile("dunes.jpg"); + + // Convert to Pixmap + Pixmap pixmap = img.toPixmap(); + --- +/ bool toPixmap(ref GamutImage source, out Pixmap result) @trusted nothrow in (source.isValid) {