mirror of https://github.com/adamdruppe/arsd.git
ketmar patch for getPixel. inefficient but convenient
This commit is contained in:
parent
c6f83abe14
commit
c0868ad46a
25
color.d
25
color.d
|
@ -764,6 +764,9 @@ interface MemoryImage {
|
||||||
|
|
||||||
/// Image height, in pixels
|
/// Image height, in pixels
|
||||||
int height() const;
|
int height() const;
|
||||||
|
|
||||||
|
/// Get image pixel. Slow, but returns valid RGBA color (completely transparent for off-image pixels).
|
||||||
|
Color getPixel(int x, int y) const;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An image that consists of indexes into a color palette. Use getAsTrueColorImage() if you don't care about palettes
|
/// An image that consists of indexes into a color palette. Use getAsTrueColorImage() if you don't care about palettes
|
||||||
|
@ -785,6 +788,18 @@ class IndexedImage : MemoryImage {
|
||||||
return _height;
|
return _height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override Color getPixel(int x, int y) const @trusted {
|
||||||
|
if (x >= 0 && y >= 0 && x < _width && y < _height) {
|
||||||
|
uint pos = y*_width+x;
|
||||||
|
if (pos >= data.length) return Color(0, 0, 0, 0);
|
||||||
|
ubyte b = data.ptr[pos];
|
||||||
|
if (b >= palette.length) return Color(0, 0, 0, 0);
|
||||||
|
return palette.ptr[b];
|
||||||
|
} else {
|
||||||
|
return Color(0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private int _width;
|
private int _width;
|
||||||
private int _height;
|
private int _height;
|
||||||
|
|
||||||
|
@ -883,6 +898,16 @@ class TrueColorImage : MemoryImage {
|
||||||
///.
|
///.
|
||||||
override int height() const { return _height; }
|
override int height() const { return _height; }
|
||||||
|
|
||||||
|
override Color getPixel(int x, int y) const @trusted {
|
||||||
|
if (x >= 0 && y >= 0 && x < _width && y < _height) {
|
||||||
|
uint pos = y*_width+x;
|
||||||
|
if (pos >= imageData.bytes.length/4) return Color(0, 0, 0, 0);
|
||||||
|
return imageData.colors.ptr[pos];
|
||||||
|
} else {
|
||||||
|
return Color(0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// .
|
/// .
|
||||||
this(int w, int h) {
|
this(int w, int h) {
|
||||||
_width = w;
|
_width = w;
|
||||||
|
|
Loading…
Reference in New Issue