1
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
*******************************************************************************
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6
* Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
8
* All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
10
* The copyright notice above does not evidence any actual of intended
11
* publication of such source code, and is an unpublished work by create3000.
12
* This material contains CONFIDENTIAL INFORMATION that is the property of
15
* No permission is granted to copy, distribute, or create derivative works from
16
* the contents of this software, in whole or in part, without the prior written
17
* permission of create3000.
19
* NON-MILITARY USE ONLY
21
* All create3000 software are effectively free software with a non-military use
22
* restriction. It is free. Well commented source is provided. You may reuse the
23
* source in any way you please with the exception anything that uses it must be
24
* marked to indicate is contains 'non-military use only' components.
26
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
28
* Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
30
* This file is part of the Cobweb Project.
32
* Cobweb is free software: you can redistribute it and/or modify it under the
33
* terms of the GNU General Public License version 3 only, as published by the
34
* Free Software Foundation.
36
* Cobweb is distributed in the hope that it will be useful, but WITHOUT ANY
37
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
38
* A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
39
* details (a copy is included in the LICENSE file that accompanied this code).
41
* You should have received a copy of the GNU General Public License version 3
42
* along with Cobweb. If not, see <http://www.gnu.org/licenses/gpl.html> for a
43
* copy of the GPLv3 License.
45
* For Silvio, Joy and Adi.
47
******************************************************************************/
53
"cobweb/Basic/X3DFieldDefinition",
54
"cobweb/Basic/FieldDefinitionArray",
55
"cobweb/Components/Texturing/X3DTexture2DNode",
56
"cobweb/Bits/X3DConstants",
57
"standard/Math/Algorithm",
58
"standard/Math/Numbers/Vector3",
59
"standard/Math/Numbers/Matrix3",
71
function PixelTexture (executionContext)
73
X3DTexture2DNode .call (this, executionContext);
75
this .addType (X3DConstants .PixelTexture);
77
this .addChildObjects ("loadState", new Fields .SFInt32 (X3DConstants .NOT_STARTED_STATE));
80
PixelTexture .prototype = $.extend (Object .create (X3DTexture2DNode .prototype),
82
constructor: PixelTexture,
83
fieldDefinitions: new FieldDefinitionArray ([
84
new X3DFieldDefinition (X3DConstants .inputOutput, "metadata", new Fields .SFNode ()),
85
new X3DFieldDefinition (X3DConstants .inputOutput, "image", new Fields .SFImage (0, 0, 0, [ ])),
86
new X3DFieldDefinition (X3DConstants .initializeOnly, "repeatS", new Fields .SFBool (true)),
87
new X3DFieldDefinition (X3DConstants .initializeOnly, "repeatT", new Fields .SFBool (true)),
88
new X3DFieldDefinition (X3DConstants .initializeOnly, "textureProperties", new Fields .SFNode ()),
90
getTypeName: function ()
92
return "PixelTexture";
94
getComponentName: function ()
98
getContainerField: function ()
102
initialize: function ()
104
X3DTexture2DNode .prototype .initialize .call (this);
106
this .image_ .addInterest ("set_image__", this);
108
this .set_image__ ();
110
checkLoadState: function ()
112
return this .loadState_ .getValue ();
114
convert: function (data, comp, array)
120
for (var i = 0, index = 0, length = array .length; i < length; ++ i, index += 4)
122
var pixel = array [i] .getValue ();
126
data [index + 2] = pixel & 255;
127
data [index + 3] = 255;
134
for (var i = 0, index = 0, length = array .length; i < length; ++ i, index += 4)
136
var pixel = array [i] .getValue ();
140
data [index + 2] = (pixel >>> 8) & 255;
141
data [index + 3] = pixel & 255;
148
for (var i = 0, index = 0, length = array .length; i < length; ++ i, index += 4)
150
var pixel = array [i] .getValue ();
152
data [index] = (pixel >>> 16) & 255;
153
data [index + 1] = (pixel >>> 8) & 255;
154
data [index + 2] = pixel & 255;
155
data [index + 3] = 255;
162
for (var i = 0, index = 0, length = array .length; i < length; ++ i, index += 4)
164
var pixel = array [i] .getValue ();
166
data [index] = (pixel >>> 24);
167
data [index + 1] = (pixel >>> 16) & 255;
168
data [index + 2] = (pixel >>> 8) & 255;
169
data [index + 3] = pixel & 255;
176
set_image__: function ()
179
width = this .image_ .width,
180
height = this .image_ .height,
181
comp = this .image_ .comp,
182
array = this .image_ .array .getValue (),
183
transparent = ! (comp % 2),
186
if (width > 0 && height > 0 && comp > 0 && comp < 5)
188
if (Algorithm .isPowerOfTwo (width) && Algorithm .isPowerOfTwo (height))
190
data = new Uint8Array (width * height * 4);
192
this .convert (data, comp, array);
194
else if (Math .max (width, height) < this .getBrowser () .getMinTextureSize () && ! this .textureProperties_ .getValue ())
196
data = new Uint8Array (width * height * 4);
198
this .convert (data, comp, array);
202
inputHeight = height;
204
width = Algorithm .nextPowerOfTwo (inputWidth) * 4;
205
height = Algorithm .nextPowerOfTwo (inputHeight) * 4;
207
data = this .resize (data, inputWidth, inputHeight, width, height);
212
canvas1 = $("<canvas></canvas>") [0],
213
canvas2 = $("<canvas></canvas>") [0],
214
cx1 = canvas1 .getContext("2d"),
215
cx2 = canvas2 .getContext("2d"),
216
imageData = cx1 .createImageData (width, height);
218
canvas1 .width = width;
219
canvas1 .height = height;
221
this .convert (imageData .data, comp, array);
222
cx1 .putImageData (imageData, 0, 0);
224
width = Algorithm .nextPowerOfTwo (width);
225
height = Algorithm .nextPowerOfTwo (height);
227
canvas2 .width = width;
228
canvas2 .height = height;
230
cx2 .drawImage (canvas1, 0, 0, canvas1 .width, canvas1 .height, 0, 0, width, height);
232
data = cx2 .getImageData (0, 0, width, height) .data;
235
this .setTexture (width, height, transparent, new Uint8Array (data), false);
236
this .loadState_ = X3DConstants .COMPLETE_STATE;
241
this .loadState_ = X3DConstants .FAILED_STATE;