Mercurial > hg > orthanc
annotate UnitTestsSources/ImageTests.cpp @ 1602:292bce3f54ed
JpegWriter
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 01 Sep 2015 13:06:39 +0200 |
parents | UnitTestsSources/PngTests.cpp@9f66a12eb8fc |
children | 1f5d6a2f9638 |
rev | line source |
---|---|
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
1 /** |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
5 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
6 * This program is free software: you can redistribute it and/or |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
7 * modify it under the terms of the GNU General Public License as |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
8 * published by the Free Software Foundation, either version 3 of the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
9 * License, or (at your option) any later version. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
10 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
11 * In addition, as a special exception, the copyright holders of this |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
12 * program give permission to link the code of its release with the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
14 * that use the same license as the "OpenSSL" library), and distribute |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
15 * the linked executables. You must obey the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
16 * in all respects for all of the code used other than "OpenSSL". If you |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
17 * modify file(s) with this exception, you may extend this exception to |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
18 * your version of the file(s), but you are not obligated to do so. If |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
19 * you do not wish to do so, delete this exception statement from your |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
20 * version. If you delete this exception statement from all source files |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
21 * in the program, then also delete it here. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
22 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
23 * This program is distributed in the hope that it will be useful, but |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
24 * WITHOUT ANY WARRANTY; without even the implied warranty of |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
26 * General Public License for more details. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
27 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
28 * You should have received a copy of the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
30 **/ |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
31 |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
32 |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
827
diff
changeset
|
33 #include "PrecompiledHeadersUnitTests.h" |
368 | 34 #include "gtest/gtest.h" |
35 | |
36 #include <stdint.h> | |
1602 | 37 #include "../Core/ImageFormats/ImageBuffer.h" |
799 | 38 #include "../Core/ImageFormats/PngReader.h" |
39 #include "../Core/ImageFormats/PngWriter.h" | |
1602 | 40 #include "../Core/ImageFormats/JpegWriter.h" |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
41 #include "../Core/Toolbox.h" |
709 | 42 #include "../Core/Uuid.h" |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
43 |
368 | 44 |
45 TEST(PngWriter, ColorPattern) | |
46 { | |
47 Orthanc::PngWriter w; | |
48 int width = 17; | |
49 int height = 61; | |
50 int pitch = width * 3; | |
51 | |
52 std::vector<uint8_t> image(height * pitch); | |
53 for (int y = 0; y < height; y++) | |
54 { | |
55 uint8_t *p = &image[0] + y * pitch; | |
56 for (int x = 0; x < width; x++, p += 3) | |
57 { | |
58 p[0] = (y % 3 == 0) ? 255 : 0; | |
59 p[1] = (y % 3 == 1) ? 255 : 0; | |
60 p[2] = (y % 3 == 2) ? 255 : 0; | |
61 } | |
62 } | |
63 | |
803
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
64 w.WriteToFile("UnitTestsResults/ColorPattern.png", width, height, pitch, Orthanc::PixelFormat_RGB24, &image[0]); |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
65 |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
66 std::string f, md5; |
803
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
67 Orthanc::Toolbox::ReadFile(f, "UnitTestsResults/ColorPattern.png"); |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
68 Orthanc::Toolbox::ComputeMD5(md5, f); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
69 ASSERT_EQ("604e785f53c99cae6ea4584870b2c41d", md5); |
368 | 70 } |
71 | |
72 TEST(PngWriter, Gray8Pattern) | |
73 { | |
74 Orthanc::PngWriter w; | |
75 int width = 17; | |
76 int height = 256; | |
77 int pitch = width; | |
78 | |
79 std::vector<uint8_t> image(height * pitch); | |
80 for (int y = 0; y < height; y++) | |
81 { | |
82 uint8_t *p = &image[0] + y * pitch; | |
83 for (int x = 0; x < width; x++, p++) | |
84 { | |
85 *p = y; | |
86 } | |
87 } | |
88 | |
803
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
89 w.WriteToFile("UnitTestsResults/Gray8Pattern.png", width, height, pitch, Orthanc::PixelFormat_Grayscale8, &image[0]); |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
90 |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
91 std::string f, md5; |
803
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
92 Orthanc::Toolbox::ReadFile(f, "UnitTestsResults/Gray8Pattern.png"); |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
93 Orthanc::Toolbox::ComputeMD5(md5, f); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
94 ASSERT_EQ("5a9b98bea3d0a6d983980cc38bfbcdb3", md5); |
368 | 95 } |
96 | |
97 TEST(PngWriter, Gray16Pattern) | |
98 { | |
99 Orthanc::PngWriter w; | |
100 int width = 256; | |
101 int height = 256; | |
369
4632a044746e
simplification of the code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
368
diff
changeset
|
102 int pitch = width * 2 + 16; |
368 | 103 |
104 std::vector<uint8_t> image(height * pitch); | |
369
4632a044746e
simplification of the code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
368
diff
changeset
|
105 |
368 | 106 int v = 0; |
107 for (int y = 0; y < height; y++) | |
108 { | |
109 uint16_t *p = reinterpret_cast<uint16_t*>(&image[0] + y * pitch); | |
110 for (int x = 0; x < width; x++, p++, v++) | |
111 { | |
112 *p = v; | |
113 } | |
114 } | |
115 | |
803
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
116 w.WriteToFile("UnitTestsResults/Gray16Pattern.png", width, height, pitch, Orthanc::PixelFormat_Grayscale16, &image[0]); |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
117 |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
118 std::string f, md5; |
803
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
119 Orthanc::Toolbox::ReadFile(f, "UnitTestsResults/Gray16Pattern.png"); |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
120 Orthanc::Toolbox::ComputeMD5(md5, f); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
121 ASSERT_EQ("0785866a08bf0a02d2eeff87f658571c", md5); |
368 | 122 } |
455 | 123 |
124 TEST(PngWriter, EndToEnd) | |
125 { | |
126 Orthanc::PngWriter w; | |
1492 | 127 unsigned int width = 256; |
128 unsigned int height = 256; | |
129 unsigned int pitch = width * 2 + 16; | |
455 | 130 |
131 std::vector<uint8_t> image(height * pitch); | |
132 | |
133 int v = 0; | |
1492 | 134 for (unsigned int y = 0; y < height; y++) |
455 | 135 { |
136 uint16_t *p = reinterpret_cast<uint16_t*>(&image[0] + y * pitch); | |
1492 | 137 for (unsigned int x = 0; x < width; x++, p++, v++) |
455 | 138 { |
139 *p = v; | |
140 } | |
141 } | |
142 | |
143 std::string s; | |
144 w.WriteToMemory(s, width, height, pitch, Orthanc::PixelFormat_Grayscale16, &image[0]); | |
145 | |
709 | 146 { |
147 Orthanc::PngReader r; | |
148 r.ReadFromMemory(s); | |
455 | 149 |
710 | 150 ASSERT_EQ(r.GetFormat(), Orthanc::PixelFormat_Grayscale16); |
709 | 151 ASSERT_EQ(r.GetWidth(), width); |
152 ASSERT_EQ(r.GetHeight(), height); | |
455 | 153 |
709 | 154 v = 0; |
1492 | 155 for (unsigned int y = 0; y < height; y++) |
709 | 156 { |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
711
diff
changeset
|
157 const uint16_t *p = reinterpret_cast<const uint16_t*>((const uint8_t*) r.GetConstBuffer() + y * r.GetPitch()); |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
711
diff
changeset
|
158 ASSERT_EQ(p, r.GetConstRow(y)); |
1492 | 159 for (unsigned int x = 0; x < width; x++, p++, v++) |
709 | 160 { |
161 ASSERT_EQ(*p, v); | |
162 } | |
163 } | |
164 } | |
165 | |
455 | 166 { |
710 | 167 Orthanc::Toolbox::TemporaryFile tmp; |
168 Orthanc::Toolbox::WriteFile(s, tmp.GetPath()); | |
709 | 169 |
170 Orthanc::PngReader r2; | |
171 r2.ReadFromFile(tmp.GetPath()); | |
172 | |
710 | 173 ASSERT_EQ(r2.GetFormat(), Orthanc::PixelFormat_Grayscale16); |
709 | 174 ASSERT_EQ(r2.GetWidth(), width); |
175 ASSERT_EQ(r2.GetHeight(), height); | |
710 | 176 |
177 v = 0; | |
1492 | 178 for (unsigned int y = 0; y < height; y++) |
455 | 179 { |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
711
diff
changeset
|
180 const uint16_t *p = reinterpret_cast<const uint16_t*>((const uint8_t*) r2.GetConstBuffer() + y * r2.GetPitch()); |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
711
diff
changeset
|
181 ASSERT_EQ(p, r2.GetConstRow(y)); |
1492 | 182 for (unsigned int x = 0; x < width; x++, p++, v++) |
709 | 183 { |
184 ASSERT_EQ(*p, v); | |
185 } | |
455 | 186 } |
187 } | |
188 } | |
1602 | 189 |
190 | |
191 | |
192 | |
193 TEST(JpegWriter, Basic) | |
194 { | |
195 Orthanc::ImageBuffer img(16, 16, Orthanc::PixelFormat_Grayscale8); | |
196 Orthanc::ImageAccessor accessor = img.GetAccessor(); | |
197 for (unsigned int y = 0, value = 0; y < img.GetHeight(); y++) | |
198 { | |
199 uint8_t* p = reinterpret_cast<uint8_t*>(accessor.GetRow(y)); | |
200 for (unsigned int x = 0; x < img.GetWidth(); x++, p++) | |
201 { | |
202 *p = value++; | |
203 } | |
204 } | |
205 | |
206 Orthanc::JpegWriter w; | |
207 w.WriteToFile("UnitTestsResults/hello.jpg", accessor); | |
208 | |
209 std::string s; | |
210 w.WriteToMemory(s, accessor); | |
211 Orthanc::Toolbox::WriteFile(s, "UnitTestsResults/hello2.jpg"); | |
212 } |