File: imagemessagehandler.cpp

package info (click to toggle)
povray 1%3A3.7.0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 147,232 kB
  • sloc: cpp: 845,011; ansic: 122,118; sh: 34,204; pascal: 6,420; asm: 3,355; ada: 1,681; makefile: 1,389; cs: 879; awk: 590; perl: 245; xml: 95
file content (253 lines) | stat: -rw-r--r-- 7,984 bytes parent folder | download | duplicates (4)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
/*******************************************************************************
 * imagemessagehandler.cpp
 *
 * ---------------------------------------------------------------------------
 * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
 * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
 *
 * POV-Ray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * POV-Ray 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------------
 * POV-Ray is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by David K. Buck.
 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
 * ---------------------------------------------------------------------------
 * $File: //depot/public/povray/3.x/source/frontend/imagemessagehandler.cpp $
 * $Revision: #1 $
 * $Change: 6069 $
 * $DateTime: 2013/11/06 11:59:40 $
 * $Author: chrisc $
 *******************************************************************************/

// configbase.h must always be the first POV file included within base *.cpp files
#include "base/configbase.h"
#include "base/types.h"

#include "frontend/configfrontend.h"
#include "frontend/renderfrontend.h"
#include "frontend/imagemessagehandler.h"

// this must be the last file included
#include "base/povdebug.h"

