aboutsummaryrefslogtreecommitdiff
path: root/externals/g3dlite/G3D.lib/source/Image1uint8.cpp
blob: c43e7194d7d3f2f59dcafee6538939ab054f5dd6 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
  @file Image1uint8.cpp

  @maintainer Morgan McGuire, matrix@graphics3d.com

  @created 2007-01-31
  @edited  2008-01-13
*/

#include "G3D/Image1uint8.h"
#include "G3D/Image3uint8.h"
#include "G3D/Image1.h"
#include "G3D/GImage.h"
#include "G3D/Color1.h"
#include "G3D/Color1uint8.h"
#include "G3D/Color4.h"
#include "G3D/Color4uint8.h"
#include "G3D/ImageFormat.h"

namespace G3D {

Image1uint8::Image1uint8(int w, int h, WrapMode wrap) : Map2D<Color1uint8, Color1>(w, h, wrap) {
    setAll(Color1uint8(0));
}


Image1uint8::Ref Image1uint8::fromImage3uint8(const ReferenceCountedPointer<class Image3uint8>& im) {
    return fromArray(im->getCArray(), im->width(), im->height(), im->wrapMode());
}


Image1uint8::Ref Image1uint8::fromGImage(const GImage& im, WrapMode wrap) {
    switch (im.channels) {
    case 1:
        return fromArray(im.pixel1(), im.width, im.height, wrap);

    case 3:
        return fromArray(im.pixel3(), im.width, im.height, wrap);

    case 4:
        return fromArray(im.pixel4(), im.width, im.height, wrap);

    default:
        debugAssertM(false, "Input GImage must have 1, 3, or 4 channels.");
        return NULL;
    }
}


Image1uint8::Ref Image1uint8::fromImage1(const ReferenceCountedPointer<Image1>& im) {
    Ref out = createEmpty(static_cast<WrapMode>(im->wrapMode()));
    out->copyArray(im->getCArray(), im->width(), im->height());

    return out;
}


Image1uint8::Ref Image1uint8::createEmpty(int width, int height, WrapMode wrap) {
    return new Type(width, height, wrap);
}


Image1uint8::Ref Image1uint8::createEmpty(WrapMode wrap) {
    return createEmpty(0, 0, wrap);
}


Image1uint8::Ref Image1uint8::fromFile(const std::string& filename, WrapMode wrap, GImage::Format fmt) {
    Ref out = createEmpty(wrap);
    out->load(filename);
    return out;
}


Image1uint8::Ref Image1uint8::fromArray(const class Color3uint8* ptr, int w, int h, WrapMode wrap) {
    Ref out = createEmpty(wrap);
    out->copyArray(ptr, w, h);
    return out;
}


Image1uint8::Ref Image1uint8::fromArray(const class Color1* ptr, int w, int h, WrapMode wrap) {
    Ref out = createEmpty(wrap);
    out->copyArray(ptr, w, h);
    return out;
}


Image1uint8::Ref Image1uint8::fromArray(const class Color1uint8* ptr, int w, int h, WrapMode wrap) {
    Ref out = createEmpty(wrap);
    out->copyArray(ptr, w, h);
    return out;
}


Image1uint8::Ref Image1uint8::fromArray(const class Color3* ptr, int w, int h, WrapMode wrap) {
    Ref out = createEmpty(wrap);
    out->copyArray(ptr, w, h);
    return out;
}


Image1uint8::Ref Image1uint8::fromArray(const class Color4uint8* ptr, int w, int h, WrapMode wrap) {
    Ref out = createEmpty(wrap);
    out->copyArray(ptr, w, h);
    return out;
}


Image1uint8::Ref Image1uint8::fromArray(const class Color4* ptr, int w, int h, WrapMode wrap) {
    Ref out = createEmpty(wrap);
    out->copyArray(ptr, w, h);
    return out;
}


void Image1uint8::load(const std::string& filename, GImage::Format fmt) {
    copyGImage(GImage(filename, fmt));
    setChanged(true);
}


void Image1uint8::copyGImage(const GImage& im) {
    switch (im.channels) {
    case 1:
        copyArray(im.pixel1(), im.width, im.height);
        break;

    case 3:
        copyArray(im.pixel3(), im.width, im.height);
        break;

    case 4:
        copyArray(im.pixel4(), im.width, im.height);
        break;
    } 
}


void Image1uint8::copyArray(const Color3uint8* src, int w, int h) {
    resize(w, h);
    int N = w * h;

    Color1uint8* dst = getCArray();
    for (int i = 0; i < N; ++i) {
        dst[i].value = (src[i].r + src[i].g + src[i].b) / 3;
    }
}

void Image1uint8::copyArray(const Color3* src, int w, int h) {
    resize(w, h);
    int N = w * h;

    Color1uint8* dst = getCArray();
    for (int i = 0; i < N; ++i) {
        dst[i] = Color1uint8(Color1(src[i].average()));
    }
}


void Image1uint8::copyArray(const Color1uint8* ptr, int w, int h) {
    resize(w, h);
    System::memcpy(getCArray(), ptr, w * h);
}


void Image1uint8::copyArray(const Color1* src, int w, int h) {
    resize(w, h);
    int N = w * h;

    Color1uint8* dst = getCArray();
    for (int i = 0; i < N; ++i) {
        dst[i] = Color1uint8(src[i]);
    }
}


void Image1uint8::copyArray(const Color4uint8* ptr, int w, int h) {
    resize(w, h);
    int N = w * h;

    Color1uint8* dst = getCArray();
    for (int i = 0; i < N; ++i) {
        dst[i].value = (ptr[i].r + ptr[i].g + ptr[i].b) / 3;
    }
}


void Image1uint8::copyArray(const Color4* src, int w, int h) {
    resize(w, h);
    int N = w * h;

    Color1uint8* dst = getCArray();
    for (int i = 0; i < N; ++i) {
        dst[i] = Color1uint8(Color1(src[i].rgb().average()));
    }
}


/** Saves in any of the formats supported by G3D::GImage. */
void Image1uint8::save(const std::string& filename, GImage::Format fmt) {
    GImage im(width(), height(), 1);
    System::memcpy(im.byte(), getCArray(), width() * height());
    im.save(filename, fmt);
}


const ImageFormat* Image1uint8::format() const {
    return ImageFormat::L8();
}

} // G3D