colorspace_spec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  17. var _primitives = require('../../core/primitives');
  18. var _stream = require('../../core/stream');
  19. var _colorspace = require('../../core/colorspace');
  20. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  21. describe('colorspace', function () {
  22. var XRefMock = function () {
  23. function XRefMock(array) {
  24. _classCallCheck(this, XRefMock);
  25. this.map = Object.create(null);
  26. for (var elem in array) {
  27. var obj = array[elem];
  28. var ref = obj.ref;
  29. var data = obj.data;
  30. this.map[ref.toString()] = data;
  31. }
  32. }
  33. _createClass(XRefMock, [{
  34. key: 'fetch',
  35. value: function fetch(ref) {
  36. return this.map[ref.toString()];
  37. }
  38. }, {
  39. key: 'fetchIfRef',
  40. value: function fetchIfRef(obj) {
  41. if (!(0, _primitives.isRef)(obj)) {
  42. return obj;
  43. }
  44. return this.fetch(obj);
  45. }
  46. }]);
  47. return XRefMock;
  48. }();
  49. describe('ColorSpace', function () {
  50. it('should be true if decode is not an array', function () {
  51. expect(_colorspace.ColorSpace.isDefaultDecode('string', 0)).toBeTruthy();
  52. });
  53. it('should be true if length of decode array is not correct', function () {
  54. expect(_colorspace.ColorSpace.isDefaultDecode([0], 1)).toBeTruthy();
  55. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0], 1)).toBeTruthy();
  56. });
  57. it('should be true if decode map matches the default decode map', function () {
  58. expect(_colorspace.ColorSpace.isDefaultDecode([], 0)).toBeTruthy();
  59. expect(_colorspace.ColorSpace.isDefaultDecode([0, 0], 1)).toBeFalsy();
  60. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1], 1)).toBeTruthy();
  61. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0, 1, 0, 1], 3)).toBeTruthy();
  62. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0, 1, 1, 1], 3)).toBeFalsy();
  63. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0, 1, 0, 1, 0, 1], 4)).toBeTruthy();
  64. expect(_colorspace.ColorSpace.isDefaultDecode([1, 0, 0, 1, 0, 1, 0, 1], 4)).toBeFalsy();
  65. });
  66. });
  67. describe('DeviceGrayCS', function () {
  68. it('should handle the case when cs is a Name object', function () {
  69. var cs = _primitives.Name.get('DeviceGray');
  70. var xref = new XRefMock([{
  71. ref: new _primitives.Ref(10, 0),
  72. data: new _primitives.Dict()
  73. }]);
  74. var res = new _primitives.Dict();
  75. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  76. var testSrc = new Uint8Array([27, 125, 250, 131]);
  77. var testDest = new Uint8Array(4 * 4 * 3);
  78. var expectedDest = new Uint8Array([27, 27, 27, 27, 27, 27, 125, 125, 125, 125, 125, 125, 27, 27, 27, 27, 27, 27, 125, 125, 125, 125, 125, 125, 250, 250, 250, 250, 250, 250, 131, 131, 131, 131, 131, 131, 250, 250, 250, 250, 250, 250, 131, 131, 131, 131, 131, 131]);
  79. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  80. expect(colorSpace.getRgb(new Float32Array([0.1]), 0)).toEqual(new Uint8Array([25, 25, 25]));
  81. expect(colorSpace.getOutputLength(2, 0)).toEqual(6);
  82. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  83. expect(testDest).toEqual(expectedDest);
  84. });
  85. it('should handle the case when cs is an indirect object', function () {
  86. var cs = new _primitives.Ref(10, 0);
  87. var xref = new XRefMock([{
  88. ref: cs,
  89. data: _primitives.Name.get('DeviceGray')
  90. }]);
  91. var res = new _primitives.Dict();
  92. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  93. var testSrc = new Uint8Array([27, 125, 250, 131]);
  94. var testDest = new Uint8Array(3 * 3 * 3);
  95. var expectedDest = new Uint8Array([27, 27, 27, 27, 27, 27, 125, 125, 125, 27, 27, 27, 27, 27, 27, 125, 125, 125, 250, 250, 250, 250, 250, 250, 131, 131, 131]);
  96. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  97. expect(colorSpace.getRgb(new Float32Array([0.2]), 0)).toEqual(new Uint8Array([51, 51, 51]));
  98. expect(colorSpace.getOutputLength(3, 1)).toEqual(12);
  99. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  100. expect(testDest).toEqual(expectedDest);
  101. });
  102. });
  103. describe('DeviceRgbCS', function () {
  104. it('should handle the case when cs is a Name object', function () {
  105. var cs = _primitives.Name.get('DeviceRGB');
  106. var xref = new XRefMock([{
  107. ref: new _primitives.Ref(10, 0),
  108. data: new _primitives.Dict()
  109. }]);
  110. var res = new _primitives.Dict();
  111. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  112. var testSrc = new Uint8Array([27, 125, 250, 131, 139, 140, 111, 25, 198, 21, 147, 255]);
  113. var testDest = new Uint8Array(4 * 4 * 3);
  114. var expectedDest = new Uint8Array([27, 125, 250, 27, 125, 250, 131, 139, 140, 131, 139, 140, 27, 125, 250, 27, 125, 250, 131, 139, 140, 131, 139, 140, 111, 25, 198, 111, 25, 198, 21, 147, 255, 21, 147, 255, 111, 25, 198, 111, 25, 198, 21, 147, 255, 21, 147, 255]);
  115. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  116. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3]), 0)).toEqual(new Uint8Array([25, 51, 76]));
  117. expect(colorSpace.getOutputLength(4, 0)).toEqual(4);
  118. expect(colorSpace.isPassthrough(8)).toBeTruthy();
  119. expect(testDest).toEqual(expectedDest);
  120. });
  121. it('should handle the case when cs is an indirect object', function () {
  122. var cs = new _primitives.Ref(10, 0);
  123. var xref = new XRefMock([{
  124. ref: cs,
  125. data: _primitives.Name.get('DeviceRGB')
  126. }]);
  127. var res = new _primitives.Dict();
  128. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  129. var testSrc = new Uint8Array([27, 125, 250, 131, 139, 140, 111, 25, 198, 21, 147, 255]);
  130. var testDest = new Uint8Array(3 * 3 * 3);
  131. var expectedDest = new Uint8Array([27, 125, 250, 27, 125, 250, 131, 139, 140, 27, 125, 250, 27, 125, 250, 131, 139, 140, 111, 25, 198, 111, 25, 198, 21, 147, 255]);
  132. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  133. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3]), 0)).toEqual(new Uint8Array([25, 51, 76]));
  134. expect(colorSpace.getOutputLength(4, 1)).toEqual(5);
  135. expect(colorSpace.isPassthrough(8)).toBeTruthy();
  136. expect(testDest).toEqual(expectedDest);
  137. });
  138. });
  139. describe('DeviceCmykCS', function () {
  140. it('should handle the case when cs is a Name object', function () {
  141. var cs = _primitives.Name.get('DeviceCMYK');
  142. var xref = new XRefMock([{
  143. ref: new _primitives.Ref(10, 0),
  144. data: new _primitives.Dict()
  145. }]);
  146. var res = new _primitives.Dict();
  147. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  148. var testSrc = new Uint8Array([27, 125, 250, 128, 131, 139, 140, 45, 111, 25, 198, 78, 21, 147, 255, 69]);
  149. var testDest = new Uint8Array(4 * 4 * 3);
  150. var expectedDest = new Uint8Array([135, 80, 18, 135, 80, 18, 113, 102, 97, 113, 102, 97, 135, 80, 18, 135, 80, 18, 113, 102, 97, 113, 102, 97, 112, 143, 75, 112, 143, 75, 188, 98, 27, 188, 98, 27, 112, 143, 75, 112, 143, 75, 188, 98, 27, 188, 98, 27]);
  151. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  152. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3, 1]), 0)).toEqual(new Uint8Array([31, 27, 20]));
  153. expect(colorSpace.getOutputLength(4, 0)).toEqual(3);
  154. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  155. expect(testDest).toEqual(expectedDest);
  156. });
  157. it('should handle the case when cs is an indirect object', function () {
  158. var cs = new _primitives.Ref(10, 0);
  159. var xref = new XRefMock([{
  160. ref: cs,
  161. data: _primitives.Name.get('DeviceCMYK')
  162. }]);
  163. var res = new _primitives.Dict();
  164. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  165. var testSrc = new Uint8Array([27, 125, 250, 128, 131, 139, 140, 45, 111, 25, 198, 78, 21, 147, 255, 69]);
  166. var testDest = new Uint8Array(3 * 3 * 3);
  167. var expectedDest = new Uint8Array([135, 80, 18, 135, 80, 18, 113, 102, 97, 135, 80, 18, 135, 80, 18, 113, 102, 97, 112, 143, 75, 112, 143, 75, 188, 98, 27]);
  168. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  169. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3, 1]), 0)).toEqual(new Uint8Array([31, 27, 20]));
  170. expect(colorSpace.getOutputLength(4, 1)).toEqual(4);
  171. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  172. expect(testDest).toEqual(expectedDest);
  173. });
  174. });
  175. describe('CalGrayCS', function () {
  176. it('should handle the case when cs is an array', function () {
  177. var params = new _primitives.Dict();
  178. params.set('WhitePoint', [1, 1, 1]);
  179. params.set('BlackPoint', [0, 0, 0]);
  180. params.set('Gamma', 2.0);
  181. var cs = [_primitives.Name.get('CalGray'), params];
  182. var xref = new XRefMock([{
  183. ref: new _primitives.Ref(10, 0),
  184. data: new _primitives.Dict()
  185. }]);
  186. var res = new _primitives.Dict();
  187. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  188. var testSrc = new Uint8Array([27, 125, 250, 131]);
  189. var testDest = new Uint8Array(4 * 4 * 3);
  190. var expectedDest = new Uint8Array([25, 25, 25, 25, 25, 25, 143, 143, 143, 143, 143, 143, 25, 25, 25, 25, 25, 25, 143, 143, 143, 143, 143, 143, 251, 251, 251, 251, 251, 251, 148, 148, 148, 148, 148, 148, 251, 251, 251, 251, 251, 251, 148, 148, 148, 148, 148, 148]);
  191. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  192. expect(colorSpace.getRgb(new Float32Array([1.0]), 0)).toEqual(new Uint8Array([255, 255, 255]));
  193. expect(colorSpace.getOutputLength(4, 0)).toEqual(12);
  194. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  195. expect(testDest).toEqual(expectedDest);
  196. });
  197. });
  198. describe('CalRGBCS', function () {
  199. it('should handle the case when cs is an array', function () {
  200. var params = new _primitives.Dict();
  201. params.set('WhitePoint', [1, 1, 1]);
  202. params.set('BlackPoint', [0, 0, 0]);
  203. params.set('Gamma', [1, 1, 1]);
  204. params.set('Matrix', [1, 0, 0, 0, 1, 0, 0, 0, 1]);
  205. var cs = [_primitives.Name.get('CalRGB'), params];
  206. var xref = new XRefMock([{
  207. ref: new _primitives.Ref(10, 0),
  208. data: new _primitives.Dict()
  209. }]);
  210. var res = new _primitives.Dict();
  211. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  212. var testSrc = new Uint8Array([27, 125, 250, 131, 139, 140, 111, 25, 198, 21, 147, 255]);
  213. var testDest = new Uint8Array(3 * 3 * 3);
  214. var expectedDest = new Uint8Array([0, 238, 255, 0, 238, 255, 185, 196, 195, 0, 238, 255, 0, 238, 255, 185, 196, 195, 235, 0, 243, 235, 0, 243, 0, 255, 255]);
  215. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  216. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3]), 0)).toEqual(new Uint8Array([0, 147, 151]));
  217. expect(colorSpace.getOutputLength(4, 0)).toEqual(4);
  218. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  219. expect(testDest).toEqual(expectedDest);
  220. });
  221. });
  222. describe('LabCS', function () {
  223. it('should handle the case when cs is an array', function () {
  224. var params = new _primitives.Dict();
  225. params.set('WhitePoint', [1, 1, 1]);
  226. params.set('BlackPoint', [0, 0, 0]);
  227. params.set('Range', [-100, 100, -100, 100]);
  228. var cs = [_primitives.Name.get('Lab'), params];
  229. var xref = new XRefMock([{
  230. ref: new _primitives.Ref(10, 0),
  231. data: new _primitives.Dict()
  232. }]);
  233. var res = new _primitives.Dict();
  234. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  235. var testSrc = new Uint8Array([27, 25, 50, 31, 19, 40, 11, 25, 98, 21, 47, 55]);
  236. var testDest = new Uint8Array(3 * 3 * 3);
  237. var expectedDest = new Uint8Array([0, 49, 101, 0, 49, 101, 0, 53, 116, 0, 49, 101, 0, 49, 101, 0, 53, 116, 0, 40, 39, 0, 40, 39, 0, 43, 90]);
  238. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  239. expect(colorSpace.getRgb([55, 25, 35], 0)).toEqual(new Uint8Array([188, 99, 61]));
  240. expect(colorSpace.getOutputLength(4, 0)).toEqual(4);
  241. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  242. expect(colorSpace.isDefaultDecode([0, 1])).toBeTruthy();
  243. expect(testDest).toEqual(expectedDest);
  244. });
  245. });
  246. describe('IndexedCS', function () {
  247. it('should handle the case when cs is an array', function () {
  248. var lookup = new Uint8Array([23, 155, 35, 147, 69, 93, 255, 109, 70]);
  249. var cs = [_primitives.Name.get('Indexed'), _primitives.Name.get('DeviceRGB'), 2, lookup];
  250. var xref = new XRefMock([{
  251. ref: new _primitives.Ref(10, 0),
  252. data: new _primitives.Dict()
  253. }]);
  254. var res = new _primitives.Dict();
  255. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  256. var testSrc = new Uint8Array([2, 2, 0, 1]);
  257. var testDest = new Uint8Array(3 * 3 * 3);
  258. var expectedDest = new Uint8Array([255, 109, 70, 255, 109, 70, 255, 109, 70, 255, 109, 70, 255, 109, 70, 255, 109, 70, 23, 155, 35, 23, 155, 35, 147, 69, 93]);
  259. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  260. expect(colorSpace.getRgb([2], 0)).toEqual(new Uint8Array([255, 109, 70]));
  261. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  262. expect(colorSpace.isDefaultDecode([0, 1])).toBeTruthy();
  263. expect(testDest).toEqual(expectedDest);
  264. });
  265. });
  266. describe('AlternateCS', function () {
  267. it('should handle the case when cs is an array', function () {
  268. var fnDict = new _primitives.Dict();
  269. fnDict.set('FunctionType', 4);
  270. fnDict.set('Domain', [0.0, 1.0]);
  271. fnDict.set('Range', [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0]);
  272. fnDict.set('Length', 58);
  273. var fn = new _stream.StringStream('{ dup 0.84 mul ' + 'exch 0.00 exch ' + 'dup 0.44 mul ' + 'exch 0.21 mul }');
  274. fn = new _stream.Stream(fn.bytes, 0, 58, fnDict);
  275. var fnRef = new _primitives.Ref(10, 0);
  276. var cs = [_primitives.Name.get('Separation'), _primitives.Name.get('LogoGreen'), _primitives.Name.get('DeviceCMYK'), fnRef];
  277. var xref = new XRefMock([{
  278. ref: fnRef,
  279. data: fn
  280. }]);
  281. var res = new _primitives.Dict();
  282. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  283. var testSrc = new Uint8Array([27, 25, 50, 31]);
  284. var testDest = new Uint8Array(3 * 3 * 3);
  285. var expectedDest = new Uint8Array([227, 243, 242, 227, 243, 242, 228, 243, 242, 227, 243, 242, 227, 243, 242, 228, 243, 242, 203, 233, 229, 203, 233, 229, 222, 241, 239]);
  286. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  287. expect(colorSpace.getRgb([0.1], 0)).toEqual(new Uint8Array([228, 243, 241]));
  288. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  289. expect(colorSpace.isDefaultDecode([0, 1])).toBeTruthy();
  290. expect(testDest).toEqual(expectedDest);
  291. });
  292. });
  293. });