Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my image's Bytes per Row more than its Bytes per Pixel times its Width?

I'm converting images to a MOV file, and had an interesting thing happen to me. I logged my bits per color component, bits per pixel, and bytes per row. Here's my code:

NSLog(@"Image width: %d, Height: %d", CGImageGetWidth(image), CGImageGetHeight(image));
NSLog(@"BPC: %d \ BPP: %d \ ByPR: %d", CGImageGetBitsPerComponent(image), CGImageGetBitsPerPixel(image), CGImageGetBytesPerRow(image));

Here's my output:

Image Width: 300, Height: 300 (everything's as expected) BPC: 8 (8 bits per color...so far so good) BPP: 32 (32 = 4 components ARGB * 8 bits per color...got it) ByPR:1216 (300 px per row * 4 bytes per pixel = 1200 bytes per row)

Why am I logged 1216 bytes per row, and not 1200? By the way, this isn't just a fluke. When I create a video based on those numbers for buffer sizes, it works. When I create it with 1200 bytes per row, I get some messed up aliasing effect.

Thoughts?!

like image 727
Herm Avatar asked Apr 10 '13 19:04

Herm


People also ask

How many bytes are in each pixel?

Each pixel typically consists of 8 bits (1 byte) for a Black and White (B&W) image or 24 bits (3 bytes) for a color image-- one byte each for Red, Green, and Blue.

How do you calculate bits per pixel?

Step 1: Multiply the detectors number of horizontal pixels by the number of vertical pixels to get the total number of pixels of the detector. Step 2: Multiply total number of pixels by the bit depth of the detector (16 bit, 14 bit etc.) to get the total number of bits of data.

What is byte and pixel?

Images are made of a grid of pixels aka “picture elements”. A pixel contains 8 bits (1 byte) if it is in BW (black and white). For colored images it uses a certain color scheme called RGB (Red, Green, Blue) represented as 1 byte each or 24 bits (3 bytes) per pixel.

How do you calculate the size of an image in RGB?

As each RGB pixel has three sets of 8-bit binary numbers it therefore has 24 bits of computer information in total. Hence the term '24-bit colour'. And as 8 bits equals 1 byte, each RGB pixel therefore equals 3 bytes in file size.


1 Answers

The system likes images to be a multiple of 64 bytes per row, presumably for better performance due to cache line alignment. 1200 is not a multiple of 64, but 1216 is.

like image 114
rob mayoff Avatar answered Sep 17 '22 21:09

rob mayoff