colorspace_spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 _primitives = require('../../core/primitives');
  17. var _stream = require('../../core/stream');
  18. var _colorspace = require('../../core/colorspace');
  19. var _test_utils = require('./test_utils');
  20. describe('colorspace', function () {
  21. describe('ColorSpace', function () {
  22. it('should be true if decode is not an array', function () {
  23. expect(_colorspace.ColorSpace.isDefaultDecode('string', 0)).toBeTruthy();
  24. });
  25. it('should be true if length of decode array is not correct', function () {
  26. expect(_colorspace.ColorSpace.isDefaultDecode([0], 1)).toBeTruthy();
  27. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0], 1)).toBeTruthy();
  28. });
  29. it('should be true if decode map matches the default decode map', function () {
  30. expect(_colorspace.ColorSpace.isDefaultDecode([], 0)).toBeTruthy();
  31. expect(_colorspace.ColorSpace.isDefaultDecode([0, 0], 1)).toBeFalsy();
  32. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1], 1)).toBeTruthy();
  33. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0, 1, 0, 1], 3)).toBeTruthy();
  34. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0, 1, 1, 1], 3)).toBeFalsy();
  35. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0, 1, 0, 1, 0, 1], 4)).toBeTruthy();
  36. expect(_colorspace.ColorSpace.isDefaultDecode([1, 0, 0, 1, 0, 1, 0, 1], 4)).toBeFalsy();
  37. });
  38. });
  39. describe('DeviceGrayCS', function () {
  40. it('should handle the case when cs is a Name object', function () {
  41. var cs = _primitives.Name.get('DeviceGray');
  42. var xref = new _test_utils.XRefMock([{
  43. ref: new _primitives.Ref(10, 0),
  44. data: new _primitives.Dict()
  45. }]);
  46. var res = new _primitives.Dict();
  47. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  48. var testSrc = new Uint8Array([27, 125, 250, 131]);
  49. var testDest = new Uint8Array(4 * 4 * 3);
  50. 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]);
  51. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  52. expect(colorSpace.getRgb(new Float32Array([0.1]), 0)).toEqual(new Uint8Array([25, 25, 25]));
  53. expect(colorSpace.getOutputLength(2, 0)).toEqual(6);
  54. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  55. expect(testDest).toEqual(expectedDest);
  56. });
  57. it('should handle the case when cs is an indirect object', function () {
  58. var cs = new _primitives.Ref(10, 0);
  59. var xref = new _test_utils.XRefMock([{
  60. ref: cs,
  61. data: _primitives.Name.get('DeviceGray')
  62. }]);
  63. var res = new _primitives.Dict();
  64. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  65. var testSrc = new Uint8Array([27, 125, 250, 131]);
  66. var testDest = new Uint8Array(3 * 3 * 3);
  67. 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]);
  68. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  69. expect(colorSpace.getRgb(new Float32Array([0.2]), 0)).toEqual(new Uint8Array([51, 51, 51]));
  70. expect(colorSpace.getOutputLength(3, 1)).toEqual(12);
  71. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  72. expect(testDest).toEqual(expectedDest);
  73. });
  74. });
  75. describe('DeviceRgbCS', function () {
  76. it('should handle the case when cs is a Name object', function () {
  77. var cs = _primitives.Name.get('DeviceRGB');
  78. var xref = new _test_utils.XRefMock([{
  79. ref: new _primitives.Ref(10, 0),
  80. data: new _primitives.Dict()
  81. }]);
  82. var res = new _primitives.Dict();
  83. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  84. var testSrc = new Uint8Array([27, 125, 250, 131, 139, 140, 111, 25, 198, 21, 147, 255]);
  85. var testDest = new Uint8Array(4 * 4 * 3);
  86. 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]);
  87. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  88. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3]), 0)).toEqual(new Uint8Array([25, 51, 76]));
  89. expect(colorSpace.getOutputLength(4, 0)).toEqual(4);
  90. expect(colorSpace.isPassthrough(8)).toBeTruthy();
  91. expect(testDest).toEqual(expectedDest);
  92. });
  93. it('should handle the case when cs is an indirect object', function () {
  94. var cs = new _primitives.Ref(10, 0);
  95. var xref = new _test_utils.XRefMock([{
  96. ref: cs,
  97. data: _primitives.Name.get('DeviceRGB')
  98. }]);
  99. var res = new _primitives.Dict();
  100. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  101. var testSrc = new Uint8Array([27, 125, 250, 131, 139, 140, 111, 25, 198, 21, 147, 255]);
  102. var testDest = new Uint8Array(3 * 3 * 3);
  103. 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]);
  104. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  105. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3]), 0)).toEqual(new Uint8Array([25, 51, 76]));
  106. expect(colorSpace.getOutputLength(4, 1)).toEqual(5);
  107. expect(colorSpace.isPassthrough(8)).toBeTruthy();
  108. expect(testDest).toEqual(expectedDest);
  109. });
  110. });
  111. describe('DeviceCmykCS', function () {
  112. it('should handle the case when cs is a Name object', function () {
  113. var cs = _primitives.Name.get('DeviceCMYK');
  114. var xref = new _test_utils.XRefMock([{
  115. ref: new _primitives.Ref(10, 0),
  116. data: new _primitives.Dict()
  117. }]);
  118. var res = new _primitives.Dict();
  119. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  120. var testSrc = new Uint8Array([27, 125, 250, 128, 131, 139, 140, 45, 111, 25, 198, 78, 21, 147, 255, 69]);
  121. var testDest = new Uint8Array(4 * 4 * 3);
  122. 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]);
  123. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  124. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3, 1]), 0)).toEqual(new Uint8Array([31, 27, 20]));
  125. expect(colorSpace.getOutputLength(4, 0)).toEqual(3);
  126. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  127. expect(testDest).toEqual(expectedDest);
  128. });
  129. it('should handle the case when cs is an indirect object', function () {
  130. var cs = new _primitives.Ref(10, 0);
  131. var xref = new _test_utils.XRefMock([{
  132. ref: cs,
  133. data: _primitives.Name.get('DeviceCMYK')
  134. }]);
  135. var res = new _primitives.Dict();
  136. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  137. var testSrc = new Uint8Array([27, 125, 250, 128, 131, 139, 140, 45, 111, 25, 198, 78, 21, 147, 255, 69]);
  138. var testDest = new Uint8Array(3 * 3 * 3);
  139. 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]);
  140. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  141. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3, 1]), 0)).toEqual(new Uint8Array([31, 27, 20]));
  142. expect(colorSpace.getOutputLength(4, 1)).toEqual(4);
  143. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  144. expect(testDest).toEqual(expectedDest);
  145. });
  146. });
  147. describe('CalGrayCS', function () {
  148. it('should handle the case when cs is an array', function () {
  149. var params = new _primitives.Dict();
  150. params.set('WhitePoint', [1, 1, 1]);
  151. params.set('BlackPoint', [0, 0, 0]);
  152. params.set('Gamma', 2.0);
  153. var cs = [_primitives.Name.get('CalGray'), params];
  154. var xref = new _test_utils.XRefMock([{
  155. ref: new _primitives.Ref(10, 0),
  156. data: new _primitives.Dict()
  157. }]);
  158. var res = new _primitives.Dict();
  159. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  160. var testSrc = new Uint8Array([27, 125, 250, 131]);
  161. var testDest = new Uint8Array(4 * 4 * 3);
  162. 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]);
  163. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  164. expect(colorSpace.getRgb(new Float32Array([1.0]), 0)).toEqual(new Uint8Array([255, 255, 255]));
  165. expect(colorSpace.getOutputLength(4, 0)).toEqual(12);
  166. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  167. expect(testDest).toEqual(expectedDest);
  168. });
  169. });
  170. describe('CalRGBCS', function () {
  171. it('should handle the case when cs is an array', function () {
  172. var params = new _primitives.Dict();
  173. params.set('WhitePoint', [1, 1, 1]);
  174. params.set('BlackPoint', [0, 0, 0]);
  175. params.set('Gamma', [1, 1, 1]);
  176. params.set('Matrix', [1, 0, 0, 0, 1, 0, 0, 0, 1]);
  177. var cs = [_primitives.Name.get('CalRGB'), params];
  178. var xref = new _test_utils.XRefMock([{
  179. ref: new _primitives.Ref(10, 0),
  180. data: new _primitives.Dict()
  181. }]);
  182. var res = new _primitives.Dict();
  183. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  184. var testSrc = new Uint8Array([27, 125, 250, 131, 139, 140, 111, 25, 198, 21, 147, 255]);
  185. var testDest = new Uint8Array(3 * 3 * 3);
  186. 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]);
  187. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  188. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3]), 0)).toEqual(new Uint8Array([0, 147, 151]));
  189. expect(colorSpace.getOutputLength(4, 0)).toEqual(4);
  190. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  191. expect(testDest).toEqual(expectedDest);
  192. });
  193. });
  194. describe('LabCS', function () {
  195. it('should handle the case when cs is an array', function () {
  196. var params = new _primitives.Dict();
  197. params.set('WhitePoint', [1, 1, 1]);
  198. params.set('BlackPoint', [0, 0, 0]);
  199. params.set('Range', [-100, 100, -100, 100]);
  200. var cs = [_primitives.Name.get('Lab'), params];
  201. var xref = new _test_utils.XRefMock([{
  202. ref: new _primitives.Ref(10, 0),
  203. data: new _primitives.Dict()
  204. }]);
  205. var res = new _primitives.Dict();
  206. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  207. var testSrc = new Uint8Array([27, 25, 50, 31, 19, 40, 11, 25, 98, 21, 47, 55]);
  208. var testDest = new Uint8Array(3 * 3 * 3);
  209. 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]);
  210. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  211. expect(colorSpace.getRgb([55, 25, 35], 0)).toEqual(new Uint8Array([188, 99, 61]));
  212. expect(colorSpace.getOutputLength(4, 0)).toEqual(4);
  213. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  214. expect(colorSpace.isDefaultDecode([0, 1])).toBeTruthy();
  215. expect(testDest).toEqual(expectedDest);
  216. });
  217. });
  218. describe('IndexedCS', function () {
  219. it('should handle the case when cs is an array', function () {
  220. var lookup = new Uint8Array([23, 155, 35, 147, 69, 93, 255, 109, 70]);
  221. var cs = [_primitives.Name.get('Indexed'), _primitives.Name.get('DeviceRGB'), 2, lookup];
  222. var xref = new _test_utils.XRefMock([{
  223. ref: new _primitives.Ref(10, 0),
  224. data: new _primitives.Dict()
  225. }]);
  226. var res = new _primitives.Dict();
  227. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  228. var testSrc = new Uint8Array([2, 2, 0, 1]);
  229. var testDest = new Uint8Array(3 * 3 * 3);
  230. 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]);
  231. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  232. expect(colorSpace.getRgb([2], 0)).toEqual(new Uint8Array([255, 109, 70]));
  233. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  234. expect(colorSpace.isDefaultDecode([0, 1])).toBeTruthy();
  235. expect(testDest).toEqual(expectedDest);
  236. });
  237. });
  238. describe('AlternateCS', function () {
  239. it('should handle the case when cs is an array', function () {
  240. var fnDict = new _primitives.Dict();
  241. fnDict.set('FunctionType', 4);
  242. fnDict.set('Domain', [0.0, 1.0]);
  243. fnDict.set('Range', [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0]);
  244. fnDict.set('Length', 58);
  245. var fn = new _stream.StringStream('{ dup 0.84 mul ' + 'exch 0.00 exch ' + 'dup 0.44 mul ' + 'exch 0.21 mul }');
  246. fn = new _stream.Stream(fn.bytes, 0, 58, fnDict);
  247. var fnRef = new _primitives.Ref(10, 0);
  248. var cs = [_primitives.Name.get('Separation'), _primitives.Name.get('LogoGreen'), _primitives.Name.get('DeviceCMYK'), fnRef];
  249. var xref = new _test_utils.XRefMock([{
  250. ref: fnRef,
  251. data: fn
  252. }]);
  253. var res = new _primitives.Dict();
  254. var colorSpace = _colorspace.ColorSpace.parse(cs, xref, res);
  255. var testSrc = new Uint8Array([27, 25, 50, 31]);
  256. var testDest = new Uint8Array(3 * 3 * 3);
  257. 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]);
  258. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  259. expect(colorSpace.getRgb([0.1], 0)).toEqual(new Uint8Array([228, 243, 241]));
  260. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  261. expect(colorSpace.isDefaultDecode([0, 1])).toBeTruthy();
  262. expect(testDest).toEqual(expectedDest);
  263. });
  264. });
  265. });