File: Image.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (112 lines) | stat: -rw-r--r-- 2,140 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "stdafx.h"
#include "Image.h"

namespace storm {
	using namespace geometry;

	inline Color toColor(byte *src) {
		return Color(src[0], src[1], src[2], src[3]);
	}

	inline byte toByte(float f) {
		return byte(f * 255.0f);
	}

	inline void fromColor(const Color &src, byte *dest) {
		dest[0] = toByte(src.r);
		dest[1] = toByte(src.g);
		dest[2] = toByte(src.b);
		dest[3] = toByte(src.a);
	}

	Image::Image() : data(null), w(0), h(0) {}

	Image::Image(const Image &o) : data(null), w(o.w), h(o.h) {
		Nat s = w * h * 4;
		data = runtime::allocBuffer(engine(), s);
		memcpy(data->v, o.data->v, s);
	}

	Image::Image(Size size) : data(null), w(Nat(size.w)), h(Nat(size.h)) {
		Nat s = w * h * 4;
		data = runtime::allocBuffer(engine(), s);
	}

	Image::Image(Nat w, Nat h) : data(null), w(w), h(h) {
		Nat s = w * h * 4;
		data = runtime::allocBuffer(engine(), s);
	}

	geometry::Size Image::size() {
		return geometry::Size(Float(w), Float(h));
	}

	Nat Image::offset(Nat x, Nat y) {
		return (y * w + x) * 4;
	}

	Color Image::get(Nat x, Nat y) {
		if (x >= w || y >= h)
			return Color();
		return toColor(data->v + offset(x, y));
	}

	Color Image::get(Point p) {
		// TODO? Interpolate colors?
		return get(Nat(p.x), Nat(p.y));
	}

	void Image::set(Nat x, Nat y, Color c) {
		if (x >= w || y >= h)
			return;
		fromColor(c, data->v + offset(x, y));
	}

	void Image::set(Point p, Color c) {
		set(Nat(p.x), Nat(p.y), c);
	}

	Bool Image::hasAlpha() {
		for (Nat y = 0; y < h; y++) {
			for (Nat x = 0; x < w; x++) {
				byte *b = buffer(x, y);
				if (b[3] != 255)
					return true;
			}
		}

		return false;
	}

	Nat Image::stride() const {
		return w * 4;
	}

	Nat Image::bufferSize() const {
		return w * h * 4;
	}

	byte *Image::buffer() {
		return data->v;
	}

	byte *Image::buffer(Nat x, Nat y) {
		return data->v + offset(x, y);
	}

	void Image::vflip() {
		Nat halfHeight = h / 2;
		for (Nat y = 0; y < halfHeight; y++) {
			for (Nat x = 0; x < w; x++) {
				byte *a = buffer(x, y);
				byte *b = buffer(x, h - y - 1);

				swap(a[0], b[0]);
				swap(a[1], b[1]);
				swap(a[2], b[2]);
				swap(a[3], b[3]);
			}
		}
	}

}