colorspace_spec.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.js");
  24. var _stream = require("../../core/stream.js");
  25. var _colorspace = require("../../core/colorspace.js");
  26. var _image_utils = require("../../core/image_utils.js");
  27. var _function = require("../../core/function.js");
  28. var _test_utils = require("./test_utils.js");
  29. describe("colorspace", function () {
  30. describe("ColorSpace.isDefaultDecode", function () {
  31. it("should be true if decode is not an array", function () {
  32. expect(_colorspace.ColorSpace.isDefaultDecode("string", 0)).toBeTruthy();
  33. });
  34. it("should be true if length of decode array is not correct", function () {
  35. expect(_colorspace.ColorSpace.isDefaultDecode([0], 1)).toBeTruthy();
  36. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0], 1)).toBeTruthy();
  37. });
  38. it("should be true if decode map matches the default decode map", function () {
  39. expect(_colorspace.ColorSpace.isDefaultDecode([], 0)).toBeTruthy();
  40. expect(_colorspace.ColorSpace.isDefaultDecode([0, 0], 1)).toBeFalsy();
  41. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1], 1)).toBeTruthy();
  42. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0, 1, 0, 1], 3)).toBeTruthy();
  43. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0, 1, 1, 1], 3)).toBeFalsy();
  44. expect(_colorspace.ColorSpace.isDefaultDecode([0, 1, 0, 1, 0, 1, 0, 1], 4)).toBeTruthy();
  45. expect(_colorspace.ColorSpace.isDefaultDecode([1, 0, 0, 1, 0, 1, 0, 1], 4)).toBeFalsy();
  46. });
  47. });
  48. describe("ColorSpace caching", function () {
  49. let localColorSpaceCache = null;
  50. beforeAll(function () {
  51. localColorSpaceCache = new _image_utils.LocalColorSpaceCache();
  52. });
  53. afterAll(function () {
  54. localColorSpaceCache = null;
  55. });
  56. it("caching by Name", function () {
  57. const xref = new _test_utils.XRefMock();
  58. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  59. xref
  60. });
  61. const colorSpace1 = _colorspace.ColorSpace.parse({
  62. cs: _primitives.Name.get("Pattern"),
  63. xref,
  64. resources: null,
  65. pdfFunctionFactory,
  66. localColorSpaceCache
  67. });
  68. expect(colorSpace1.name).toEqual("Pattern");
  69. const colorSpace2 = _colorspace.ColorSpace.parse({
  70. cs: _primitives.Name.get("Pattern"),
  71. xref,
  72. resources: null,
  73. pdfFunctionFactory,
  74. localColorSpaceCache
  75. });
  76. expect(colorSpace2.name).toEqual("Pattern");
  77. const colorSpaceNonCached = _colorspace.ColorSpace.parse({
  78. cs: _primitives.Name.get("Pattern"),
  79. xref,
  80. resources: null,
  81. pdfFunctionFactory,
  82. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  83. });
  84. expect(colorSpaceNonCached.name).toEqual("Pattern");
  85. const colorSpaceOther = _colorspace.ColorSpace.parse({
  86. cs: _primitives.Name.get("RGB"),
  87. xref,
  88. resources: null,
  89. pdfFunctionFactory,
  90. localColorSpaceCache
  91. });
  92. expect(colorSpaceOther.name).toEqual("DeviceRGB");
  93. expect(colorSpace1).toBe(colorSpace2);
  94. expect(colorSpace1).not.toBe(colorSpaceNonCached);
  95. expect(colorSpace1).not.toBe(colorSpaceOther);
  96. });
  97. it("caching by Ref", function () {
  98. const paramsCalGray = new _primitives.Dict();
  99. paramsCalGray.set("WhitePoint", [1, 1, 1]);
  100. paramsCalGray.set("BlackPoint", [0, 0, 0]);
  101. paramsCalGray.set("Gamma", 2.0);
  102. const paramsCalRGB = new _primitives.Dict();
  103. paramsCalRGB.set("WhitePoint", [1, 1, 1]);
  104. paramsCalRGB.set("BlackPoint", [0, 0, 0]);
  105. paramsCalRGB.set("Gamma", [1, 1, 1]);
  106. paramsCalRGB.set("Matrix", [1, 0, 0, 0, 1, 0, 0, 0, 1]);
  107. const xref = new _test_utils.XRefMock([{
  108. ref: _primitives.Ref.get(50, 0),
  109. data: [_primitives.Name.get("CalGray"), paramsCalGray]
  110. }, {
  111. ref: _primitives.Ref.get(100, 0),
  112. data: [_primitives.Name.get("CalRGB"), paramsCalRGB]
  113. }]);
  114. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  115. xref
  116. });
  117. const colorSpace1 = _colorspace.ColorSpace.parse({
  118. cs: _primitives.Ref.get(50, 0),
  119. xref,
  120. resources: null,
  121. pdfFunctionFactory,
  122. localColorSpaceCache
  123. });
  124. expect(colorSpace1.name).toEqual("CalGray");
  125. const colorSpace2 = _colorspace.ColorSpace.parse({
  126. cs: _primitives.Ref.get(50, 0),
  127. xref,
  128. resources: null,
  129. pdfFunctionFactory,
  130. localColorSpaceCache
  131. });
  132. expect(colorSpace2.name).toEqual("CalGray");
  133. const colorSpaceNonCached = _colorspace.ColorSpace.parse({
  134. cs: _primitives.Ref.get(50, 0),
  135. xref,
  136. resources: null,
  137. pdfFunctionFactory,
  138. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  139. });
  140. expect(colorSpaceNonCached.name).toEqual("CalGray");
  141. const colorSpaceOther = _colorspace.ColorSpace.parse({
  142. cs: _primitives.Ref.get(100, 0),
  143. xref,
  144. resources: null,
  145. pdfFunctionFactory,
  146. localColorSpaceCache
  147. });
  148. expect(colorSpaceOther.name).toEqual("CalRGB");
  149. expect(colorSpace1).toBe(colorSpace2);
  150. expect(colorSpace1).not.toBe(colorSpaceNonCached);
  151. expect(colorSpace1).not.toBe(colorSpaceOther);
  152. });
  153. });
  154. describe("DeviceGrayCS", function () {
  155. it("should handle the case when cs is a Name object", function () {
  156. const cs = _primitives.Name.get("DeviceGray");
  157. const xref = new _test_utils.XRefMock([{
  158. ref: _primitives.Ref.get(10, 0),
  159. data: new _primitives.Dict()
  160. }]);
  161. const resources = new _primitives.Dict();
  162. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  163. xref
  164. });
  165. const colorSpace = _colorspace.ColorSpace.parse({
  166. cs,
  167. xref,
  168. resources,
  169. pdfFunctionFactory,
  170. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  171. });
  172. const testSrc = new Uint8Array([27, 125, 250, 131]);
  173. const testDest = new Uint8ClampedArray(4 * 4 * 3);
  174. const expectedDest = new Uint8ClampedArray([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]);
  175. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  176. expect(colorSpace.getRgb(new Float32Array([0.1]), 0)).toEqual(new Uint8ClampedArray([26, 26, 26]));
  177. expect(colorSpace.getOutputLength(2, 0)).toEqual(6);
  178. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  179. expect(testDest).toEqual(expectedDest);
  180. });
  181. it("should handle the case when cs is an indirect object", function () {
  182. const cs = _primitives.Ref.get(10, 0);
  183. const xref = new _test_utils.XRefMock([{
  184. ref: cs,
  185. data: _primitives.Name.get("DeviceGray")
  186. }]);
  187. const resources = new _primitives.Dict();
  188. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  189. xref
  190. });
  191. const colorSpace = _colorspace.ColorSpace.parse({
  192. cs,
  193. xref,
  194. resources,
  195. pdfFunctionFactory,
  196. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  197. });
  198. const testSrc = new Uint8Array([27, 125, 250, 131]);
  199. const testDest = new Uint8ClampedArray(3 * 3 * 3);
  200. const expectedDest = new Uint8ClampedArray([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]);
  201. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  202. expect(colorSpace.getRgb(new Float32Array([0.2]), 0)).toEqual(new Uint8ClampedArray([51, 51, 51]));
  203. expect(colorSpace.getOutputLength(3, 1)).toEqual(12);
  204. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  205. expect(testDest).toEqual(expectedDest);
  206. });
  207. });
  208. describe("DeviceRgbCS", function () {
  209. it("should handle the case when cs is a Name object", function () {
  210. const cs = _primitives.Name.get("DeviceRGB");
  211. const xref = new _test_utils.XRefMock([{
  212. ref: _primitives.Ref.get(10, 0),
  213. data: new _primitives.Dict()
  214. }]);
  215. const resources = new _primitives.Dict();
  216. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  217. xref
  218. });
  219. const colorSpace = _colorspace.ColorSpace.parse({
  220. cs,
  221. xref,
  222. resources,
  223. pdfFunctionFactory,
  224. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  225. });
  226. const testSrc = new Uint8Array([27, 125, 250, 131, 139, 140, 111, 25, 198, 21, 147, 255]);
  227. const testDest = new Uint8ClampedArray(4 * 4 * 3);
  228. const expectedDest = new Uint8ClampedArray([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]);
  229. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  230. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3]), 0)).toEqual(new Uint8ClampedArray([26, 51, 77]));
  231. expect(colorSpace.getOutputLength(4, 0)).toEqual(4);
  232. expect(colorSpace.isPassthrough(8)).toBeTruthy();
  233. expect(testDest).toEqual(expectedDest);
  234. });
  235. it("should handle the case when cs is an indirect object", function () {
  236. const cs = _primitives.Ref.get(10, 0);
  237. const xref = new _test_utils.XRefMock([{
  238. ref: cs,
  239. data: _primitives.Name.get("DeviceRGB")
  240. }]);
  241. const resources = new _primitives.Dict();
  242. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  243. xref
  244. });
  245. const colorSpace = _colorspace.ColorSpace.parse({
  246. cs,
  247. xref,
  248. resources,
  249. pdfFunctionFactory,
  250. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  251. });
  252. const testSrc = new Uint8Array([27, 125, 250, 131, 139, 140, 111, 25, 198, 21, 147, 255]);
  253. const testDest = new Uint8ClampedArray(3 * 3 * 3);
  254. const expectedDest = new Uint8ClampedArray([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]);
  255. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  256. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3]), 0)).toEqual(new Uint8ClampedArray([26, 51, 77]));
  257. expect(colorSpace.getOutputLength(4, 1)).toEqual(5);
  258. expect(colorSpace.isPassthrough(8)).toBeTruthy();
  259. expect(testDest).toEqual(expectedDest);
  260. });
  261. });
  262. describe("DeviceCmykCS", function () {
  263. it("should handle the case when cs is a Name object", function () {
  264. const cs = _primitives.Name.get("DeviceCMYK");
  265. const xref = new _test_utils.XRefMock([{
  266. ref: _primitives.Ref.get(10, 0),
  267. data: new _primitives.Dict()
  268. }]);
  269. const resources = new _primitives.Dict();
  270. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  271. xref
  272. });
  273. const colorSpace = _colorspace.ColorSpace.parse({
  274. cs,
  275. xref,
  276. resources,
  277. pdfFunctionFactory,
  278. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  279. });
  280. const testSrc = new Uint8Array([27, 125, 250, 128, 131, 139, 140, 45, 111, 25, 198, 78, 21, 147, 255, 69]);
  281. const testDest = new Uint8ClampedArray(4 * 4 * 3);
  282. const expectedDest = new Uint8ClampedArray([135, 81, 18, 135, 81, 18, 114, 102, 97, 114, 102, 97, 135, 81, 18, 135, 81, 18, 114, 102, 97, 114, 102, 97, 112, 144, 75, 112, 144, 75, 188, 98, 27, 188, 98, 27, 112, 144, 75, 112, 144, 75, 188, 98, 27, 188, 98, 27]);
  283. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  284. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3, 1]), 0)).toEqual(new Uint8ClampedArray([32, 28, 21]));
  285. expect(colorSpace.getOutputLength(4, 0)).toEqual(3);
  286. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  287. expect(testDest).toEqual(expectedDest);
  288. });
  289. it("should handle the case when cs is an indirect object", function () {
  290. const cs = _primitives.Ref.get(10, 0);
  291. const xref = new _test_utils.XRefMock([{
  292. ref: cs,
  293. data: _primitives.Name.get("DeviceCMYK")
  294. }]);
  295. const resources = new _primitives.Dict();
  296. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  297. xref
  298. });
  299. const colorSpace = _colorspace.ColorSpace.parse({
  300. cs,
  301. xref,
  302. resources,
  303. pdfFunctionFactory,
  304. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  305. });
  306. const testSrc = new Uint8Array([27, 125, 250, 128, 131, 139, 140, 45, 111, 25, 198, 78, 21, 147, 255, 69]);
  307. const testDest = new Uint8ClampedArray(3 * 3 * 3);
  308. const expectedDest = new Uint8ClampedArray([135, 81, 18, 135, 81, 18, 114, 102, 97, 135, 81, 18, 135, 81, 18, 114, 102, 97, 112, 144, 75, 112, 144, 75, 188, 98, 27]);
  309. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  310. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3, 1]), 0)).toEqual(new Uint8ClampedArray([32, 28, 21]));
  311. expect(colorSpace.getOutputLength(4, 1)).toEqual(4);
  312. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  313. expect(testDest).toEqual(expectedDest);
  314. });
  315. });
  316. describe("CalGrayCS", function () {
  317. it("should handle the case when cs is an array", function () {
  318. const params = new _primitives.Dict();
  319. params.set("WhitePoint", [1, 1, 1]);
  320. params.set("BlackPoint", [0, 0, 0]);
  321. params.set("Gamma", 2.0);
  322. const cs = [_primitives.Name.get("CalGray"), params];
  323. const xref = new _test_utils.XRefMock([{
  324. ref: _primitives.Ref.get(10, 0),
  325. data: new _primitives.Dict()
  326. }]);
  327. const resources = new _primitives.Dict();
  328. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  329. xref
  330. });
  331. const colorSpace = _colorspace.ColorSpace.parse({
  332. cs,
  333. xref,
  334. resources,
  335. pdfFunctionFactory,
  336. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  337. });
  338. const testSrc = new Uint8Array([27, 125, 250, 131]);
  339. const testDest = new Uint8ClampedArray(4 * 4 * 3);
  340. const expectedDest = new Uint8ClampedArray([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, 149, 149, 149, 149, 149, 149, 251, 251, 251, 251, 251, 251, 149, 149, 149, 149, 149, 149]);
  341. colorSpace.fillRgb(testDest, 2, 2, 4, 4, 4, 8, testSrc, 0);
  342. expect(colorSpace.getRgb(new Float32Array([1.0]), 0)).toEqual(new Uint8ClampedArray([255, 255, 255]));
  343. expect(colorSpace.getOutputLength(4, 0)).toEqual(12);
  344. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  345. expect(testDest).toEqual(expectedDest);
  346. });
  347. });
  348. describe("CalRGBCS", function () {
  349. it("should handle the case when cs is an array", function () {
  350. const params = new _primitives.Dict();
  351. params.set("WhitePoint", [1, 1, 1]);
  352. params.set("BlackPoint", [0, 0, 0]);
  353. params.set("Gamma", [1, 1, 1]);
  354. params.set("Matrix", [1, 0, 0, 0, 1, 0, 0, 0, 1]);
  355. const cs = [_primitives.Name.get("CalRGB"), params];
  356. const xref = new _test_utils.XRefMock([{
  357. ref: _primitives.Ref.get(10, 0),
  358. data: new _primitives.Dict()
  359. }]);
  360. const resources = new _primitives.Dict();
  361. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  362. xref
  363. });
  364. const colorSpace = _colorspace.ColorSpace.parse({
  365. cs,
  366. xref,
  367. resources,
  368. pdfFunctionFactory,
  369. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  370. });
  371. const testSrc = new Uint8Array([27, 125, 250, 131, 139, 140, 111, 25, 198, 21, 147, 255]);
  372. const testDest = new Uint8ClampedArray(3 * 3 * 3);
  373. const expectedDest = new Uint8ClampedArray([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]);
  374. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  375. expect(colorSpace.getRgb(new Float32Array([0.1, 0.2, 0.3]), 0)).toEqual(new Uint8ClampedArray([0, 147, 151]));
  376. expect(colorSpace.getOutputLength(4, 0)).toEqual(4);
  377. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  378. expect(testDest).toEqual(expectedDest);
  379. });
  380. });
  381. describe("LabCS", function () {
  382. it("should handle the case when cs is an array", function () {
  383. const params = new _primitives.Dict();
  384. params.set("WhitePoint", [1, 1, 1]);
  385. params.set("BlackPoint", [0, 0, 0]);
  386. params.set("Range", [-100, 100, -100, 100]);
  387. const cs = [_primitives.Name.get("Lab"), params];
  388. const xref = new _test_utils.XRefMock([{
  389. ref: _primitives.Ref.get(10, 0),
  390. data: new _primitives.Dict()
  391. }]);
  392. const resources = new _primitives.Dict();
  393. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  394. xref
  395. });
  396. const colorSpace = _colorspace.ColorSpace.parse({
  397. cs,
  398. xref,
  399. resources,
  400. pdfFunctionFactory,
  401. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  402. });
  403. const testSrc = new Uint8Array([27, 25, 50, 31, 19, 40, 11, 25, 98, 21, 47, 55]);
  404. const testDest = new Uint8ClampedArray(3 * 3 * 3);
  405. const expectedDest = new Uint8ClampedArray([0, 49, 101, 0, 49, 101, 0, 53, 117, 0, 49, 101, 0, 49, 101, 0, 53, 117, 0, 41, 40, 0, 41, 40, 0, 43, 90]);
  406. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  407. expect(colorSpace.getRgb([55, 25, 35], 0)).toEqual(new Uint8ClampedArray([188, 100, 61]));
  408. expect(colorSpace.getOutputLength(4, 0)).toEqual(4);
  409. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  410. expect(colorSpace.isDefaultDecode([0, 1])).toBeTruthy();
  411. expect(testDest).toEqual(expectedDest);
  412. });
  413. });
  414. describe("IndexedCS", function () {
  415. it("should handle the case when cs is an array", function () {
  416. const lookup = new _stream.Stream(new Uint8Array([23, 155, 35, 147, 69, 93, 255, 109, 70]));
  417. const cs = [_primitives.Name.get("Indexed"), _primitives.Name.get("DeviceRGB"), 2, lookup];
  418. const xref = new _test_utils.XRefMock([{
  419. ref: _primitives.Ref.get(10, 0),
  420. data: new _primitives.Dict()
  421. }]);
  422. const resources = new _primitives.Dict();
  423. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  424. xref
  425. });
  426. const colorSpace = _colorspace.ColorSpace.parse({
  427. cs,
  428. xref,
  429. resources,
  430. pdfFunctionFactory,
  431. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  432. });
  433. const testSrc = new Uint8Array([2, 2, 0, 1]);
  434. const testDest = new Uint8ClampedArray(3 * 3 * 3);
  435. const expectedDest = new Uint8ClampedArray([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]);
  436. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  437. expect(colorSpace.getRgb([2], 0)).toEqual(new Uint8ClampedArray([255, 109, 70]));
  438. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  439. expect(colorSpace.isDefaultDecode([0, 1], 1)).toBeTruthy();
  440. expect(testDest).toEqual(expectedDest);
  441. });
  442. });
  443. describe("AlternateCS", function () {
  444. it("should handle the case when cs is an array", function () {
  445. const fnDict = new _primitives.Dict();
  446. fnDict.set("FunctionType", 4);
  447. fnDict.set("Domain", [0.0, 1.0]);
  448. fnDict.set("Range", [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0]);
  449. fnDict.set("Length", 58);
  450. let fn = new _stream.StringStream("{ dup 0.84 mul " + "exch 0.00 exch " + "dup 0.44 mul " + "exch 0.21 mul }");
  451. fn = new _stream.Stream(fn.bytes, 0, 58, fnDict);
  452. const fnRef = _primitives.Ref.get(10, 0);
  453. const cs = [_primitives.Name.get("Separation"), _primitives.Name.get("LogoGreen"), _primitives.Name.get("DeviceCMYK"), fnRef];
  454. const xref = new _test_utils.XRefMock([{
  455. ref: fnRef,
  456. data: fn
  457. }]);
  458. const resources = new _primitives.Dict();
  459. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  460. xref
  461. });
  462. const colorSpace = _colorspace.ColorSpace.parse({
  463. cs,
  464. xref,
  465. resources,
  466. pdfFunctionFactory,
  467. localColorSpaceCache: new _image_utils.LocalColorSpaceCache()
  468. });
  469. const testSrc = new Uint8Array([27, 25, 50, 31]);
  470. const testDest = new Uint8ClampedArray(3 * 3 * 3);
  471. const expectedDest = new Uint8ClampedArray([226, 242, 241, 226, 242, 241, 229, 244, 242, 226, 242, 241, 226, 242, 241, 229, 244, 242, 203, 232, 229, 203, 232, 229, 222, 241, 238]);
  472. colorSpace.fillRgb(testDest, 2, 2, 3, 3, 3, 8, testSrc, 0);
  473. expect(colorSpace.getRgb([0.1], 0)).toEqual(new Uint8ClampedArray([228, 243, 242]));
  474. expect(colorSpace.isPassthrough(8)).toBeFalsy();
  475. expect(colorSpace.isDefaultDecode([0, 1])).toBeTruthy();
  476. expect(testDest).toEqual(expectedDest);
  477. });
  478. });
  479. });