comparison UnitTestsSources/PngTests.cpp @ 967:dfc076546821

add suffix Tests to unit test sources
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Jun 2014 15:36:38 +0200
parents UnitTestsSources/Png.cpp@84513f2ee1f3
children 6e7e5ed91c2d
comparison
equal deleted inserted replaced
966:886652370ff2 967:dfc076546821
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "PrecompiledHeadersUnitTests.h"
34 #include "gtest/gtest.h"
35
36 #include <stdint.h>
37 #include "../Core/ImageFormats/PngReader.h"
38 #include "../Core/ImageFormats/PngWriter.h"
39 #include "../Core/Toolbox.h"
40 #include "../Core/Uuid.h"
41
42
43 TEST(PngWriter, ColorPattern)
44 {
45 Orthanc::PngWriter w;
46 int width = 17;
47 int height = 61;
48 int pitch = width * 3;
49
50 std::vector<uint8_t> image(height * pitch);
51 for (int y = 0; y < height; y++)
52 {
53 uint8_t *p = &image[0] + y * pitch;
54 for (int x = 0; x < width; x++, p += 3)
55 {
56 p[0] = (y % 3 == 0) ? 255 : 0;
57 p[1] = (y % 3 == 1) ? 255 : 0;
58 p[2] = (y % 3 == 2) ? 255 : 0;
59 }
60 }
61
62 w.WriteToFile("UnitTestsResults/ColorPattern.png", width, height, pitch, Orthanc::PixelFormat_RGB24, &image[0]);
63
64 std::string f, md5;
65 Orthanc::Toolbox::ReadFile(f, "UnitTestsResults/ColorPattern.png");
66 Orthanc::Toolbox::ComputeMD5(md5, f);
67 ASSERT_EQ("604e785f53c99cae6ea4584870b2c41d", md5);
68 }
69
70 TEST(PngWriter, Gray8Pattern)
71 {
72 Orthanc::PngWriter w;
73 int width = 17;
74 int height = 256;
75 int pitch = width;
76
77 std::vector<uint8_t> image(height * pitch);
78 for (int y = 0; y < height; y++)
79 {
80 uint8_t *p = &image[0] + y * pitch;
81 for (int x = 0; x < width; x++, p++)
82 {
83 *p = y;
84 }
85 }
86
87 w.WriteToFile("UnitTestsResults/Gray8Pattern.png", width, height, pitch, Orthanc::PixelFormat_Grayscale8, &image[0]);
88
89 std::string f, md5;
90 Orthanc::Toolbox::ReadFile(f, "UnitTestsResults/Gray8Pattern.png");
91 Orthanc::Toolbox::ComputeMD5(md5, f);
92 ASSERT_EQ("5a9b98bea3d0a6d983980cc38bfbcdb3", md5);
93 }
94
95 TEST(PngWriter, Gray16Pattern)
96 {
97 Orthanc::PngWriter w;
98 int width = 256;
99 int height = 256;
100 int pitch = width * 2 + 16;
101
102 std::vector<uint8_t> image(height * pitch);
103
104 int v = 0;
105 for (int y = 0; y < height; y++)
106 {
107 uint16_t *p = reinterpret_cast<uint16_t*>(&image[0] + y * pitch);
108 for (int x = 0; x < width; x++, p++, v++)
109 {
110 *p = v;
111 }
112 }
113
114 w.WriteToFile("UnitTestsResults/Gray16Pattern.png", width, height, pitch, Orthanc::PixelFormat_Grayscale16, &image[0]);
115
116 std::string f, md5;
117 Orthanc::Toolbox::ReadFile(f, "UnitTestsResults/Gray16Pattern.png");
118 Orthanc::Toolbox::ComputeMD5(md5, f);
119 ASSERT_EQ("0785866a08bf0a02d2eeff87f658571c", md5);
120 }
121
122 TEST(PngWriter, EndToEnd)
123 {
124 Orthanc::PngWriter w;
125 int width = 256;
126 int height = 256;
127 int pitch = width * 2 + 16;
128
129 std::vector<uint8_t> image(height * pitch);
130
131 int v = 0;
132 for (int y = 0; y < height; y++)
133 {
134 uint16_t *p = reinterpret_cast<uint16_t*>(&image[0] + y * pitch);
135 for (int x = 0; x < width; x++, p++, v++)
136 {
137 *p = v;
138 }
139 }
140
141 std::string s;
142 w.WriteToMemory(s, width, height, pitch, Orthanc::PixelFormat_Grayscale16, &image[0]);
143
144 {
145 Orthanc::PngReader r;
146 r.ReadFromMemory(s);
147
148 ASSERT_EQ(r.GetFormat(), Orthanc::PixelFormat_Grayscale16);
149 ASSERT_EQ(r.GetWidth(), width);
150 ASSERT_EQ(r.GetHeight(), height);
151
152 v = 0;
153 for (int y = 0; y < height; y++)
154 {
155 const uint16_t *p = reinterpret_cast<const uint16_t*>((const uint8_t*) r.GetConstBuffer() + y * r.GetPitch());
156 ASSERT_EQ(p, r.GetConstRow(y));
157 for (int x = 0; x < width; x++, p++, v++)
158 {
159 ASSERT_EQ(*p, v);
160 }
161 }
162 }
163
164 {
165 Orthanc::Toolbox::TemporaryFile tmp;
166 Orthanc::Toolbox::WriteFile(s, tmp.GetPath());
167
168 Orthanc::PngReader r2;
169 r2.ReadFromFile(tmp.GetPath());
170
171 ASSERT_EQ(r2.GetFormat(), Orthanc::PixelFormat_Grayscale16);
172 ASSERT_EQ(r2.GetWidth(), width);
173 ASSERT_EQ(r2.GetHeight(), height);
174
175 v = 0;
176 for (int y = 0; y < height; y++)
177 {
178 const uint16_t *p = reinterpret_cast<const uint16_t*>((const uint8_t*) r2.GetConstBuffer() + y * r2.GetPitch());
179 ASSERT_EQ(p, r2.GetConstRow(y));
180 for (int x = 0; x < width; x++, p++, v++)
181 {
182 ASSERT_EQ(*p, v);
183 }
184 }
185 }
186 }