Mercurial > hg > orthanc
annotate UnitTestsSources/Png.cpp @ 786:b6d6b65142e8
DicomModification
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 05 May 2014 13:07:10 +0200 |
parents | 8f62e8d5a384 |
children | 37adac56017a |
rev | line source |
---|---|
368 | 1 #include "gtest/gtest.h" |
2 | |
3 #include <stdint.h> | |
455 | 4 #include "../Core/FileFormats/PngReader.h" |
454 | 5 #include "../Core/FileFormats/PngWriter.h" |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
6 #include "../Core/Toolbox.h" |
709 | 7 #include "../Core/Uuid.h" |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
8 |
368 | 9 |
10 TEST(PngWriter, ColorPattern) | |
11 { | |
12 Orthanc::PngWriter w; | |
13 int width = 17; | |
14 int height = 61; | |
15 int pitch = width * 3; | |
16 | |
17 std::vector<uint8_t> image(height * pitch); | |
18 for (int y = 0; y < height; y++) | |
19 { | |
20 uint8_t *p = &image[0] + y * pitch; | |
21 for (int x = 0; x < width; x++, p += 3) | |
22 { | |
23 p[0] = (y % 3 == 0) ? 255 : 0; | |
24 p[1] = (y % 3 == 1) ? 255 : 0; | |
25 p[2] = (y % 3 == 2) ? 255 : 0; | |
26 } | |
27 } | |
28 | |
29 w.WriteToFile("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
|
30 |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
31 std::string f, md5; |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
32 Orthanc::Toolbox::ReadFile(f, "ColorPattern.png"); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
33 Orthanc::Toolbox::ComputeMD5(md5, f); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
34 ASSERT_EQ("604e785f53c99cae6ea4584870b2c41d", md5); |
368 | 35 } |
36 | |
37 TEST(PngWriter, Gray8Pattern) | |
38 { | |
39 Orthanc::PngWriter w; | |
40 int width = 17; | |
41 int height = 256; | |
42 int pitch = width; | |
43 | |
44 std::vector<uint8_t> image(height * pitch); | |
45 for (int y = 0; y < height; y++) | |
46 { | |
47 uint8_t *p = &image[0] + y * pitch; | |
48 for (int x = 0; x < width; x++, p++) | |
49 { | |
50 *p = y; | |
51 } | |
52 } | |
53 | |
54 w.WriteToFile("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
|
55 |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
56 std::string f, md5; |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
57 Orthanc::Toolbox::ReadFile(f, "Gray8Pattern.png"); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
58 Orthanc::Toolbox::ComputeMD5(md5, f); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
59 ASSERT_EQ("5a9b98bea3d0a6d983980cc38bfbcdb3", md5); |
368 | 60 } |
61 | |
62 TEST(PngWriter, Gray16Pattern) | |
63 { | |
64 Orthanc::PngWriter w; | |
65 int width = 256; | |
66 int height = 256; | |
369
4632a044746e
simplification of the code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
368
diff
changeset
|
67 int pitch = width * 2 + 16; |
368 | 68 |
69 std::vector<uint8_t> image(height * pitch); | |
369
4632a044746e
simplification of the code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
368
diff
changeset
|
70 |
368 | 71 int v = 0; |
72 for (int y = 0; y < height; y++) | |
73 { | |
74 uint16_t *p = reinterpret_cast<uint16_t*>(&image[0] + y * pitch); | |
75 for (int x = 0; x < width; x++, p++, v++) | |
76 { | |
77 *p = v; | |
78 } | |
79 } | |
80 | |
81 w.WriteToFile("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
|
82 |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
83 std::string f, md5; |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
84 Orthanc::Toolbox::ReadFile(f, "Gray16Pattern.png"); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
85 Orthanc::Toolbox::ComputeMD5(md5, f); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
632
diff
changeset
|
86 ASSERT_EQ("0785866a08bf0a02d2eeff87f658571c", md5); |
368 | 87 } |
455 | 88 |
89 TEST(PngWriter, EndToEnd) | |
90 { | |
91 Orthanc::PngWriter w; | |
92 int width = 256; | |
93 int height = 256; | |
94 int pitch = width * 2 + 16; | |
95 | |
96 std::vector<uint8_t> image(height * pitch); | |
97 | |
98 int v = 0; | |
99 for (int y = 0; y < height; y++) | |
100 { | |
101 uint16_t *p = reinterpret_cast<uint16_t*>(&image[0] + y * pitch); | |
102 for (int x = 0; x < width; x++, p++, v++) | |
103 { | |
104 *p = v; | |
105 } | |
106 } | |
107 | |
108 std::string s; | |
109 w.WriteToMemory(s, width, height, pitch, Orthanc::PixelFormat_Grayscale16, &image[0]); | |
110 | |
709 | 111 { |
112 Orthanc::PngReader r; | |
113 r.ReadFromMemory(s); | |
455 | 114 |
710 | 115 ASSERT_EQ(r.GetFormat(), Orthanc::PixelFormat_Grayscale16); |
709 | 116 ASSERT_EQ(r.GetWidth(), width); |
117 ASSERT_EQ(r.GetHeight(), height); | |
455 | 118 |
709 | 119 v = 0; |
120 for (int y = 0; y < height; y++) | |
121 { | |
122 uint16_t *p = reinterpret_cast<uint16_t*>((uint8_t*) r.GetBuffer() + y * r.GetPitch()); | |
711
8f62e8d5a384
test main dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
710
diff
changeset
|
123 ASSERT_EQ(p, r.GetBuffer(y)); |
709 | 124 for (int x = 0; x < width; x++, p++, v++) |
125 { | |
126 ASSERT_EQ(*p, v); | |
127 } | |
128 } | |
129 } | |
130 | |
455 | 131 { |
710 | 132 Orthanc::Toolbox::TemporaryFile tmp; |
133 Orthanc::Toolbox::WriteFile(s, tmp.GetPath()); | |
709 | 134 |
135 Orthanc::PngReader r2; | |
136 r2.ReadFromFile(tmp.GetPath()); | |
137 | |
710 | 138 ASSERT_EQ(r2.GetFormat(), Orthanc::PixelFormat_Grayscale16); |
709 | 139 ASSERT_EQ(r2.GetWidth(), width); |
140 ASSERT_EQ(r2.GetHeight(), height); | |
710 | 141 |
142 v = 0; | |
709 | 143 for (int y = 0; y < height; y++) |
455 | 144 { |
709 | 145 uint16_t *p = reinterpret_cast<uint16_t*>((uint8_t*) r2.GetBuffer() + y * r2.GetPitch()); |
711
8f62e8d5a384
test main dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
710
diff
changeset
|
146 ASSERT_EQ(p, r2.GetBuffer(y)); |
709 | 147 for (int x = 0; x < width; x++, p++, v++) |
148 { | |
149 ASSERT_EQ(*p, v); | |
150 } | |
455 | 151 } |
152 } | |
153 } |