File: bitmap.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (240 lines) | stat: -rw-r--r-- 7,885 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ags/shared/gfx/bitmap.h"
#include "ags/shared/util/memory.h"

namespace AGS3 {
namespace AGS {
namespace Shared {

// TODO: revise this construction later
namespace BitmapHelper {

Bitmap *CreateBitmap(int width, int height, int color_depth) {
	Bitmap *bitmap = new Bitmap();
	if (!bitmap->Create(width, height, color_depth)) {
		delete bitmap;
		bitmap = nullptr;
	}
	return bitmap;
}

Bitmap *CreateClearBitmap(int width, int height, int color_depth, int clear_color) {
	Bitmap *bitmap = new Bitmap();
	if (!bitmap->Create(width, height, color_depth)) {
		delete bitmap;
		return nullptr;
	}
	bitmap->Clear(clear_color);
	return bitmap;
}

Bitmap *CreateTransparentBitmap(int width, int height, int color_depth) {
	Bitmap *bitmap = new Bitmap();
	if (!bitmap->CreateTransparent(width, height, color_depth)) {
		delete bitmap;
		bitmap = nullptr;
	}
	return bitmap;
}

Bitmap *CreateSubBitmap(Bitmap *src, const Rect &rc) {
	Bitmap *bitmap = new Bitmap();
	if (!bitmap->CreateSubBitmap(src, rc)) {
		delete bitmap;
		bitmap = nullptr;
	}
	return bitmap;
}

Bitmap *CreateBitmapCopy(Bitmap *src, int color_depth) {
	Bitmap *bitmap = new Bitmap();
	if (!bitmap->CreateCopy(src, color_depth)) {
		delete bitmap;
		bitmap = nullptr;
	}
	return bitmap;
}

Bitmap *LoadFromFile(const char *filename) {
	Bitmap *bitmap = new Bitmap();
	if (!bitmap->LoadFromFile(filename)) {
		delete bitmap;
		bitmap = nullptr;
	}
	return bitmap;
}

Bitmap *LoadFromFile(PACKFILE *pf) {
	Bitmap *bitmap = new Bitmap();
	if (!bitmap->LoadFromFile(pf)) {
		delete bitmap;
		bitmap = nullptr;
	}
	return bitmap;
}

Bitmap *AdjustBitmapSize(Bitmap *src, int width, int height) {
	int oldw = src->GetWidth(), oldh = src->GetHeight();
	if ((oldw == width) && (oldh == height))
		return src;
	Bitmap *bmp = BitmapHelper::CreateBitmap(width, height, src->GetColorDepth());
	bmp->StretchBlt(src, RectWH(0, 0, oldw, oldh), RectWH(0, 0, width, height));
	return bmp;
}

void MakeOpaque(Bitmap *bmp) {
	if (bmp->GetColorDepth() < 32)
		return; // no alpha channel

	for (int i = 0; i < bmp->GetHeight(); ++i) {
		uint32_t *line = reinterpret_cast<uint32_t *>(bmp->GetScanLineForWriting(i));
		uint32_t *line_end = line + bmp->GetWidth();
		for (uint32_t *px = line; px != line_end; ++px)
			*px = makeacol32(getr32(*px), getg32(*px), getb32(*px), 255);
	}
}

void MakeOpaqueSkipMask(Bitmap *bmp) {
	if (bmp->GetColorDepth() < 32)
		return; // no alpha channel

	for (int i = 0; i < bmp->GetHeight(); ++i) {
		uint32_t *line = reinterpret_cast<uint32_t *>(bmp->GetScanLineForWriting(i));
		uint32_t *line_end = line + bmp->GetWidth();
		for (uint32_t *px = line; px != line_end; ++px)
			if (*px != MASK_COLOR_32)
				*px = makeacol32(getr32(*px), getg32(*px), getb32(*px), 255);
	}
}

void ReplaceAlphaWithRGBMask(Bitmap *bmp) {
	if (bmp->GetColorDepth() < 32)
		return; // no alpha channel

	for (int i = 0; i < bmp->GetHeight(); ++i) {
		uint32_t *line = reinterpret_cast<uint32_t *>(bmp->GetScanLineForWriting(i));
		uint32_t *line_end = line + bmp->GetWidth();
		for (uint32_t *px = line; px != line_end; ++px)
			if (geta32(*px) == 0)
				*px = MASK_COLOR_32;
	}
}

// Functor that copies the "mask color" pixels from source to dest
template <class TPx, size_t BPP_>
struct PixelTransCpy {
	static const size_t BPP = BPP_;
	inline void operator ()(uint8_t *dst, const uint8_t *src, uint32_t mask_color, bool /*use_alpha*/) const {
		if (*(const TPx *)src == mask_color)
			*(TPx *)dst = mask_color;
	}
};

// Functor that tells to never skip a pixel in the mask
struct PixelNoSkip {
	inline bool operator ()(uint8_t * /*data*/, uint32_t /*mask_color*/, bool /*use_alpha*/) const {
		return false;
	}
};

typedef PixelTransCpy<uint8_t, 1> PixelTransCpy8;
typedef PixelTransCpy<uint16_t, 2> PixelTransCpy16;

// Functor that copies the "mask color" pixels from source to dest, 24-bit depth
struct PixelTransCpy24 {
	static const size_t BPP = 3;
	inline void operator ()(uint8_t *dst, const uint8_t *src, uint32_t mask_color, bool /*use_alpha*/) const {
		const uint8_t *mcol_ptr = (const uint8_t *)&mask_color;
		if (src[0] == mcol_ptr[0] && src[1] == mcol_ptr[1] && src[2] == mcol_ptr[2]) {
			dst[0] = mcol_ptr[0];
			dst[1] = mcol_ptr[1];
			dst[2] = mcol_ptr[2];
		}
	}
};

// Functor that copies the "mask color" pixels from source to dest, 32-bit depth, with alpha
struct PixelTransCpy32 {
	static const size_t BPP = 4;
	inline void operator ()(uint8_t *dst, const uint8_t *src, uint32_t mask_color, bool use_alpha) const {
		if (*(const uint32_t *)src == mask_color)
			*(uint32_t *)dst = mask_color;
		else if (use_alpha)
			dst[3] = src[3]; // copy alpha channel
		else
			dst[3] = 0xFF; // set the alpha channel byte to opaque
	}
};

// Functor that tells to skip pixels if they match the mask color or have alpha = 0
struct PixelTransSkip32 {
	inline bool operator ()(uint8_t *data, uint32_t mask_color, bool use_alpha) const {
		return *(const uint32_t *)data == mask_color || (use_alpha && data[3] == 0);
	}
};

// Applies bitmap mask, using 2 functors:
// - one that tells whether to skip current pixel;
// - another that copies the color from src to dest
template <class FnPxProc, class FnSkip>
void ApplyMask(uint8_t *dst, const uint8_t *src, size_t pitch, size_t height,
	FnPxProc proc, FnSkip skip, uint32_t mask_color, bool dst_has_alpha, bool mask_has_alpha) {
	for (size_t y = 0; y < height; ++y) {
		for (size_t x = 0; x < pitch; x += FnPxProc::BPP, src += FnPxProc::BPP, dst += FnPxProc::BPP) {
			if (!skip(dst, mask_color, dst_has_alpha))
				proc(dst, src, mask_color, mask_has_alpha);
		}
	}
}

void CopyTransparency(Bitmap *dst, const Bitmap *mask, bool dst_has_alpha, bool mask_has_alpha) {
	color_t mask_color = mask->GetMaskColor();
	uint8_t *dst_ptr = dst->GetDataForWriting();
	const uint8_t *src_ptr = mask->GetData();
	const size_t bpp = mask->GetBPP();
	const size_t pitch = mask->GetLineLength();
	const size_t height = mask->GetHeight();

	if (bpp == 1)
		ApplyMask(dst_ptr, src_ptr, pitch, height, PixelTransCpy8(), PixelNoSkip(), mask_color, dst_has_alpha, mask_has_alpha);
	else if (bpp == 2)
		ApplyMask(dst_ptr, src_ptr, pitch, height, PixelTransCpy16(), PixelNoSkip(), mask_color, dst_has_alpha, mask_has_alpha);
	else if (bpp == 3)
		ApplyMask(dst_ptr, src_ptr, pitch, height, PixelTransCpy24(), PixelNoSkip(), mask_color, dst_has_alpha, mask_has_alpha);
	else
		ApplyMask(dst_ptr, src_ptr, pitch, height, PixelTransCpy32(), PixelTransSkip32(), mask_color, dst_has_alpha, mask_has_alpha);
}

void ReadPixelsFromMemory(Bitmap *dst, const uint8_t *src_buffer, const size_t src_pitch, const size_t src_px_offset) {
	const size_t bpp = dst->GetBPP();
	const size_t src_px_pitch = src_pitch / bpp;
	if (src_px_offset >= src_px_pitch)
		return; // nothing to copy
	Memory::BlockCopy(dst->GetDataForWriting(), dst->GetLineLength(), 0, src_buffer, src_pitch, src_px_offset * bpp, dst->GetHeight());
}

} // namespace BitmapHelper

} // namespace Shared
} // namespace AGS
} // namespace AGS3