targa_read_test.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // Copyright 2013 Christian Henning
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. //#define BOOST_TEST_MODULE targa_read_test_module
  9. #include <boost/gil.hpp>
  10. #include <boost/gil/extension/io/targa.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. #include "paths.hpp"
  13. #include "scanline_read_test.hpp"
  14. using namespace std;
  15. using namespace boost::gil;
  16. using tag_t = targa_tag;
  17. BOOST_AUTO_TEST_SUITE( gil_io_targa_tests )
  18. #ifdef BOOST_GIL_IO_USE_TARGA_FILEFORMAT_TEST_SUITE_IMAGES
  19. template< typename Image >
  20. void test_targa_scanline_reader( string filename )
  21. {
  22. test_scanline_reader<Image, targa_tag>( string( targa_in + filename ).c_str() );
  23. }
  24. template< typename Image >
  25. void write( Image& img
  26. , const string& file_name
  27. )
  28. {
  29. #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  30. write_view( targa_out + file_name
  31. , view( img )
  32. , tag_t()
  33. );
  34. #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  35. }
  36. BOOST_AUTO_TEST_CASE( read_header_test )
  37. {
  38. {
  39. using backend_t = get_reader_backend<std::string const, tag_t>::type;
  40. backend_t backend = read_image_info( targa_filename
  41. , tag_t()
  42. );
  43. BOOST_CHECK_EQUAL( backend._info._header_size , 18 );
  44. BOOST_CHECK_EQUAL( backend._info._offset , 18 );
  45. BOOST_CHECK_EQUAL( backend._info._color_map_type , 0 );
  46. BOOST_CHECK_EQUAL( backend._info._image_type , 10 );
  47. BOOST_CHECK_EQUAL( backend._info._color_map_start , 0 );
  48. BOOST_CHECK_EQUAL( backend._info._color_map_length, 0 );
  49. BOOST_CHECK_EQUAL( backend._info._color_map_depth , 0 );
  50. BOOST_CHECK_EQUAL( backend._info._x_origin , 0 );
  51. BOOST_CHECK_EQUAL( backend._info._y_origin , 0 );
  52. BOOST_CHECK_EQUAL( backend._info._width , 124 );
  53. BOOST_CHECK_EQUAL( backend._info._height , 124 );
  54. BOOST_CHECK_EQUAL( backend._info._bits_per_pixel , 24 );
  55. BOOST_CHECK_EQUAL( backend._info._descriptor , 0 );
  56. }
  57. }
  58. BOOST_AUTO_TEST_CASE( read_reference_images_test )
  59. {
  60. // 24BPP_compressed.tga
  61. {
  62. rgb8_image_t img;
  63. read_image( targa_in + "24BPP_compressed.tga", img, tag_t() );
  64. typename rgb8_image_t::x_coord_t width = view( img ).width();
  65. typename rgb8_image_t::y_coord_t height = view( img ).height();
  66. BOOST_CHECK_EQUAL( width , 124 );
  67. BOOST_CHECK_EQUAL( height, 124 );
  68. BOOST_CHECK( view( img )(0, 0) == rgb8_pixel_t(248, 0, 248) );
  69. BOOST_CHECK( view( img )(width-1, 0) == rgb8_pixel_t(0, 0, 248) );
  70. BOOST_CHECK( view( img )(0, height-1) == rgb8_pixel_t(248, 0, 0) );
  71. BOOST_CHECK( view( img )(width-1, height-1) == rgb8_pixel_t(248, 0, 248) );
  72. write( img, "24BPP_compressed_out.tga" );
  73. }
  74. // 24BPP_uncompressed.tga
  75. {
  76. rgb8_image_t img;
  77. read_image( targa_in + "24BPP_uncompressed.tga", img, tag_t() );
  78. typename rgb8_image_t::x_coord_t width = view( img ).width();
  79. typename rgb8_image_t::y_coord_t height = view( img ).height();
  80. BOOST_CHECK_EQUAL( width , 124 );
  81. BOOST_CHECK_EQUAL( height, 124 );
  82. BOOST_CHECK( view( img )(0, 0) == rgb8_pixel_t(248, 0, 248) );
  83. BOOST_CHECK( view( img )(width-1, 0) == rgb8_pixel_t(0, 0, 248) );
  84. BOOST_CHECK( view( img )(0, height-1) == rgb8_pixel_t(248, 0, 0) );
  85. BOOST_CHECK( view( img )(width-1, height-1) == rgb8_pixel_t(248, 0, 248) );
  86. write( img, "24BPP_uncompressed_out.tga" );
  87. test_targa_scanline_reader< bgr8_image_t >( "24BPP_uncompressed.tga" );
  88. }
  89. // 32BPP_compressed.tga
  90. {
  91. rgba8_image_t img;
  92. read_image( targa_in + "32BPP_compressed.tga", img, tag_t() );
  93. typename rgba8_image_t::x_coord_t width = view( img ).width();
  94. typename rgba8_image_t::y_coord_t height = view( img ).height();
  95. BOOST_CHECK_EQUAL( width , 124 );
  96. BOOST_CHECK_EQUAL( height, 124 );
  97. BOOST_CHECK( view( img )(0, 0) == rgba8_pixel_t(248, 0, 248, 255) );
  98. BOOST_CHECK( view( img )(width-1, 0) == rgba8_pixel_t(0, 0, 248, 255) );
  99. BOOST_CHECK( view( img )(0, height-1) == rgba8_pixel_t(0, 0, 0, 0) );
  100. BOOST_CHECK( view( img )(width-1, height-1) == rgba8_pixel_t(248, 0, 248, 255) );
  101. write( img, "32BPP_compressed_out.tga" );
  102. }
  103. // 32BPP_uncompressed.tga
  104. {
  105. rgba8_image_t img;
  106. read_image( targa_in + "32BPP_uncompressed.tga", img, tag_t() );
  107. typename rgba8_image_t::x_coord_t width = view( img ).width();
  108. typename rgba8_image_t::y_coord_t height = view( img ).height();
  109. BOOST_CHECK_EQUAL( width , 124 );
  110. BOOST_CHECK_EQUAL( height, 124 );
  111. BOOST_CHECK( view( img )(0, 0) == rgba8_pixel_t(248, 0, 248, 255) );
  112. BOOST_CHECK( view( img )(width-1, 0) == rgba8_pixel_t(0, 0, 248, 255) );
  113. BOOST_CHECK( view( img )(0, height-1) == rgba8_pixel_t(0, 0, 0, 0) );
  114. BOOST_CHECK( view( img )(width-1, height-1) == rgba8_pixel_t(248, 0, 248, 255) );
  115. write( img, "32BPP_uncompressed_out.tga" );
  116. test_targa_scanline_reader< bgra8_image_t >( "32BPP_uncompressed.tga" );
  117. }
  118. // 24BPP_compressed_ul_origin.tga
  119. {
  120. rgb8_image_t img;
  121. read_image( targa_in + "24BPP_compressed_ul_origin.tga", img, tag_t() );
  122. typename rgb8_image_t::x_coord_t width = view( img ).width();
  123. typename rgb8_image_t::y_coord_t height = view( img ).height();
  124. BOOST_CHECK_EQUAL( width , 124 );
  125. BOOST_CHECK_EQUAL( height, 124 );
  126. BOOST_CHECK( view( img )(0, 0) == rgb8_pixel_t(248, 0, 248) );
  127. BOOST_CHECK( view( img )(width-1, 0) == rgb8_pixel_t(0, 0, 248) );
  128. BOOST_CHECK( view( img )(0, height-1) == rgb8_pixel_t(248, 0, 0) );
  129. BOOST_CHECK( view( img )(width-1, height-1) == rgb8_pixel_t(248, 0, 248) );
  130. write( img, "24BPP_compressed_ul_origin_out.tga" );
  131. }
  132. // 24BPP_uncompressed_ul_origin.tga
  133. {
  134. rgb8_image_t img;
  135. read_image( targa_in + "24BPP_uncompressed_ul_origin.tga", img, tag_t() );
  136. typename rgb8_image_t::x_coord_t width = view( img ).width();
  137. typename rgb8_image_t::y_coord_t height = view( img ).height();
  138. BOOST_CHECK_EQUAL( width , 124 );
  139. BOOST_CHECK_EQUAL( height, 124 );
  140. BOOST_CHECK( view( img )(0, 0) == rgb8_pixel_t(248, 0, 248) );
  141. BOOST_CHECK( view( img )(width-1, 0) == rgb8_pixel_t(0, 0, 248) );
  142. BOOST_CHECK( view( img )(0, height-1) == rgb8_pixel_t(248, 0, 0) );
  143. BOOST_CHECK( view( img )(width-1, height-1) == rgb8_pixel_t(248, 0, 248) );
  144. write( img, "24BPP_uncompressed_ul_origin_out.tga" );
  145. }
  146. // 32BPP_compressed_ul_origin.tga
  147. {
  148. rgba8_image_t img;
  149. read_image( targa_in + "32BPP_compressed_ul_origin.tga", img, tag_t() );
  150. typename rgba8_image_t::x_coord_t width = view( img ).width();
  151. typename rgba8_image_t::y_coord_t height = view( img ).height();
  152. BOOST_CHECK_EQUAL( width , 124 );
  153. BOOST_CHECK_EQUAL( height, 124 );
  154. BOOST_CHECK( view( img )(0, 0) == rgba8_pixel_t(248, 0, 248, 255) );
  155. BOOST_CHECK( view( img )(width-1, 0) == rgba8_pixel_t(0, 0, 248, 255) );
  156. BOOST_CHECK( view( img )(0, height-1) == rgba8_pixel_t(0, 0, 0, 0) );
  157. BOOST_CHECK( view( img )(width-1, height-1) == rgba8_pixel_t(248, 0, 248, 255) );
  158. write( img, "32BPP_compressed_ul_origin_out.tga" );
  159. }
  160. // 32BPP_uncompressed_ul_origin.tga
  161. {
  162. rgba8_image_t img;
  163. read_image( targa_in + "32BPP_uncompressed_ul_origin.tga", img, tag_t() );
  164. typename rgba8_image_t::x_coord_t width = view( img ).width();
  165. typename rgba8_image_t::y_coord_t height = view( img ).height();
  166. BOOST_CHECK_EQUAL( width , 124 );
  167. BOOST_CHECK_EQUAL( height, 124 );
  168. BOOST_CHECK( view( img )(0, 0) == rgba8_pixel_t(248, 0, 248, 255) );
  169. BOOST_CHECK( view( img )(width-1, 0) == rgba8_pixel_t(0, 0, 248, 255) );
  170. BOOST_CHECK( view( img )(0, height-1) == rgba8_pixel_t(0, 0, 0, 0) );
  171. BOOST_CHECK( view( img )(width-1, height-1) == rgba8_pixel_t(248, 0, 248, 255) );
  172. write( img, "32BPP_uncompressed_ul_origin_out.tga" );
  173. }
  174. }
  175. BOOST_AUTO_TEST_CASE( partial_image_test )
  176. {
  177. const std::string filename( targa_in + "24BPP_compressed.tga" );
  178. {
  179. rgb8_image_t img;
  180. read_image( filename
  181. , img
  182. , image_read_settings< targa_tag >( point_t( 0, 0 ), point_t( 50, 50 ) )
  183. );
  184. #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  185. write_view( targa_out + "targa_partial.tga"
  186. , view( img )
  187. , tag_t()
  188. );
  189. #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  190. }
  191. }
  192. #endif // BOOST_GIL_IO_USE_TARGA_FILEFORMAT_TEST_SUITE_IMAGES
  193. BOOST_AUTO_TEST_SUITE_END()