packed_pixel.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  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. #include <boost/gil/extension/io/jpeg.hpp>
  9. #include <algorithm>
  10. // This test file demonstrates how to use packed pixel formats in GIL.
  11. // A "packed" pixel is a pixel whose channels are bit ranges.
  12. // Here we create an RGB image whose pixel has 16-bits, such as:
  13. // bits [0..6] are the blue channel
  14. // bits [7..13] are the green channel
  15. // bits [14..15] are the red channel
  16. // We read a regular 8-bit RGB image, convert it to packed BGR772, convert it back to 8-bit RGB and save it to a file.
  17. // Since the red channel is only two bits the color loss should be observable in the result
  18. //
  19. // This test file also demonstrates how to use bit-aligned images - these are images whose pixels themselves are not byte aligned.
  20. // For example, an rgb222 image has a pixel whose size is 6 bits. Bit-aligned images are more complicated than packed images. They
  21. // require a special proxy class to represent pixel reference and pixel iterator (packed images use C++ reference and C pointer respectively).
  22. // The alignment parameter in the constructor of bit-aligned images is in bit units. For example, if you want your bit-aligned image to have 4-byte
  23. // alignment of its rows use alignment of 32, not 4.
  24. //
  25. // To demonstrate that image view transformations work on packed images, we save the result transposed.
  26. using namespace boost;
  27. using namespace boost::gil;
  28. int main() {
  29. bgr8_image_t img;
  30. read_image("test.jpg",img, jpeg_tag{});
  31. ////////////////////////////////
  32. // define a bgr772 image. It is a "packed" image - its channels are not byte-aligned, but its pixels are.
  33. ////////////////////////////////
  34. using bgr772_image_t = packed_image3_type<uint16_t, 7,7,2, bgr_layout_t>::type;
  35. bgr772_image_t bgr772_img(img.dimensions());
  36. copy_and_convert_pixels(const_view(img),view(bgr772_img));
  37. // Save the result. JPEG I/O does not support the packed pixel format, so convert it back to 8-bit RGB
  38. write_view("out-packed_pixel_bgr772.jpg",color_converted_view<bgr8_pixel_t>(transposed_view(const_view(bgr772_img))), jpeg_tag{});
  39. ////////////////////////////////
  40. // define a gray1 image (one-bit per pixel). It is a "bit-aligned" image - its pixels are not byte aligned.
  41. ////////////////////////////////
  42. using gray1_image_t = bit_aligned_image1_type<1, gray_layout_t>::type;
  43. gray1_image_t gray1_img(img.dimensions());
  44. copy_and_convert_pixels(const_view(img),view(gray1_img));
  45. // Save the result. JPEG I/O does not support the packed pixel format, so convert it back to 8-bit RGB
  46. write_view("out-packed_pixel_gray1.jpg",color_converted_view<gray8_pixel_t>(transposed_view(const_view(gray1_img))), jpeg_tag{});
  47. return 0;
  48. }