annotate UnitTestsSources/Png.cpp @ 671:7131193b0e4b

fix mainline version
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 08 Nov 2013 16:28:58 +0100
parents 51892be15618
children 6c90ce085261
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
368
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #include "gtest/gtest.h"
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 #include <stdint.h>
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
4 #include "../Core/FileFormats/PngReader.h"
454
6f47a4262618 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 369
diff changeset
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"
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
7
368
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 TEST(PngWriter, ColorPattern)
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 {
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 Orthanc::PngWriter w;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 int width = 17;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 int height = 61;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 int pitch = width * 3;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 std::vector<uint8_t> image(height * pitch);
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 for (int y = 0; y < height; y++)
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 {
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 uint8_t *p = &image[0] + y * pitch;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 for (int x = 0; x < width; x++, p += 3)
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 {
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 p[0] = (y % 3 == 0) ? 255 : 0;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 p[1] = (y % 3 == 1) ? 255 : 0;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 p[2] = (y % 3 == 2) ? 255 : 0;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 }
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 }
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 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
29
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
30 std::string f, md5;
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
31 Orthanc::Toolbox::ReadFile(f, "ColorPattern.png");
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
32 Orthanc::Toolbox::ComputeMD5(md5, f);
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
33 ASSERT_EQ("604e785f53c99cae6ea4584870b2c41d", md5);
368
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 }
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 TEST(PngWriter, Gray8Pattern)
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 Orthanc::PngWriter w;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 int width = 17;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 int height = 256;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 int pitch = width;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 std::vector<uint8_t> image(height * pitch);
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 for (int y = 0; y < height; y++)
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 uint8_t *p = &image[0] + y * pitch;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 for (int x = 0; x < width; x++, p++)
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 *p = y;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 }
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 }
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 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
54
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
55 std::string f, md5;
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
56 Orthanc::Toolbox::ReadFile(f, "Gray8Pattern.png");
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
57 Orthanc::Toolbox::ComputeMD5(md5, f);
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
58 ASSERT_EQ("5a9b98bea3d0a6d983980cc38bfbcdb3", md5);
368
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 TEST(PngWriter, Gray16Pattern)
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 {
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 Orthanc::PngWriter w;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 int width = 256;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 int height = 256;
369
4632a044746e simplification of the code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 368
diff changeset
66 int pitch = width * 2 + 16;
368
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 std::vector<uint8_t> image(height * pitch);
369
4632a044746e simplification of the code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 368
diff changeset
69
368
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 int v = 0;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 for (int y = 0; y < height; y++)
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 uint16_t *p = reinterpret_cast<uint16_t*>(&image[0] + y * pitch);
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 for (int x = 0; x < width; x++, p++, v++)
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 {
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 *p = v;
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 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
81
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
82 std::string f, md5;
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
83 Orthanc::Toolbox::ReadFile(f, "Gray16Pattern.png");
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
84 Orthanc::Toolbox::ComputeMD5(md5, f);
51892be15618 fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
85 ASSERT_EQ("0785866a08bf0a02d2eeff87f658571c", md5);
368
80011cd589e6 support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 }
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
87
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
88 TEST(PngWriter, EndToEnd)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
89 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
90 Orthanc::PngWriter w;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
91 int width = 256;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
92 int height = 256;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
93 int pitch = width * 2 + 16;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
94
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
95 std::vector<uint8_t> image(height * pitch);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
96
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
97 int v = 0;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
98 for (int y = 0; y < height; y++)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
99 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
100 uint16_t *p = reinterpret_cast<uint16_t*>(&image[0] + y * pitch);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
101 for (int x = 0; x < width; x++, p++, v++)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
102 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
103 *p = v;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
104 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
105 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
106
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
107 std::string s;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
108 w.WriteToMemory(s, width, height, pitch, Orthanc::PixelFormat_Grayscale16, &image[0]);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
109
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
110 Orthanc::PngReader r;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
111 r.ReadFromMemory(s);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
112
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
113 ASSERT_EQ(r.GetWidth(), width);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
114 ASSERT_EQ(r.GetHeight(), height);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
115
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
116 v = 0;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
117 for (int y = 0; y < height; y++)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
118 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
119 uint16_t *p = reinterpret_cast<uint16_t*>((uint8_t*) r.GetBuffer() + y * r.GetPitch());
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
120 for (int x = 0; x < width; x++, p++, v++)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
121 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
122 ASSERT_EQ(*p, v);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
123 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
124 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 454
diff changeset
125 }