forked from eriksson/X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparserIMAGE.js
132 lines (104 loc) · 3.31 KB
/
parserIMAGE.js
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
/*
*
* xxxxxxx xxxxxxx
* x:::::x x:::::x
* x:::::x x:::::x
* x:::::xx:::::x
* x::::::::::x
* x::::::::x
* x::::::::x
* x::::::::::x
* x:::::xx:::::x
* x:::::x x:::::x
* x:::::x x:::::x
* THE xxxxxxx xxxxxxx TOOLKIT
*
* http://www.goXTK.com
*
* Copyright (c) 2012 The X Toolkit Developers <[email protected]>
*
* The X Toolkit (XTK) is licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
*
*
*
*/
// provides
goog.provide('X.parserIMAGE');
// requires
goog.require('X.event');
goog.require('X.parser');
/**
* Create a parser for images (PNG/JPG/JPEG/GIF).
*
* @constructor
* @extends X.parser
*/
X.parserIMAGE = function() {
//
// call the standard constructor of X.base
goog.base(this);
//
// class attributes
/**
* @inheritDoc
* @const
*/
this._classname = 'parserIMAGE';
};
// inherit from X.parser
goog.inherits(X.parserIMAGE, X.parser);
/**
* @inheritDoc
*/
X.parserIMAGE.prototype.parse = function(container, object, data, flag) {
if (!(data instanceof ArrayBuffer)) {
throw new Error();
}
// convert data to a byte array
var bytebuffer = new Uint8Array(data);
var i = bytebuffer.length;
// create a binary string of the bytebuffer
var binaryString = new Array(i);
while (i--) {
binaryString[i] = String.fromCharCode(bytebuffer[i]);
}
var convertedData = binaryString.join('');
// encode the converted binary string
var encodedData = window.btoa(convertedData);
// create a new image
var _image = new Image();
// we need to wait until the image is properly set up
goog.events.listenOnce(_image, goog.events.EventType.LOAD,
this.parseCompleted.bind(this, _image, container, object, data, flag));
// set the encoded data using a data uri
_image.src = "data:image/" + flag + ";base64," + encodedData;
};
/**
* The callback which gets called when the image was set up using data uri. We
* fire the modified event here to tell the X.loader that we are done.
*
* @param {!Image} image The image container.
* @param {!X.base} container A container which holds the loaded data. This can
* be an X.object as well.
* @param {!X.object} object The object to configure.
* @param {!ArrayBuffer} data The data to parse.
* @param {*} flag An additional flag.
*/
X.parserIMAGE.prototype.parseCompleted = function(image, container, object,
data, flag) {
// attach the image to the container
container._image = image;
// if there is any raw data, reset it
container._rawData = null;
// the object should be set up here, so let's fire a modified event
var modifiedEvent = new X.event.ModifiedEvent();
modifiedEvent._object = object;
modifiedEvent._container = container;
this.dispatchEvent(modifiedEvent);
};
// export symbols (required for advanced compilation)
goog.exportSymbol('X.parserIMAGE', X.parserIMAGE);
goog.exportSymbol('X.parserIMAGE.prototype.parse',
X.parserIMAGE.prototype.parse);