Committed by
David PHAM-VAN
Fix PdfImage constructor without alpha channel
Showing
3 changed files
with
105 additions
and
4 deletions
@@ -10,6 +10,7 @@ | @@ -10,6 +10,7 @@ | ||
10 | - Allow saving an unmodified document | 10 | - Allow saving an unmodified document |
11 | - Table cell: dynamic widget [Shahriyar Aghajani] | 11 | - Table cell: dynamic widget [Shahriyar Aghajani] |
12 | - Move Table.fromTextArray to TableHelper.fromTextArray | 12 | - Move Table.fromTextArray to TableHelper.fromTextArray |
13 | +- Fix PdfImage constructor without alpha channel [Tomasz Gucio] | ||
13 | 14 | ||
14 | ## 3.10.1 | 15 | ## 3.10.1 |
15 | 16 |
@@ -97,10 +97,18 @@ class PdfImage extends PdfXObject { | @@ -97,10 +97,18 @@ class PdfImage extends PdfXObject { | ||
97 | final h = height; | 97 | final h = height; |
98 | final s = w * h; | 98 | final s = w * h; |
99 | final out = Uint8List(s * 3); | 99 | final out = Uint8List(s * 3); |
100 | - for (var i = 0; i < s; i++) { | ||
101 | - out[i * 3] = image[i * 4]; | ||
102 | - out[i * 3 + 1] = image[i * 4 + 1]; | ||
103 | - out[i * 3 + 2] = image[i * 4 + 2]; | 100 | + if (alpha) { |
101 | + for (var i = 0; i < s; i++) { | ||
102 | + out[i * 3] = image[i * 4]; | ||
103 | + out[i * 3 + 1] = image[i * 4 + 1]; | ||
104 | + out[i * 3 + 2] = image[i * 4 + 2]; | ||
105 | + } | ||
106 | + } else { | ||
107 | + for (var i = 0; i < s; i++) { | ||
108 | + out[i * 3] = image[i * 3]; | ||
109 | + out[i * 3 + 1] = image[i * 3 + 1]; | ||
110 | + out[i * 3 + 2] = image[i * 3 + 2]; | ||
111 | + } | ||
104 | } | 112 | } |
105 | 113 | ||
106 | im.buf.putBytes(out); | 114 | im.buf.putBytes(out); |
pdf/test/image_test.dart
0 → 100644
1 | +/* | ||
2 | + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com> | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +import 'dart:typed_data'; | ||
18 | + | ||
19 | +import 'package:pdf/pdf.dart'; | ||
20 | +import 'package:pdf/src/priv.dart'; | ||
21 | +import 'package:test/test.dart'; | ||
22 | + | ||
23 | +const kRedValue = 110; | ||
24 | +const kGreenValue = 120; | ||
25 | +const kBlueValue = 130; | ||
26 | +const kAlphaValue = 200; | ||
27 | + | ||
28 | +Uint8List createTestImage(int width, int height, {bool withAlpha = true}) { | ||
29 | + final channelCount = withAlpha ? 4 : 3; | ||
30 | + final img = Uint8List(width * height * channelCount); | ||
31 | + for (var pixelIndex = 0; pixelIndex < width * height; pixelIndex++) { | ||
32 | + img[pixelIndex * channelCount] = kRedValue; | ||
33 | + img[pixelIndex * channelCount + 1] = kGreenValue; | ||
34 | + img[pixelIndex * channelCount + 2] = kBlueValue; | ||
35 | + if (channelCount == 4) { | ||
36 | + img[pixelIndex * channelCount + 3] = kAlphaValue; | ||
37 | + } | ||
38 | + } | ||
39 | + | ||
40 | + return img; | ||
41 | +} | ||
42 | + | ||
43 | +void main() { | ||
44 | + test('PdfImage constructor with alpha channel', () async { | ||
45 | + final img = createTestImage(300, 200); | ||
46 | + | ||
47 | + final pdf = PdfDocument(); | ||
48 | + | ||
49 | + final image = PdfImage( | ||
50 | + pdf, | ||
51 | + image: img.buffer.asUint8List(), | ||
52 | + width: 300, | ||
53 | + height: 200, | ||
54 | + ); | ||
55 | + | ||
56 | + expect(image.params['/Width'], const PdfNum(300)); | ||
57 | + expect(image.params['/Height'], const PdfNum(200)); | ||
58 | + expect(image.params['/SMask'], isA<PdfIndirect>()); | ||
59 | + | ||
60 | + final buf = image.buf.output(); | ||
61 | + for (var pixelIndex = 0; pixelIndex < 300 * 200; pixelIndex++) { | ||
62 | + expect(buf[pixelIndex * 3], kRedValue); | ||
63 | + expect(buf[pixelIndex * 3 + 1], kGreenValue); | ||
64 | + expect(buf[pixelIndex * 3 + 2], kBlueValue); | ||
65 | + } | ||
66 | + }); | ||
67 | + | ||
68 | + test('PdfImage constructor without alpha channel', () async { | ||
69 | + final img = createTestImage(300, 200, withAlpha: false); | ||
70 | + | ||
71 | + final pdf = PdfDocument(); | ||
72 | + | ||
73 | + final image = PdfImage( | ||
74 | + pdf, | ||
75 | + image: img.buffer.asUint8List(), | ||
76 | + width: 300, | ||
77 | + height: 200, | ||
78 | + alpha: false, | ||
79 | + ); | ||
80 | + | ||
81 | + expect(image.params['/Width'], const PdfNum(300)); | ||
82 | + expect(image.params['/Height'], const PdfNum(200)); | ||
83 | + expect(image.params['/SMask'], isNull); | ||
84 | + | ||
85 | + final buf = image.buf.output(); | ||
86 | + for (var pixelIndex = 0; pixelIndex < 300 * 200; pixelIndex++) { | ||
87 | + expect(buf[pixelIndex * 3], kRedValue); | ||
88 | + expect(buf[pixelIndex * 3 + 1], kGreenValue); | ||
89 | + expect(buf[pixelIndex * 3 + 2], kBlueValue); | ||
90 | + } | ||
91 | + }); | ||
92 | +} |
-
Please register or login to post a comment