Newer
Older
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
}
var code = codes[codeBuf & ((1 << maxLen) - 1)];
var codeLen = code >> 16;
var codeVal = code & 0xffff;
if (codeLen < 1 || codeSize < codeLen) {
error("Bad encoding in flate stream");
}
this.codeBuf = codeBuf >> codeLen;
this.codeSize = codeSize - codeLen;
return codeVal;
};
FlateStream.prototype.generateHuffmanTable = function flateStreamGenerateHuffmanTable(
lengths
) {
var n = lengths.length;
// find max code length
var maxLen = 0;
var i;
for (i = 0; i < n; ++i) {
if (lengths[i] > maxLen) {
maxLen = lengths[i];
}
}
// build the table
var size = 1 << maxLen;
var codes = new Int32Array(size);
for (
var len = 1, code = 0, skip = 2;
len <= maxLen;
++len, code <<= 1, skip <<= 1
) {
for (var val = 0; val < n; ++val) {
if (lengths[val] === len) {
// bit-reverse the code
var code2 = 0;
var t = code;
for (i = 0; i < len; ++i) {
code2 = (code2 << 1) | (t & 1);
t >>= 1;
}
// fill the table entries
for (i = code2; i < size; i += skip) {
codes[i] = (len << 16) | val;
}
++code;
}
}
}
return [codes, maxLen];
};
FlateStream.prototype.readBlock = function FlateStream_readBlock() {
var buffer, len;
var str = this.str;
// read block header
var hdr = this.getBits(3);
if (hdr & 1) {
this.eof = true;
}
hdr >>= 1;
if (hdr === 0) {
// uncompressed block
var b;
if ((b = str.getByte()) === -1) {
error("Bad block header in flate stream");
}
var blockLen = b;
if ((b = str.getByte()) === -1) {
error("Bad block header in flate stream");
}
blockLen |= b << 8;
if ((b = str.getByte()) === -1) {
error("Bad block header in flate stream");
}
var check = b;
if ((b = str.getByte()) === -1) {
error("Bad block header in flate stream");
}
check |= b << 8;
if (check !== (~blockLen & 0xffff) && (blockLen !== 0 || check !== 0)) {
// Ignoring error for bad "empty" block (see issue 1277)
error("Bad uncompressed block length in flate stream");
}
this.codeBuf = 0;
this.codeSize = 0;
var bufferLength = this.bufferLength;
buffer = this.ensureBuffer(bufferLength + blockLen);
var end = bufferLength + blockLen;
this.bufferLength = end;
if (blockLen === 0) {
if (str.peekByte() === -1) {
this.eof = true;
}
} else {
for (var n = bufferLength; n < end; ++n) {
if ((b = str.getByte()) === -1) {
this.eof = true;
break;
}
buffer[n] = b;
}
}
return;
}
var litCodeTable;
var distCodeTable;
if (hdr === 1) {
// compressed block, fixed codes
litCodeTable = fixedLitCodeTab;
distCodeTable = fixedDistCodeTab;
} else if (hdr === 2) {
// compressed block, dynamic codes
var numLitCodes = this.getBits(5) + 257;
var numDistCodes = this.getBits(5) + 1;
var numCodeLenCodes = this.getBits(4) + 4;
// build the code lengths code table
var codeLenCodeLengths = new Uint8Array(codeLenCodeMap.length);
var i;
for (i = 0; i < numCodeLenCodes; ++i) {
codeLenCodeLengths[codeLenCodeMap[i]] = this.getBits(3);
}
var codeLenCodeTab = this.generateHuffmanTable(codeLenCodeLengths);
// build the literal and distance code tables
len = 0;
i = 0;
var codes = numLitCodes + numDistCodes;
var codeLengths = new Uint8Array(codes);
var bitsLength, bitsOffset, what;
while (i < codes) {
var code = this.getCode(codeLenCodeTab);
if (code === 16) {
bitsLength = 2;
bitsOffset = 3;
what = len;
} else if (code === 17) {
bitsLength = 3;
bitsOffset = 3;
what = len = 0;
} else if (code === 18) {
bitsLength = 7;
bitsOffset = 11;
what = len = 0;
} else {
codeLengths[i++] = len = code;
continue;
}
var repeatLength = this.getBits(bitsLength) + bitsOffset;
while (repeatLength-- > 0) {
codeLengths[i++] = what;
}
}
litCodeTable = this.generateHuffmanTable(
codeLengths.subarray(0, numLitCodes)
);
distCodeTable = this.generateHuffmanTable(
codeLengths.subarray(numLitCodes, codes)
);
} else {
error("Unknown block type in flate stream");
}
buffer = this.buffer;
var limit = buffer ? buffer.length : 0;
var pos = this.bufferLength;
while (true) {
var code1 = this.getCode(litCodeTable);
if (code1 < 256) {
if (pos + 1 >= limit) {
buffer = this.ensureBuffer(pos + 1);
limit = buffer.length;
}
buffer[pos++] = code1;
continue;
}
if (code1 === 256) {
this.bufferLength = pos;
return;
}
code1 -= 257;
code1 = lengthDecode[code1];
var code2 = code1 >> 16;
if (code2 > 0) {
code2 = this.getBits(code2);
}
len = (code1 & 0xffff) + code2;
code1 = this.getCode(distCodeTable);
code1 = distDecode[code1];
code2 = code1 >> 16;
if (code2 > 0) {
code2 = this.getBits(code2);
}
var dist = (code1 & 0xffff) + code2;
if (pos + len >= limit) {
buffer = this.ensureBuffer(pos + len);
limit = buffer.length;
}
for (var k = 0; k < len; ++k, ++pos) {
buffer[pos] = buffer[pos - dist];
}
}
};
return FlateStream;
})();
var PredictorStream = (function PredictorStreamClosure() {
function PredictorStream(str, maybeLength, params) {
var predictor = (this.predictor = params.get("Predictor") || 1);
if (predictor <= 1) {
return str; // no prediction
}
if (predictor !== 2 && (predictor < 10 || predictor > 15)) {
error("Unsupported predictor: " + predictor);
}
if (predictor === 2) {
this.readBlock = this.readBlockTiff;
} else {
this.readBlock = this.readBlockPng;
}
this.str = str;
this.dict = str.dict;
var colors = (this.colors = params.get("Colors") || 1);
var bits = (this.bits = params.get("BitsPerComponent") || 8);
var columns = (this.columns = params.get("Columns") || 1);
this.pixBytes = (colors * bits + 7) >> 3;
this.rowBytes = (columns * colors * bits + 7) >> 3;
DecodeStream.call(this, maybeLength);
return this;
}
PredictorStream.prototype = Object.create(DecodeStream.prototype);
PredictorStream.prototype.readBlockTiff = function predictorStreamReadBlockTiff() {
var rowBytes = this.rowBytes;
var bufferLength = this.bufferLength;
var buffer = this.ensureBuffer(bufferLength + rowBytes);
var bits = this.bits;
var colors = this.colors;
var rawBytes = this.str.getBytes(rowBytes);
this.eof = !rawBytes.length;
if (this.eof) {
return;
}
var inbuf = 0,
outbuf = 0;
var inbits = 0,
outbits = 0;
var pos = bufferLength;
var i;
if (bits === 1) {
for (i = 0; i < rowBytes; ++i) {
var c = rawBytes[i];
inbuf = (inbuf << 8) | c;
// bitwise addition is exclusive or
// first shift inbuf and then add
buffer[pos++] = (c ^ (inbuf >> colors)) & 0xff;
// truncate inbuf (assumes colors < 16)
inbuf &= 0xffff;
}
} else if (bits === 8) {
for (i = 0; i < colors; ++i) {
buffer[pos++] = rawBytes[i];
}
for (; i < rowBytes; ++i) {
buffer[pos] = buffer[pos - colors] + rawBytes[i];
pos++;
}
} else {
var compArray = new Uint8Array(colors + 1);
var bitMask = (1 << bits) - 1;
var j = 0,
k = bufferLength;
var columns = this.columns;
for (i = 0; i < columns; ++i) {
for (var kk = 0; kk < colors; ++kk) {
if (inbits < bits) {
inbuf = (inbuf << 8) | (rawBytes[j++] & 0xff);
inbits += 8;
}
compArray[kk] =
(compArray[kk] + (inbuf >> (inbits - bits))) & bitMask;
inbits -= bits;
outbuf = (outbuf << bits) | compArray[kk];
outbits += bits;
if (outbits >= 8) {
buffer[k++] = (outbuf >> (outbits - 8)) & 0xff;
outbits -= 8;
}
}
}
if (outbits > 0) {
buffer[k++] =
(outbuf << (8 - outbits)) + (inbuf & ((1 << (8 - outbits)) - 1));
}
}
this.bufferLength += rowBytes;
};
PredictorStream.prototype.readBlockPng = function predictorStreamReadBlockPng() {
var rowBytes = this.rowBytes;
var pixBytes = this.pixBytes;
var predictor = this.str.getByte();
var rawBytes = this.str.getBytes(rowBytes);
this.eof = !rawBytes.length;
if (this.eof) {
return;
}
var bufferLength = this.bufferLength;
var buffer = this.ensureBuffer(bufferLength + rowBytes);
var prevRow = buffer.subarray(bufferLength - rowBytes, bufferLength);
if (prevRow.length === 0) {
prevRow = new Uint8Array(rowBytes);
}
var i,
j = bufferLength,
up,
c;
switch (predictor) {
case 0:
for (i = 0; i < rowBytes; ++i) {
buffer[j++] = rawBytes[i];
}
break;
case 1:
for (i = 0; i < pixBytes; ++i) {
buffer[j++] = rawBytes[i];
}
for (; i < rowBytes; ++i) {
buffer[j] = (buffer[j - pixBytes] + rawBytes[i]) & 0xff;
j++;
}
break;
case 2:
for (i = 0; i < rowBytes; ++i) {
buffer[j++] = (prevRow[i] + rawBytes[i]) & 0xff;
}
break;
case 3:
for (i = 0; i < pixBytes; ++i) {
buffer[j++] = (prevRow[i] >> 1) + rawBytes[i];
}
for (; i < rowBytes; ++i) {
buffer[j] =
(((prevRow[i] + buffer[j - pixBytes]) >> 1) + rawBytes[i]) & 0xff;
j++;
}
break;
case 4:
// we need to save the up left pixels values. the simplest way
// is to create a new buffer
for (i = 0; i < pixBytes; ++i) {
up = prevRow[i];
c = rawBytes[i];
buffer[j++] = up + c;
}
for (; i < rowBytes; ++i) {
up = prevRow[i];
var upLeft = prevRow[i - pixBytes];
var left = buffer[j - pixBytes];
var p = left + up - upLeft;
var pa = p - left;
if (pa < 0) {
pa = -pa;
}
var pb = p - up;
if (pb < 0) {
pb = -pb;
}
var pc = p - upLeft;
if (pc < 0) {
pc = -pc;
}
c = rawBytes[i];
if (pa <= pb && pa <= pc) {
buffer[j++] = left + c;
} else if (pb <= pc) {
buffer[j++] = up + c;
} else {
buffer[j++] = upLeft + c;
}
}
break;
default:
error("Unsupported predictor: " + predictor);
}
this.bufferLength += rowBytes;
};
return PredictorStream;
})();
/**
* Depending on the type of JPEG a JpegStream is handled in different ways. For
* JPEG's that are supported natively such as DeviceGray and DeviceRGB the image
* data is stored and then loaded by the browser. For unsupported JPEG's we use
* a library to decode these images and the stream behaves like all the other
* DecodeStreams.
*/
var JpegStream = (function JpegStreamClosure() {
function JpegStream(stream, maybeLength, dict, xref) {
// Some images may contain 'junk' before the SOI (start-of-image) marker.
// Note: this seems to mainly affect inline images.
var ch;
while ((ch = stream.getByte()) !== -1) {
if (ch === 0xff) {
// Find the first byte of the SOI marker (0xFFD8).
stream.skip(-1); // Reset the stream position to the SOI.
break;
}
}
this.stream = stream;
this.maybeLength = maybeLength;
this.dict = dict;
DecodeStream.call(this, maybeLength);
}
JpegStream.prototype = Object.create(DecodeStream.prototype);
Object.defineProperty(JpegStream.prototype, "bytes", {
get: function JpegStream_bytes() {
// If this.maybeLength is null, we'll get the entire stream.
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
},
configurable: true
});
JpegStream.prototype.ensureBuffer = function JpegStream_ensureBuffer(req) {
if (this.bufferLength) {
return;
}
try {
var jpegImage = new JpegImage();
// checking if values needs to be transformed before conversion
if (this.forceRGB && this.dict && isArray(this.dict.get("Decode"))) {
var decodeArr = this.dict.get("Decode");
var bitsPerComponent = this.dict.get("BitsPerComponent") || 8;
var decodeArrLength = decodeArr.length;
var transform = new Int32Array(decodeArrLength);
var transformNeeded = false;
var maxValue = (1 << bitsPerComponent) - 1;
for (var i = 0; i < decodeArrLength; i += 2) {
transform[i] = ((decodeArr[i + 1] - decodeArr[i]) * 256) | 0;
transform[i + 1] = (decodeArr[i] * maxValue) | 0;
if (transform[i] !== 256 || transform[i + 1] !== 0) {
transformNeeded = true;
}
}
if (transformNeeded) {
jpegImage.decodeTransform = transform;
}
}
jpegImage.parse(this.bytes);
var data = jpegImage.getData(
this.drawWidth,
this.drawHeight,
this.forceRGB
);
this.buffer = data;
this.bufferLength = data.length;
this.eof = true;
} catch (e) {
error("JPEG error: " + e);
}
};
JpegStream.prototype.getBytes = function JpegStream_getBytes(length) {
this.ensureBuffer();
return this.buffer;
};
JpegStream.prototype.getIR = function JpegStream_getIR() {
return PDFJS.createObjectURL(this.bytes, "image/jpeg");
};
/**
* Checks if the image can be decoded and displayed by the browser without any
* further processing such as color space conversions.
*/
JpegStream.prototype.isNativelySupported = function JpegStream_isNativelySupported(
xref,
res
) {
var cs = ColorSpace.parse(this.dict.get("ColorSpace", "CS"), xref, res);
return (
(cs.name === "DeviceGray" || cs.name === "DeviceRGB") &&
cs.isDefaultDecode(this.dict.get("Decode", "D"))
);
};
/**
* Checks if the image can be decoded by the browser.
*/
JpegStream.prototype.isNativelyDecodable = function JpegStream_isNativelyDecodable(
xref,
res
) {
var cs = ColorSpace.parse(this.dict.get("ColorSpace", "CS"), xref, res);
return (
(cs.numComps === 1 || cs.numComps === 3) &&
cs.isDefaultDecode(this.dict.get("Decode", "D"))
);
};
return JpegStream;
})();
/**
* For JPEG 2000's we use a library to decode these images and
* the stream behaves like all the other DecodeStreams.
*/
var JpxStream = (function JpxStreamClosure() {
function JpxStream(stream, maybeLength, dict) {
this.stream = stream;
this.maybeLength = maybeLength;
this.dict = dict;
DecodeStream.call(this, maybeLength);
}
JpxStream.prototype = Object.create(DecodeStream.prototype);
Object.defineProperty(JpxStream.prototype, "bytes", {
get: function JpxStream_bytes() {
// If this.maybeLength is null, we'll get the entire stream.
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
},
configurable: true
});
JpxStream.prototype.ensureBuffer = function JpxStream_ensureBuffer(req) {
if (this.bufferLength) {
return;
}
var jpxImage = new JpxImage();
jpxImage.parse(this.bytes);
var width = jpxImage.width;
var height = jpxImage.height;
var componentsCount = jpxImage.componentsCount;
var tileCount = jpxImage.tiles.length;
if (tileCount === 1) {
this.buffer = jpxImage.tiles[0].items;
} else {
var data = new Uint8Array(width * height * componentsCount);
for (var k = 0; k < tileCount; k++) {
var tileComponents = jpxImage.tiles[k];
var tileWidth = tileComponents.width;
var tileHeight = tileComponents.height;
var tileLeft = tileComponents.left;
var tileTop = tileComponents.top;
var src = tileComponents.items;
var srcPosition = 0;
var dataPosition = (width * tileTop + tileLeft) * componentsCount;
var imgRowSize = width * componentsCount;
var tileRowSize = tileWidth * componentsCount;
for (var j = 0; j < tileHeight; j++) {
var rowBytes = src.subarray(srcPosition, srcPosition + tileRowSize);
data.set(rowBytes, dataPosition);
srcPosition += tileRowSize;
dataPosition += imgRowSize;
}
}
this.buffer = data;
}
this.bufferLength = this.buffer.length;
this.eof = true;
};
return JpxStream;
})();
/**
* For JBIG2's we use a library to decode these images and
* the stream behaves like all the other DecodeStreams.
*/
var Jbig2Stream = (function Jbig2StreamClosure() {
function Jbig2Stream(stream, maybeLength, dict) {
this.stream = stream;
this.maybeLength = maybeLength;
this.dict = dict;
DecodeStream.call(this, maybeLength);
}
Jbig2Stream.prototype = Object.create(DecodeStream.prototype);
Object.defineProperty(Jbig2Stream.prototype, "bytes", {
get: function Jbig2Stream_bytes() {
// If this.maybeLength is null, we'll get the entire stream.
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
},
configurable: true
});
Jbig2Stream.prototype.ensureBuffer = function Jbig2Stream_ensureBuffer(
req
) {
if (this.bufferLength) {
return;
}
var jbig2Image = new Jbig2Image();
var chunks = [],
xref = this.dict.xref;
var decodeParams = xref.fetchIfRef(this.dict.get("DecodeParms"));
// According to the PDF specification, DecodeParms can be either
// a dictionary, or an array whose elements are dictionaries.
if (isArray(decodeParams)) {
if (decodeParams.length > 1) {
warn(
"JBIG2 - 'DecodeParms' array with multiple elements " +
"not supported."
);
}
decodeParams = xref.fetchIfRef(decodeParams[0]);
}
if (decodeParams && decodeParams.has("JBIG2Globals")) {
var globalsStream = decodeParams.get("JBIG2Globals");
var globals = globalsStream.getBytes();
chunks.push({ data: globals, start: 0, end: globals.length });
}
chunks.push({ data: this.bytes, start: 0, end: this.bytes.length });
var data = jbig2Image.parseChunks(chunks);
var dataLength = data.length;
// JBIG2 had black as 1 and white as 0, inverting the colors
for (var i = 0; i < dataLength; i++) {
data[i] ^= 0xff;
}
this.buffer = data;
this.bufferLength = dataLength;
this.eof = true;
};
return Jbig2Stream;
})();
var DecryptStream = (function DecryptStreamClosure() {
function DecryptStream(str, maybeLength, decrypt) {
this.str = str;
this.dict = str.dict;
this.decrypt = decrypt;
this.nextChunk = null;
this.initialized = false;
DecodeStream.call(this, maybeLength);
}
var chunkSize = 512;
DecryptStream.prototype = Object.create(DecodeStream.prototype);
DecryptStream.prototype.readBlock = function DecryptStream_readBlock() {
var chunk;
if (this.initialized) {
chunk = this.nextChunk;
} else {
chunk = this.str.getBytes(chunkSize);
this.initialized = true;
}
if (!chunk || chunk.length === 0) {
this.eof = true;
return;
}
this.nextChunk = this.str.getBytes(chunkSize);
var hasMoreData = this.nextChunk && this.nextChunk.length > 0;
var decrypt = this.decrypt;
chunk = decrypt(chunk, !hasMoreData);
var bufferLength = this.bufferLength;
var i,
n = chunk.length;
var buffer = this.ensureBuffer(bufferLength + n);
for (i = 0; i < n; i++) {
buffer[bufferLength++] = chunk[i];
}
this.bufferLength = bufferLength;
};
return DecryptStream;
})();
var Ascii85Stream = (function Ascii85StreamClosure() {
function Ascii85Stream(str, maybeLength) {
this.str = str;
this.dict = str.dict;
this.input = new Uint8Array(5);
// Most streams increase in size when decoded, but Ascii85 streams
// typically shrink by ~20%.
if (maybeLength) {
maybeLength = 0.8 * maybeLength;
}
DecodeStream.call(this, maybeLength);
}
Ascii85Stream.prototype = Object.create(DecodeStream.prototype);
Ascii85Stream.prototype.readBlock = function Ascii85Stream_readBlock() {
var TILDA_CHAR = 0x7e; // '~'
var Z_LOWER_CHAR = 0x7a; // 'z'
var EOF = -1;
var str = this.str;
var c = str.getByte();
while (Lexer.isSpace(c)) {
c = str.getByte();
}
if (c === EOF || c === TILDA_CHAR) {
this.eof = true;
return;
}
var bufferLength = this.bufferLength,
buffer;
var i;
// special code for z
if (c === Z_LOWER_CHAR) {
buffer = this.ensureBuffer(bufferLength + 4);
for (i = 0; i < 4; ++i) {
buffer[bufferLength + i] = 0;
}
this.bufferLength += 4;
} else {
var input = this.input;
input[0] = c;
for (i = 1; i < 5; ++i) {
c = str.getByte();
while (Lexer.isSpace(c)) {
c = str.getByte();
}
input[i] = c;
if (c === EOF || c === TILDA_CHAR) {
break;
}
}
buffer = this.ensureBuffer(bufferLength + i - 1);
this.bufferLength += i - 1;
// partial ending;
if (i < 5) {
for (; i < 5; ++i) {
input[i] = 0x21 + 84;
}
this.eof = true;
}
var t = 0;
for (i = 0; i < 5; ++i) {
t = t * 85 + (input[i] - 0x21);
}
for (i = 3; i >= 0; --i) {
buffer[bufferLength + i] = t & 0xff;
t >>= 8;
}
}
};
return Ascii85Stream;
})();
var AsciiHexStream = (function AsciiHexStreamClosure() {
function AsciiHexStream(str, maybeLength) {
this.str = str;
this.dict = str.dict;
this.firstDigit = -1;
// Most streams increase in size when decoded, but AsciiHex streams shrink
// by 50%.
if (maybeLength) {
maybeLength = 0.5 * maybeLength;
}
DecodeStream.call(this, maybeLength);
}
AsciiHexStream.prototype = Object.create(DecodeStream.prototype);
AsciiHexStream.prototype.readBlock = function AsciiHexStream_readBlock() {
var UPSTREAM_BLOCK_SIZE = 8000;
var bytes = this.str.getBytes(UPSTREAM_BLOCK_SIZE);
if (!bytes.length) {
this.eof = true;
return;
}
var maxDecodeLength = (bytes.length + 1) >> 1;
var buffer = this.ensureBuffer(this.bufferLength + maxDecodeLength);
var bufferLength = this.bufferLength;
var firstDigit = this.firstDigit;
for (var i = 0, ii = bytes.length; i < ii; i++) {
var ch = bytes[i],
digit;
if (ch >= 0x30 && ch <= 0x39) {
// '0'-'9'
digit = ch & 0x0f;
} else if ((ch >= 0x41 && ch <= 0x46) || (ch >= 0x61 && ch <= 0x66)) {
// 'A'-'Z', 'a'-'z'
digit = (ch & 0x0f) + 9;
} else if (ch === 0x3e) {
// '>'
this.eof = true;
break;
} else {
// probably whitespace
continue; // ignoring
}
if (firstDigit < 0) {
firstDigit = digit;
} else {
buffer[bufferLength++] = (firstDigit << 4) | digit;
firstDigit = -1;
}
}
if (firstDigit >= 0 && this.eof) {
// incomplete byte
buffer[bufferLength++] = firstDigit << 4;
firstDigit = -1;
}
this.firstDigit = firstDigit;
this.bufferLength = bufferLength;
};
return AsciiHexStream;
})();
var RunLengthStream = (function RunLengthStreamClosure() {
function RunLengthStream(str, maybeLength) {
this.str = str;
this.dict = str.dict;
DecodeStream.call(this, maybeLength);
}
RunLengthStream.prototype = Object.create(DecodeStream.prototype);
RunLengthStream.prototype.readBlock = function RunLengthStream_readBlock() {
// The repeatHeader has following format. The first byte defines type of run
// and amount of bytes to repeat/copy: n = 0 through 127 - copy next n bytes
// (in addition to the second byte from the header), n = 129 through 255 -
// duplicate the second byte from the header (257 - n) times, n = 128 - end.
var repeatHeader = this.str.getBytes(2);
if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] === 128) {
this.eof = true;
return;
}
var buffer;
var bufferLength = this.bufferLength;
var n = repeatHeader[0];
if (n < 128) {
// copy n bytes
buffer = this.ensureBuffer(bufferLength + n + 1);
buffer[bufferLength++] = repeatHeader[1];
if (n > 0) {
var source = this.str.getBytes(n);
buffer.set(source, bufferLength);
bufferLength += n;
}
} else {
n = 257 - n;
var b = repeatHeader[1];
buffer = this.ensureBuffer(bufferLength + n + 1);
for (var i = 0; i < n; i++) {
buffer[bufferLength++] = b;
}
}
this.bufferLength = bufferLength;
};
return RunLengthStream;
})();
var CCITTFaxStream = (function CCITTFaxStreamClosure() {
var ccittEOL = -2;
var twoDimPass = 0;
var twoDimHoriz = 1;
var twoDimVert0 = 2;
var twoDimVertR1 = 3;
var twoDimVertL1 = 4;
var twoDimVertR2 = 5;
var twoDimVertL2 = 6;
var twoDimVertR3 = 7;
var twoDimVertL3 = 8;
var twoDimTable = [
[-1, -1],
[-1, -1], // 000000x
[7, twoDimVertL3], // 0000010
[7, twoDimVertR3], // 0000011
[6, twoDimVertL2],
[6, twoDimVertL2], // 000010x
[6, twoDimVertR2],
[6, twoDimVertR2], // 000011x
[4, twoDimPass],
[4, twoDimPass], // 0001xxx
[4, twoDimPass],
[4, twoDimPass],
[4, twoDimPass],
[4, twoDimPass],
[4, twoDimPass],
[4, twoDimPass],
[3, twoDimHoriz],
[3, twoDimHoriz], // 001xxxx
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimHoriz],
[3, twoDimVertL1],
[3, twoDimVertL1], // 010xxxx
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertL1],
[3, twoDimVertR1],
[3, twoDimVertR1], // 011xxxx
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[3, twoDimVertR1],
[1, twoDimVert0],
[1, twoDimVert0], // 1xxxxxx
[1, twoDimVert0],