With the end of the year coming, it's nice to take a break from questions that matter and just spend a few minutes over-optimizing a solution.
Setup :
I have some RGBA pixel data stored in an array of unsigned chars (basically, 8 bits per array member, so 1/4th a pixel). Now, my palette colors are set as 32 bit Hex values, and I want to assign them to my array. I have come up with 2 methods of extracting the different components, a BIT AND/SHIFT and a BIT SHIFT/CAST method :
I'm sure there's about 3 dozen other ways of doing this, some that might even be faster than using bit operations. Anyway, how would you guys have done this ? What would be faster on iOS's ARM architecture ? Does it even matter (of course not) ?
Setup :
I have some RGBA pixel data stored in an array of unsigned chars (basically, 8 bits per array member, so 1/4th a pixel). Now, my palette colors are set as 32 bit Hex values, and I want to assign them to my array. I have come up with 2 methods of extracting the different components, a BIT AND/SHIFT and a BIT SHIFT/CAST method :
Code:
-(void) setRGBAPixelAtX: (NSInteger) x y: (NSInteger) y color: (uint32_t) pixel
{
NSInteger index = (width * bytesPerChannel * (y - 1)) + ((x - 1) * bytesPerChannel);
/* The SHIFT/CAST method */
imagePixels[index] = (unsigned char) (pixel >> 24),
imagePixels[index+1] = (unsigned char) (pixel >> 16),
imagePixels[index+2] = (unsigned char) (pixel >> 8),
imagePixels[index+3] = (unsigned char) pixel;
/* OR the AND/SHIFT method */
imagePixels[index] = (pixel & 0xff000000) >> 24,
imagePixels[index+1] = (pixel & 0x00ff0000) >> 16,
imagePixels[index+2] = (pixel & 0x0000ff00) >> 8,
imagePixels[index+3] = pixel & 0x000000ff;
}
I'm sure there's about 3 dozen other ways of doing this, some that might even be faster than using bit operations. Anyway, how would you guys have done this ? What would be faster on iOS's ARM architecture ? Does it even matter (of course not) ?