1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // TODO(tsepez) this requires a lot more testing.
6 
7 #include <stdint.h>
8 
9 #include "core/fxcodec/jbig2/JBig2_Image.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace {
13 
14 const int32_t kWidthPixels = 80;
15 const int32_t kWidthBytes = 10;
16 const int32_t kStrideBytes = kWidthBytes + 1;  // For testing stride != width.
17 const int32_t kHeightLines = 20;
18 const int32_t kLargerHeightLines = 100;
19 const int32_t kTooLargeHeightLines = 40000000;
20 
21 }  // namespace
22 
TEST(fxcodec,JBig2ImageCreate)23 TEST(fxcodec, JBig2ImageCreate) {
24   CJBig2_Image img(kWidthPixels, kHeightLines);
25   img.setPixel(0, 0, true);
26   img.setPixel(kWidthPixels - 1, kHeightLines - 1, false);
27   EXPECT_EQ(kWidthPixels, img.width());
28   EXPECT_EQ(kHeightLines, img.height());
29   EXPECT_TRUE(img.getPixel(0, 0));
30   EXPECT_FALSE(img.getPixel(kWidthPixels - 1, kHeightLines - 1));
31 }
32 
TEST(fxcodec,JBig2ImageCreateTooBig)33 TEST(fxcodec, JBig2ImageCreateTooBig) {
34   CJBig2_Image img(kWidthPixels, kTooLargeHeightLines);
35   EXPECT_EQ(0, img.width());
36   EXPECT_EQ(0, img.height());
37   EXPECT_EQ(nullptr, img.m_pData);
38 }
39 
TEST(fxcodec,JBig2ImageCreateExternal)40 TEST(fxcodec, JBig2ImageCreateExternal) {
41   uint8_t buf[kHeightLines * kStrideBytes];
42   CJBig2_Image img(kWidthPixels, kHeightLines, kStrideBytes, buf);
43   img.setPixel(0, 0, true);
44   img.setPixel(kWidthPixels - 1, kHeightLines - 1, false);
45   EXPECT_EQ(kWidthPixels, img.width());
46   EXPECT_EQ(kHeightLines, img.height());
47   EXPECT_TRUE(img.getPixel(0, 0));
48   EXPECT_FALSE(img.getPixel(kWidthPixels - 1, kHeightLines - 1));
49 }
50 
TEST(fxcodec,JBig2ImageCreateExternalTooBig)51 TEST(fxcodec, JBig2ImageCreateExternalTooBig) {
52   uint8_t buf[kHeightLines * kStrideBytes];
53   CJBig2_Image img(kWidthPixels, kTooLargeHeightLines, kStrideBytes, buf);
54   EXPECT_EQ(0, img.width());
55   EXPECT_EQ(0, img.height());
56   EXPECT_EQ(nullptr, img.m_pData);
57 }
58 
TEST(fxcodec,JBig2ImageExpand)59 TEST(fxcodec, JBig2ImageExpand) {
60   CJBig2_Image img(kWidthPixels, kHeightLines);
61   img.setPixel(0, 0, true);
62   img.setPixel(kWidthPixels - 1, kHeightLines - 1, false);
63   img.expand(kLargerHeightLines, true);
64   EXPECT_EQ(kWidthPixels, img.width());
65   EXPECT_EQ(kLargerHeightLines, img.height());
66   EXPECT_TRUE(img.getPixel(0, 0));
67   EXPECT_FALSE(img.getPixel(kWidthPixels - 1, kHeightLines - 1));
68   EXPECT_TRUE(img.getPixel(kWidthPixels - 1, kLargerHeightLines - 1));
69 }
70 
TEST(fxcodec,JBig2ImageExpandTooBig)71 TEST(fxcodec, JBig2ImageExpandTooBig) {
72   CJBig2_Image img(kWidthPixels, kHeightLines);
73   img.setPixel(0, 0, true);
74   img.setPixel(kWidthPixels - 1, kHeightLines - 1, false);
75   img.expand(kTooLargeHeightLines, true);
76   EXPECT_EQ(kWidthPixels, img.width());
77   EXPECT_EQ(kHeightLines, img.height());
78   EXPECT_TRUE(img.getPixel(0, 0));
79   EXPECT_FALSE(img.getPixel(kWidthPixels - 1, kHeightLines - 1));
80 }
81 
TEST(fxcodec,JBig2ImageExpandExternal)82 TEST(fxcodec, JBig2ImageExpandExternal) {
83   uint8_t buf[kHeightLines * kStrideBytes];
84   CJBig2_Image img(kWidthPixels, kHeightLines, kStrideBytes, buf);
85   img.setPixel(0, 0, true);
86   img.setPixel(kWidthPixels - 1, kHeightLines - 1, false);
87   img.expand(kLargerHeightLines, true);
88   EXPECT_EQ(kWidthPixels, img.width());
89   EXPECT_EQ(kLargerHeightLines, img.height());
90   EXPECT_TRUE(img.getPixel(0, 0));
91   EXPECT_FALSE(img.getPixel(kWidthPixels - 1, kHeightLines - 1));
92   EXPECT_TRUE(img.getPixel(kWidthPixels - 1, kLargerHeightLines - 1));
93 }
94 
TEST(fxcodec,JBig2ImageExpandExternalTooBig)95 TEST(fxcodec, JBig2ImageExpandExternalTooBig) {
96   uint8_t buf[kHeightLines * kStrideBytes];
97   CJBig2_Image img(kWidthPixels, kHeightLines, kStrideBytes, buf);
98   img.setPixel(0, 0, true);
99   img.setPixel(kWidthPixels - 1, kHeightLines - 1, false);
100   img.expand(kTooLargeHeightLines, true);
101   EXPECT_EQ(kWidthPixels, img.width());
102   EXPECT_EQ(kHeightLines, img.height());
103   EXPECT_TRUE(img.getPixel(0, 0));
104   EXPECT_FALSE(img.getPixel(kWidthPixels - 1, kHeightLines - 1));
105 }
106