comparison UnitTestsSources/Png.cpp @ 762:45b16f67259c lua-scripting

integration mainline -> lua-scripting
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 Apr 2014 16:47:21 +0200
parents 8f62e8d5a384
children 37adac56017a
comparison
equal deleted inserted replaced
409:63f707278fc8 762:45b16f67259c
1 #include "gtest/gtest.h"
2
3 #include <stdint.h>
4 #include "../Core/FileFormats/PngReader.h"
5 #include "../Core/FileFormats/PngWriter.h"
6 #include "../Core/Toolbox.h"
7 #include "../Core/Uuid.h"
8
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]);
30
31 std::string f, md5;
32 Orthanc::Toolbox::ReadFile(f, "ColorPattern.png");
33 Orthanc::Toolbox::ComputeMD5(md5, f);
34 ASSERT_EQ("604e785f53c99cae6ea4584870b2c41d", md5);
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]);
55
56 std::string f, md5;
57 Orthanc::Toolbox::ReadFile(f, "Gray8Pattern.png");
58 Orthanc::Toolbox::ComputeMD5(md5, f);
59 ASSERT_EQ("5a9b98bea3d0a6d983980cc38bfbcdb3", md5);
60 }
61
62 TEST(PngWriter, Gray16Pattern)
63 {
64 Orthanc::PngWriter w;
65 int width = 256;
66 int height = 256;
67 int pitch = width * 2 + 16;
68
69 std::vector<uint8_t> image(height * pitch);
70
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]);
82
83 std::string f, md5;
84 Orthanc::Toolbox::ReadFile(f, "Gray16Pattern.png");
85 Orthanc::Toolbox::ComputeMD5(md5, f);
86 ASSERT_EQ("0785866a08bf0a02d2eeff87f658571c", md5);
87 }
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
111 {
112 Orthanc::PngReader r;
113 r.ReadFromMemory(s);
114
115 ASSERT_EQ(r.GetFormat(), Orthanc::PixelFormat_Grayscale16);
116 ASSERT_EQ(r.GetWidth(), width);
117 ASSERT_EQ(r.GetHeight(), height);
118
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());
123 ASSERT_EQ(p, r.GetBuffer(y));
124 for (int x = 0; x < width; x++, p++, v++)
125 {
126 ASSERT_EQ(*p, v);
127 }
128 }
129 }
130
131 {
132 Orthanc::Toolbox::TemporaryFile tmp;
133 Orthanc::Toolbox::WriteFile(s, tmp.GetPath());
134
135 Orthanc::PngReader r2;
136 r2.ReadFromFile(tmp.GetPath());
137
138 ASSERT_EQ(r2.GetFormat(), Orthanc::PixelFormat_Grayscale16);
139 ASSERT_EQ(r2.GetWidth(), width);
140 ASSERT_EQ(r2.GetHeight(), height);
141
142 v = 0;
143 for (int y = 0; y < height; y++)
144 {
145 uint16_t *p = reinterpret_cast<uint16_t*>((uint8_t*) r2.GetBuffer() + y * r2.GetPitch());
146 ASSERT_EQ(p, r2.GetBuffer(y));
147 for (int x = 0; x < width; x++, p++, v++)
148 {
149 ASSERT_EQ(*p, v);
150 }
151 }
152 }
153 }