1 // This is mul/vimt/tests/test_v2i.cxx
2 #include <iostream>
3 #ifdef _MSC_VER
4 # include "vcl_msvc_warnings.h"
5 #endif
6 #include "vpl/vpl.h"
7 #include "vnl/vnl_matrix.h" // for frobenius_norm()
8 #include "vul/vul_temp_filename.h"
9 #include <mbl/mbl_stl.h>
10 #include <vimt/vimt_load.h>
11 #include "testlib/testlib_test.h"
12 #include "vsl/vsl_quick_file.h"
13 #include "vil/vil_save.h"
14 #include "vil/vil_load.h"
15 #include "vil/vil_new.h"
16 #include "vil/vil_image_view.h"
17 #include "vil/vil_image_resource.h"
18 #include "vil/vil_property.h"
19 #include <vimt/vimt_vil_v2i.h>
20 #include <vimt/vimt_transform_2d.h>
21 #include <vimt/vimt_image_2d_of.h>
22 #include <vimt/vimt_add_all_binary_loaders.h>
23
test_v2i()24 static void test_v2i()
25 {
26 std::cout << "*****************************\n"
27 << " Testing vimt_vil_v2i_format\n"
28 << "*****************************\n";
29
30 vimt_add_all_binary_loaders();
31
32 // create a simple single plane image
33 vil_image_view<vxl_int_32> im1(3,4);
34 mbl_stl_increments(im1.begin(), im1.end(), -2);
35
36 // Try saving and laoding vil image.
37 std::string fname1 = vul_temp_filename() + ".v2i";
38 TEST("Successfully saved simple v2i image",vil_save(im1,fname1.c_str()), true);
39 vil_image_resource_sptr ir1 = vil_load_image_resource(fname1.c_str());
40 TEST( "Successfully loaded simple v2i image",!ir1, false);
41 if (ir1) {
42 TEST("Loaded simple image has identity for a transform",
43 dynamic_cast<vimt_vil_v2i_image&>(*ir1).world2im().is_identity(),true);
44 }
45 vpl_unlink(fname1.c_str());
46
47 vil_image_view<float> im2(3,4,6);
48 mbl_stl_increments(im2.begin(), im2.end(), -200.0f);
49 vimt_transform_2d tr2;
50 tr2.set_zoom_only(2.0, -5.0, -5.0);
51 std::string fname2 = vul_temp_filename() + ".v2i";
52 {
53 vil_image_resource_sptr ir2 = vil_new_image_resource(
54 fname2.c_str(), 3, 4, 6, VIL_PIXEL_FORMAT_FLOAT, "v2i");
55 TEST("Successfully opened image on disk", !ir2, false);
56 if (ir2) {
57 ir2->put_view(im2);
58 dynamic_cast<vimt_vil_v2i_image&>(*ir2).set_world2im(tr2);
59 // Save image as ir2 is destroyed.
60 }
61 }
62
63 vil_image_resource_sptr ir3 = vil_load_image_resource(fname2.c_str());
64 TEST( "Successfully loaded complicated v2i image",!ir3, false);
65 if (ir3) {
66 vimt_image_2d_of<float> im3(ir3->get_view(), vimt_load_transform(ir3));
67 TEST("Loaded complicated image has correct pixel values",
68 vil_image_view_deep_equality(im3.image(), im2), true);
69 TEST("Loaded complicated image has correct transform",
70 (im3.world2im().matrix()- tr2.matrix()).frobenius_norm() < 1e-6 , true);
71
72 float size[2];
73 TEST("get_property()", ir3->get_property(vil_property_pixel_size, size), true);
74 TEST("vil_property_pixel_size is correct", size[0] == 0.5 &&
75 size[1] == 0.5, true);
76 }
77 vpl_unlink(fname2.c_str());
78
79 vil_image_view<float> im4(3,4,6);
80 mbl_stl_increments(im4.begin(), im4.end(), -200.0f);
81
82 std::string fname3 = vul_temp_filename() + ".v2i";
83 {
84 vil_image_resource_sptr ir4 = vil_new_image_resource(
85 fname3.c_str(), 3, 4, 6, VIL_PIXEL_FORMAT_FLOAT, "v2i");
86 TEST("Successfully opened image on disk", !ir4, false);
87 if (ir4) {
88 ir4->put_view(im4);
89 // Start with one pixel size
90 dynamic_cast<vimt_vil_v2i_image&>(*ir4).set_pixel_size(0.001f, 0.002f);
91 // Save image as ir4 is destroyed.
92 }
93 }
94
95 vil_image_resource_sptr ir5 = vil_load_image_resource(fname3.c_str());
96 TEST("Successfully loaded complicated v2i image",!ir5, false);
97 if (ir5) {
98 vimt_image_2d_of<float> im5(ir5->get_view(), vimt_load_transform(ir5));
99 TEST("Loaded complicated image has correct pixel values",
100 vil_image_view_deep_equality(im5.image(), im4), true);
101 float size[2];
102 TEST("get_property()", ir5->get_property(vil_property_pixel_size, size), true);
103 TEST("vil_property_pixel_size is correct", size[0] == 0.001f &&
104 size[1] == 0.002f, true);
105 }
106 vpl_unlink(fname3.c_str());
107
108 vimt_transform_2d tr6;
109 tr6.set_zoom_only(2.0, -5.0, -5.0);
110 vimt_image_2d_of<float> im6(3,4,6, tr6);
111 mbl_stl_increments(im6.image().begin(), im6.image().end(), -200.0f);
112
113 std::string fname4 = vul_temp_filename() + ".v2i";
114 vsl_quick_file_save(im6, fname4);
115
116 vil_image_resource_sptr ir7 = vil_load_image_resource(fname4.c_str());
117 TEST( "Successfully loaded complicated v2i image",!ir7, false);
118 if (ir7) {
119 vil_image_view<float> im7(ir7->get_view());
120 TEST("Loaded complicated image has correct pixel values",
121 vil_image_view_deep_equality(im6.image(), im7), true);
122 float size[2];
123 TEST("get_property()", ir7->get_property(vil_property_pixel_size, size), true);
124 TEST("vil_property_pixel_size is correct", size[0] == 0.0005f &&
125 size[1] == 0.0005f, true);
126 vimt_transform_2d tr7 = vimt_load_transform(ir7, 1000.0);
127 std::cout << "saved: " << tr6 << std::endl
128 << "loaded: " << tr7 << std::endl;
129 TEST_NEAR("Loaded complicated image has correct transform",
130 (tr6.matrix()- tr7.matrix()).frobenius_norm(), 0.0, 1e-6);
131 }
132 vpl_unlink(fname4.c_str());
133 }
134
135 TESTMAIN(test_v2i);
136