Implement PixmapBlueprints

This commit is contained in:
Elias Batek 2024-10-12 23:00:39 +02:00
parent 7d39086857
commit 31a247c4c9
2 changed files with 89 additions and 4 deletions

View File

@ -151,6 +151,61 @@ static assert(Pixel.sizeof == uint.sizeof);
}
}
/++
Meta data for the construction a Pixmap
+/
struct PixmapBlueprint {
/++
Total number of pixels stored in a Pixmap.
+/
size_t length;
/++
Width of a Pixmap.
+/
int width;
@safe pure nothrow @nogc:
///
public static PixmapBlueprint fromSize(const Size size) {
return PixmapBlueprint(
size.area,
size.width,
);
}
///
public static PixmapBlueprint fromPixmap(const Pixmap pixmap) {
return PixmapBlueprint(
pixmap.length,
pixmap.width,
);
}
/++
Determines whether the blueprint is plausible.
+/
bool isValid() const {
return ((length % width) == 0);
}
/++
Height of a Pixmap.
See_also:
This is the counterpart to the dimension known as [width].
+/
int height() const {
return castTo!int(length / width);
}
///
Size size() const {
return Size(width, height);
}
}
/++
Pixel data container
+/
@ -165,11 +220,13 @@ struct Pixmap {
@safe pure nothrow:
///
deprecated("Do `Pixmap.makeNew(size)` instead.")
this(Size size) {
this.size = size;
}
///
deprecated("Do `Pixmap.makeNew(Size(width, height))` instead.")
this(int width, int height)
in (width > 0)
in (height > 0) {
@ -183,6 +240,17 @@ struct Pixmap {
this.width = width;
}
///
static Pixmap makeNew(PixmapBlueprint blueprint) {
auto data = new Pixel[](blueprint.length);
return Pixmap(data, blueprint.width);
}
///
static Pixmap makeNew(Size size) {
return Pixmap.makeNew(PixmapBlueprint.fromSize(size));
}
/++
Creates a $(I deep clone) of the Pixmap
+/
@ -215,7 +283,7 @@ struct Pixmap {
---
)
+/
Pixmap copyTo(Pixmap target) const {
Pixmap copyTo(Pixmap target) @nogc const {
// Length adjustment
const l = this.length;
if (target.data.length < l) {
@ -229,7 +297,7 @@ struct Pixmap {
return target;
}
private void copyToImpl(Pixmap target) const {
private void copyToImpl(Pixmap target) @nogc const {
target.data[] = this.data[];
}
@ -308,6 +376,23 @@ struct Pixmap {
return (width * int(Pixel.sizeof));
}
/++
Adjusts the Pixmap according to the provided blueprint.
The blueprint must not be larger than the data buffer of the pixmap.
This function does not reallocate the pixel data buffer.
If the blueprint is larger than the data buffer of the pixmap,
this will result in a bounds-check error if applicable.
+/
void adjustTo(PixmapBlueprint blueprint) {
debug assert(this.data.length >= blueprint.length);
debug assert(blueprint.isValid);
this.data = this.data[0 .. blueprint.length];
this.width = blueprint.width;
}
/++
Calculates the index (linear offset) of the requested position
within the pixmap data.
@ -461,7 +546,7 @@ struct SubPixmap {
Use [extractToPixmap] for a non-allocating variant with an .
+/
Pixmap extractToNewPixmap() const {
auto pm = Pixmap(size);
auto pm = Pixmap.makeNew(size);
this.extractToPixmap(pm);
return pm;
}

View File

@ -902,7 +902,7 @@ final class PixmapPresenter {
_renderer = renderer;
// create software framebuffer
auto framebuffer = Pixmap(config.renderer.resolution);
auto framebuffer = Pixmap.makeNew(config.renderer.resolution);
// OpenGL?
auto openGlOptions = OpenGlOptions.no;