namespace pov_frontend
{

ImageMessageHandler::ImageMessageHandler()
{
}

ImageMessageHandler::~ImageMessageHandler()
{
}

void ImageMessageHandler::HandleMessage(const SceneData& sd, const ViewData& vd, POVMSType ident, POVMS_Object& msg)
{
	switch(ident)
	{
		case kPOVMsgIdent_PixelSet:
			DrawPixelSet(sd, vd, msg);
			break;
		case kPOVMsgIdent_PixelBlockSet:
			DrawPixelBlockSet(sd, vd, msg);
			break;
		case kPOVMsgIdent_PixelRowSet:
			DrawPixelRowSet(sd, vd, msg);
			break;
		case kPOVMsgIdent_RectangleFrameSet:
			DrawRectangleFrameSet(sd, vd, msg);
			break;
		case kPOVMsgIdent_FilledRectangleSet:
			DrawFilledRectangleSet(sd, vd, msg);
			break;
	}
}

void ImageMessageHandler::DrawPixelSet(const SceneData& sd, const ViewData& vd, POVMS_Object& msg)
{
	POVMS_Attribute pixelposattr;
	POVMS_Attribute pixelcolattr;
	unsigned int psize(msg.GetInt(kPOVAttrib_PixelSize));

	msg.Get(kPOVAttrib_PixelPositions, pixelposattr);
	msg.Get(kPOVAttrib_PixelColors, pixelcolattr);

	vector<POVMSInt> pixelpositions(pixelposattr.GetIntVector());
	vector<POVMSFloat> pixelcolors(pixelcolattr.GetFloatVector());

	if((pixelpositions.size() / 2) != (pixelcolors.size() / 5))
		throw POV_EXCEPTION(kInvalidDataSizeErr, "Number of pixel colors and pixel positions does not match!");

	GammaCurvePtr gamma;
	if (vd.display)
		gamma = vd.display->GetGamma();

	for(int i = 0, ii = 0; (i < pixelcolors.size()) && (ii < pixelpositions.size()); i += 5, ii += 2)
	{
		Colour col(pixelcolors[i], pixelcolors[i + 1], pixelcolors[i + 2], pixelcolors[i + 3], pixelcolors[i + 4]);
		Colour gcol(col);
		unsigned int x(pixelpositions[ii]);
		unsigned int y(pixelpositions[ii + 1]);
		Display::RGBA8 rgba;
		float dither = GetDitherOffset(x, y);

		if(vd.display)
		{
			// TODO ALPHA - display may profit from receiving the data in its original, premultiplied form
			// Premultiplied alpha was good for the math, but the display expects non-premultiplied alpha, so fix this if possible.
			float alpha = gcol.FTtoA();
			if (alpha != 1.0 && fabs(alpha) > 1e-6) // TODO FIXME - magic value
			{
				gcol.red()   /= alpha;
				gcol.green() /= alpha;
				gcol.blue()  /= alpha;
			}
		}

		rgba.red   = IntEncode(gamma, gcol.red(),   255, dither);
		rgba.green = IntEncode(gamma, gcol.green(), 255, dither);
		rgba.blue  = IntEncode(gamma, gcol.blue(),  255, dither);
		rgba.alpha = IntEncode(       gcol.FTtoA(), 255, dither);

		if(psize == 1)
		{
			if(vd.display)
				vd.display->DrawPixel(x, y, rgba);

			if((vd.image) && (x < vd.image->GetWidth()) && (y < vd.image->GetHeight()))
				vd.image->SetRGBAValue(x, y, col.red(), col.green(), col.blue(), col.FTtoA());
		}
		else
		{
			if(vd.display)
				vd.display->DrawFilledRectangle(x, y, x + psize - 1, y + psize - 1, rgba);

			if(vd.image)
			{
				for(unsigned int py = 0; (py < psize) && (y + py < vd.image->GetHeight()); py++)
				{
					for(unsigned int px = 0; (px < psize) && (x + px < vd.image->GetWidth()); px++)
						vd.image->SetRGBAValue(x + px, y + py, col.red(), col.green(), col.blue(), col.FTtoA());
				}
			}
		}
	}

	if(vd.imageBackup)
	{
		msg.Write(*vd.imageBackup);
		vd.imageBackup->flush();
	}
}

void ImageMessageHandler::DrawPixelBlockSet(const SceneData& sd, const ViewData& vd, POVMS_Object& msg)
{
	POVRect rect(msg.GetInt(kPOVAttrib_Left), msg.GetInt(kPOVAttrib_Top), msg.GetInt(kPOVAttrib_Right), msg.GetInt(kPOVAttrib_Bottom));
	POVMS_Attribute pixelattr;
	vector<Colour> cols;
	vector<Display::RGBA8> rgbas;
	unsigned int psize(msg.GetInt(kPOVAttrib_PixelSize));
	int i = 0;

	msg.Get(kPOVAttrib_PixelBlock, pixelattr);

	vector<POVMSFloat> pixelvector(pixelattr.GetFloatVector());

	cols.reserve(rect.GetArea());
	rgbas.reserve(rect.GetArea());

	GammaCurvePtr gamma;

	if (vd.display)
		gamma = vd.display->GetGamma();

	for(i = 0; i < rect.GetArea() *  5; i += 5)
	{
		Colour col(pixelvector[i], pixelvector[i + 1], pixelvector[i + 2], pixelvector[i + 3], pixelvector[i + 4]);
		Colour gcol(col);
		Display::RGBA8 rgba;
		unsigned int x(rect.left + (i/5) % rect.GetWidth());
		unsigned int y(rect.top  + (i/5) / rect.GetWidth());
		float dither = GetDitherOffset(x, y);

		if(vd.display)
		{
			// TODO ALPHA - display may profit from receiving the data in its original, premultiplied form
			// Premultiplied alpha was good for the math, but the display expects non-premultiplied alpha, so fix this if possible.
			float alpha = gcol.FTtoA();
			if (alpha != 1.0 && fabs(alpha) > 1e-6) // TODO FIXME - magic value
			{
				gcol.red()   /= alpha;
				gcol.green() /= alpha;
				gcol.blue()  /= alpha;
			}
		}

		rgba.red   = IntEncode(gamma, gcol.red(),   255, dither);
		rgba.green = IntEncode(gamma, gcol.green(), 255, dither);
		rgba.blue  = IntEncode(gamma, gcol.blue(),  255, dither);
		rgba.alpha = IntEncode(       gcol.FTtoA(), 255, dither);

		cols.push_back(col);
		rgbas.push_back(rgba);
	}

	if(vd.display)
	{
		if(psize == 1)
			vd.display->DrawPixelBlock(rect.left, rect.top, rect.right, rect.bottom, &rgbas[0]);
		else
		{
			for(unsigned int y = rect.top, i = 0; y <= rect.bottom; y += psize)
			{
				for(unsigned int x = rect.left; x <= rect.right; x += psize, i++)
					vd.display->DrawFilledRectangle(x, y, x + psize - 1, y + psize - 1, rgbas[0]);
			}
		}
	}

	if(vd.image)
	{
		for(unsigned int y = rect.top, i = 0; y <= rect.bottom; y += psize)
		{
			for(unsigned int x = rect.left; x <= rect.right; x += psize, i++)
			{
				for(unsigned int py = 0; py < psize; py++)
				{
					for(unsigned int px = 0; px < psize; px++)
						vd.image->SetRGBAValue(x + px, y + py, cols[i].red(), cols[i].green(), cols[i].blue(), cols[i].FTtoA());
				}
			}
		}
	}

	if(vd.imageBackup)
	{
		msg.Write(*vd.imageBackup);
		vd.imageBackup->flush();
	}
}

void ImageMessageHandler::DrawPixelRowSet(const SceneData& sd, const ViewData& vd, POVMS_Object& msg)
{
}

void ImageMessageHandler::DrawRectangleFrameSet(const SceneData& sd, const ViewData& vd, POVMS_Object& msg)
{
}

void ImageMessageHandler::DrawFilledRectangleSet(const SceneData& sd, const ViewData& vd, POVMS_Object& msg)
{
}

}