colorspace_spec.js 16 KB

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