Image Processing

Proud of myself for writing this. If there are any suggestions for improvement, let me know! I find this totally cool. You’ll need to give it a directory with a few “jpg” images to run it.

https://github.com/GoesToEleven/la/blob/master/cc/02_image-comparision/02_solution-in-steps/13_images-pixels/main.go

QUESTION:
When I run the code, I get the output below. From that output, it looks like the color depth per pixel is 2 bytes each for r, g, b, a:

16 + 16 + 16 + 16 = 64 bits per pixel

Wikipedia tells me that “Images can have 64-bit pixels with 48-bit color and a 16-bit alpha channel.” (https://en.wikipedia.org/wiki/Color_depth)

When I multiply the color depth per pixel * the total number of pixels, I get a number that doesn’t correspond to the file’s actual size. Is this difference because the file is compressed?

Image 0 	 pixel 0 	 r g b a: {60244 62058 63494 65535}
Image 0 	 pixel 1 	 r g b a: {60244 62058 63494 65535}
Image 0 	 pixel 2 	 r g b a: {60244 62058 63494 65535}
Image 0 	 pixel 3 	 r g b a: {60244 62058 63494 65535}
Image 0 	 pixel 4 	 r g b a: {60501 62403 63297 65535}
Image 0 	 pixel 5 	 r g b a: {60501 62403 63297 65535}
Image 0 	 pixel 6 	 r g b a: {60244 62146 63040 65535}
Image 0 	 pixel 7 	 r g b a: {60244 62146 63040 65535}
Image 0 	 pixel 8 	 r g b a: {60758 62660 63554 65535}
Image 0 	 pixel 9 	 r g b a: {60758 62660 63554 65535}
Image 0 	 pixel 10 	 r g b a: {61015 62829 64265 65535}
Image 1 	 pixel 0 	 r g b a: {1825 14201 32552 65535}
Image 1 	 pixel 1 	 r g b a: {1311 13687 32038 65535}
Image 1 	 pixel 2 	 r g b a: {1825 14201 32552 65535}
Image 1 	 pixel 3 	 r g b a: {2082 14458 32809 65535}
Image 1 	 pixel 4 	 r g b a: {1568 13944 32295 65535}
Image 1 	 pixel 5 	 r g b a: {1568 13856 32749 65535}
Image 1 	 pixel 6 	 r g b a: {1825 14113 33006 65535}
Image 1 	 pixel 7 	 r g b a: {1568 13856 32749 65535}
Image 1 	 pixel 8 	 r g b a: {1311 13687 32038 65535}
Image 1 	 pixel 9 	 r g b a: {1311 13687 32038 65535}
Image 1 	 pixel 10 	 r g b a: {1568 13944 32295 65535}
Image 2 	 pixel 0 	 r g b a: {65535 65535 65535 65535}
Image 2 	 pixel 1 	 r g b a: {65535 65535 65535 65535}
Image 2 	 pixel 2 	 r g b a: {65535 65535 65535 65535}
Image 2 	 pixel 3 	 r g b a: {65535 65535 65535 65535}
Image 2 	 pixel 4 	 r g b a: {65535 65535 65535 65535}
Image 2 	 pixel 5 	 r g b a: {65535 65535 65535 65535}
Image 2 	 pixel 6 	 r g b a: {65535 65535 65535 65535}
Image 2 	 pixel 7 	 r g b a: {65535 65535 65535 65535}
Image 2 	 pixel 8 	 r g b a: {65535 65535 65535 65535}
Image 2 	 pixel 9 	 r g b a: {65535 65535 65535 65535}
Image 2 	 pixel 10 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 0 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 1 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 2 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 3 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 4 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 5 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 6 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 7 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 8 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 9 	 r g b a: {65535 65535 65535 65535}
Image 3 	 pixel 10 	 r g b a: {65535 65535 65535 65535}
1 Like

Yep, JPEG compression will reduce the file-size quite a bit.

1 Like

Congratulations! It’s fun to harness power like that, especially in a concise and easy to understand program :relaxed:

Besides compression, another reason you’re calculating a size difference is that Image.At() always returns Colors, which provide that 64-bit value via RGBA(). Even if the underlying Image in memory isn’t in that format!

For example, JPEGs are often some variation of YCbCr per pixel, even after they’re loaded and decompressed. That’s only 24 bits per pixel.

But the Image in memory doesn’t need to have 64-bit pixels, or even RGBA pixels, for At() and RGBA() to work.

When you call At() on a JPEG Image, the JPEG Image actually returns a YCbCr. YCbCr fulfills the Color interface, so you can call RGBA() on it – but it does a colorspace conversion internally! :smiley: https://golang.org/pkg/image/color/#YCbCr

This is the “magic” of interfaces: Same Image interface, so you call At() on it, but different types of image underneath. Same Color interface, so you can call RGBA() on it… but different underlying pixel type.

1 Like

Thank you!

This was very helpful.

I appreciate your response.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.