2
0

colorspace_spec.js 16 KB

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