1 // Clip Library
2 // Copyright (c) 2015-2016 David Capello
3 
4 #include "clip.h"
5 #include <iostream>
6 #include <iomanip>
7 
8 template<typename T>
print_channel(const clip::image & img,const clip::image_spec & spec,const std::string & channel_name,const int channel_mask,const int channel_shift,const int channel_fill_width,const int pixel_size)9 static void print_channel(const clip::image& img,
10                           const clip::image_spec& spec,
11                           const std::string& channel_name,
12                           const int channel_mask,
13                           const int channel_shift,
14                           const int channel_fill_width,
15                           const int pixel_size) {
16   std::cout << channel_name << ":\n";
17   for (unsigned long y=0; y<spec.height; ++y) {
18     const char* p = (img.data()+spec.bytes_per_row*y);
19     std::cout << "  ";
20     for (unsigned long x=0; x<spec.width; ++x, p += pixel_size) {
21       std::cout << std::right
22                 << std::hex
23                 << std::setw(channel_fill_width)
24                 << (((*((T*)p)) & channel_mask) >> channel_shift) << " ";
25     }
26     std::cout << "\n";
27   }
28 }
29 
30 template<typename T>
print_channels(const clip::image & img,const clip::image_spec & spec,const int channel_fill_width,int pixel_size=sizeof (T))31 static void print_channels(const clip::image& img,
32                            const clip::image_spec& spec,
33                            const int channel_fill_width,
34                            int pixel_size = sizeof(T)) {
35   print_channel<T>(img, spec, "Red",   spec.red_mask,   spec.red_shift,   channel_fill_width, pixel_size);
36   print_channel<T>(img, spec, "Green", spec.green_mask, spec.green_shift, channel_fill_width, pixel_size);
37   print_channel<T>(img, spec, "Blue",  spec.blue_mask,  spec.blue_shift,  channel_fill_width, pixel_size);
38   if (spec.alpha_mask)
39     print_channel<T>(img, spec, "Alpha", spec.alpha_mask, spec.alpha_shift, channel_fill_width, pixel_size);
40 }
41 
main()42 int main() {
43   if (!clip::has(clip::image_format())) {
44     std::cout << "Clipboard doesn't contain an image\n";
45     return 1;
46   }
47 
48   clip::image img;
49   if (!clip::get_image(img)) {
50     std::cout << "Error getting image from clipboard\n";
51     return 1;
52   }
53 
54   clip::image_spec spec = img.spec();
55 
56   std::cout << "Image in clipboard "
57             << spec.width << "x" << spec.height
58             << " (" << spec.bits_per_pixel << "bpp)\n"
59             << "Format:" << "\n"
60             << std::hex
61             << "  Red   mask: " << spec.red_mask << "\n"
62             << "  Green mask: " << spec.green_mask << "\n"
63             << "  Blue  mask: " << spec.blue_mask << "\n"
64             << "  Alpha mask: " << spec.alpha_mask << "\n"
65             << std::dec
66             << "  Red   shift: " << spec.red_shift << "\n"
67             << "  Green shift: " << spec.green_shift << "\n"
68             << "  Blue  shift: " << spec.blue_shift << "\n"
69             << "  Alpha shift: " << spec.alpha_shift << "\n";
70 
71   std::cout << "Bytes:\n";
72   for (unsigned long y=0; y<spec.height; ++y) {
73     char* p = img.data()+spec.bytes_per_row*y;
74     std::cout << "  ";
75     for (unsigned long x=0; x<spec.width; ++x) {
76       for (unsigned long byte=0; byte<spec.bits_per_pixel/8; ++byte, ++p)
77         std::cout << std::right << std::hex << std::setfill('0') << std::setw(2) << int((*p) & 0xff) << " ";
78     }
79     std::cout << "\n";
80   }
81 
82   switch (spec.bits_per_pixel) {
83     case 16: print_channels<uint16_t>(img, spec, 2);    break;
84     case 24: print_channels<uint32_t>(img, spec, 2, 3); break;
85     case 32: print_channels<uint32_t>(img, spec, 2);    break;
86     case 64: print_channels<uint64_t>(img, spec, 4);    break;
87   }
88 }
89