16

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?!

Herm
  • 411
  • 5
  • 14
  • Actually I need to do the same thing. Can you give me the code which converts images into mov? It's okay if you can't. My email is animesh1789@gmail.com . It'd be super grateful. – anivader May 09 '16 at 05:49
  • I've slightly different needs: new kidney and money. Any amount. It'd be cool if you can share. Tha-a-anks! – Ian Bytchek May 04 '17 at 19:26

1 Answers1

24

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.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Interesting...any reason why, or is this pretty arbitrary? Just a matter of making allocating buffers easier? Also, do you have any documentation that might indicate this specifically for CGImage? Thanks! – Herm Apr 12 '13 at 01:29
  • 1
    Like I said, I assume it is due to cache line behavior. The CPU always loads a whole cache line at once. I don't recall any documentation to point you at. – rob mayoff Apr 12 '13 at 01:59
  • Is there any way to stop it from doing that? Can I set my bytesperrow? – anivader May 09 '16 at 05:52
  • If you have the raw pixel data with a different `bytesPerRow`, you can use `CGImageCreate`. If you're using the system's image loading functions, you get what the system picks. – rob mayoff May 09 '16 at 05:55
  • Indeed I have raw pixel buffer from another image with different bytes per row. If I just copy everything over and create a new image, it creates a slanted image with pixels spilling over from the right and coming back from the left side. How do I identify and get rid of additional pixels from each row when copying? – anivader May 09 '16 at 13:01
  • You need to post your own question with full details. – rob mayoff May 09 '16 at 15:22