clone function for images

This commit is contained in:
Adam D. Ruppe 2018-02-14 10:22:16 -05:00
parent b2a1ed86ae
commit c1317f932c
1 changed files with 19 additions and 0 deletions

19
color.d
View File

@ -823,6 +823,9 @@ interface MemoryImage {
/// Set image pixel.
void setPixel(int x, int y, in Color clr);
/// Returns a copy of the image
MemoryImage clone() const;
/// Load image from file. This will import arsd.image to do the actual work, and cost nothing if you don't use it.
static MemoryImage fromImage(T : const(char)[]) (T filename) @trusted {
static if (__traits(compiles, (){import arsd.image;})) {
@ -857,6 +860,15 @@ class IndexedImage : MemoryImage {
return _height;
}
/// .
override IndexedImage clone() const {
auto n = new IndexedImage(width, height);
n.data[] = this.data[]; // the data member is already there, so array copy
n.palette = this.palette.dup; // and here we need to allocate too, so dup
n.hasAlpha = this.hasAlpha;
return n;
}
override Color getPixel(int x, int y) const @trusted {
if (x >= 0 && y >= 0 && x < _width && y < _height) {
uint pos = y*_width+x;
@ -976,6 +988,13 @@ class TrueColorImage : MemoryImage {
int _width;
int _height;
/// .
override TrueColorImage clone() const {
auto n = new TrueColorImage(width, height);
n.imageData.bytes[] = this.imageData.bytes[]; // copy into existing array ctor allocated
return n;
}
/// .
override int width() const { return _width; }
///.