また続き

メモリリークは起きてなさそう。画像をどんどん読み込んでも落ちなさそう。ただ、特定の画像を読もうとすると必ず落ちます。どっちがええんかな。色空間の変換もしてくれるし、API Referenceに記述されてる範囲で収まってる分だけ、こっちの方が筋が良さそうだけど… ちなみに落ちるのはCGContextDrawImageのトコです。

void SketchPainter::setUIImage(UIImage *img) {
  double ratio, ratio_w, ratio_h;
  uint16 *buf = this->buf();
  uint x, y, index, offset_x, offset_y, offset;
  
  CGImageRef cgimg = img.CGImage;
  uint imgw = CGImageGetWidth(cgimg);
  uint imgh = CGImageGetHeight(cgimg);

  ratio_w = (double)imgw / width;
  ratio_h = (double)imgh / height;
  ratio = (ratio_w < ratio_h) ? ratio_w : ratio_h;
  offset_x = max(0, (int)floor((imgw - width * ratio) / 2));
  offset_y = max(0, (int)floor((imgh - height * ratio) / 2));

  CGContextRef bitmap;
  NSMutableData *imgdata = [[NSMutableData alloc]
			     initWithLength:width * height * 4];
  UInt8 *imgbuf = (UInt8*)[imgdata mutableBytes];
  bitmap = CGBitmapContextCreate(imgbuf, imgw, imgh, 8, imgw * 4,
				 CGColorSpaceCreateDeviceRGB(),
				 kCGImageAlphaNoneSkipFirst);
  CGContextDrawImage(bitmap, CGRectMake(0, 0, imgw, imgh), cgimg);
  CGContextRelease(bitmap);

  PtColor col;
  offset = offset_y * imgw + offset_x;
  for (y = 0; y < height; y ++)
    for (x = 0; x < width; x ++) {
      index = (offset + 
	       (uint)floor(y * ratio) * imgw +
	       (uint)floor(x * ratio)) * 4;
      col.setRgb(imgbuf[index + 1],
		 imgbuf[index + 2],
		 imgbuf[index + 3]);
      buf[y * width +  x] = SketchPainter::pack_color(col);
    }
  [imgdata release];
}