画像のピクセルデータの読み込み
できないできないと言ってきたけど、なんか勘違いしてたみたいで普通の方法でできるみたい。ただ、色深度の変更とか色コンポーネントの並びを整備するとかの手段がなさそうなので(あったら教えて下さい)、なんか面倒っぽい方法になってます。あとは読めないbits per pixelの時に警告を出すようにすれば使えかな。
void SketchPainter::setUIImage(UIImage *img) { CFDataRef ibuf; float imgw, imgh; uint16 *buf = this->buf(); uint bpp = CGImageGetBitsPerPixel(img.CGImage); uint bpr = CGImageGetBytesPerRow(img.CGImage); ibuf = CGDataProviderCopyData(CGImageGetDataProvider(img.CGImage)); imgw = img.size.width; imgh = img.size.height; NSLog(@"bits per pixel %d", bpp); if (ibuf) { if (bpp == 16) { uint16 a; uint x, y, index; uint16 *imgbuf = (uint16*)CFDataGetBytePtr(ibuf); for (y = 0; y < height; y ++) for (x = 0; x < width; x ++) { index = (floor((double)imgh * y / height) * bpr / 2 + floor((double)imgw * x / width)); a = imgbuf[index]; buf[y * width + x] = (a << 1); } } else if (bpp == 32) { PtColor col; uint x, y, index; UInt8* imgbuf = (UInt8*)CFDataGetBytePtr(ibuf); for (y = 0; y < height; y ++) for (x = 0; x < width; x ++) { index = (floor((double)imgh * y / height) * bpr / 4 + floor((double)imgw * x / width)) * 4; col.setRgb(imgbuf[index + 2], imgbuf[index + 1], imgbuf[index + 0]); buf[y * width + x] = SketchPainter::pack_color(col); } } CFRelease(ibuf); } }