Compositing a CIImage to NSBitmapImage planar problems

  • All:

      I am composing a CIImage to a NSBitmapImageRep using code
    described in http://www.cocoabuilder.com/archive/message/cocoa/
    2005/5/18/136311, like:

    bitmapImageRep = [[NSBitmapImageRep alloc]
    initWithBitmapDataPlanes:NULL
      pixelsWide:outputBitmapSize.width
      pixelsHigh:outputBitmapSize.height
      bitsPerSample:8
      samplesPerPixel:4
      hasAlpha:YES
      isPlanar:NO
      colorSpaceName:NSDeviceRGBColorSpace
      bytesPerRow:0
      bitsPerPixel:0];

    The example code works fine, and if I:

    myBMImagRep = [ciImage getNSBitmapImageRep];
    [[myBMImageRep TIFFRepresenatation] writeToFile:@"/tmp/foo.tiff"
    atomically:YES];

    everything works fine.  When I inspect the data through [bitmapData],
    things are fine as well.

    I am more interested in planar data, so changing the above allocation
    of the to:

    bitmapImageRep = [[NSBitmapImageRep alloc]
    initWithBitmapDataPlanes:NULL
      pixelsWide:outputBitmapSize.width
      pixelsHigh:outputBitmapSize.height
      bitsPerSample:8
      samplesPerPixel:4
      hasAlpha:YES
      isPlanar:YES
      colorSpaceName:NSDeviceRGBColorSpace
      bytesPerRow:0
      bitsPerPixel:0];

    The code runs much more quickly, but the [bitmapData] is empty.  Any
    pointers on what's going on here would be appreciated.

    Thanks in advance.

    Best Regards,
      Jeremy Faller
  • Hello,

    On 16.10.2006, at 11:15, Jeremy Faller wrote:

    > bitmapImageRep = [[NSBitmapImageRep alloc]
    > initWithBitmapDataPlanes:NULL
    > pixelsWide:outputBitmapSize.width
    > pixelsHigh:outputBitmapSize.height
    > bitsPerSample:8
    > samplesPerPixel:4
    > hasAlpha:YES
    > isPlanar:YES
    > colorSpaceName:NSDeviceRGBColorSpace
    > bytesPerRow:0
    > bitsPerPixel:0];

    >
    > The code runs much more quickly, but the [bitmapData] is empty.  Any
    > pointers on what's going on here would be appreciated.

    From the docs for graphicsContextWithBitmapImageRep:
    (NSGraphicsContext)
    > This method accepts only single plane NSBitmapImageRep instances.
    i.e. planar imageReps with more than one plane cannot be created this
    way.
    Look at the system console. You should have seen error messages with
    something
    like:  "CGContextSaveGState: invalid context" and
    "CGContextDrawImage: invalid context"
    and some more messages.

    If you have a *meshed* sourceRep with 8 bit per sample and 4 samples
    per pixel
    and need a *planar* destRep with also 8 bit per sample and 4 samples
    per pixel
    and if pixelsWide and pixelsHigh are the same it is not difficult to
    convert
    from meshed to planar. It can be made in a few lines of code.

    Here a code snippet without errorchecking or optimizing
    (say:pseudocode).
    Precondition: You have a valid sourceRep and an empty (but valid)
    destRep:

    destIndex = 0;
    for( row=0; row<[sourceRep pixelsHigh]; row++ ){
        buffer = [sourceRep bitmapData] + row*[sourceRep bytesPerRow];
        for( pix=0; pix<[sourceRep pixelsWide ]; pix++ ){
          destPlane[0][destIndex] = buffer[4*pix];  // red
          destPlane[1][destIndex] = buffer[4*pix+1]; // green
          destPlane[2][destIndex] = buffer[4*pix+2]; // blue
          destPlane[3][destIndex] = buffer[4*pix+3]; // alpha
          destIndex+=4;
        }
    }

    Good luck
    Heinrich Giesen
    --
    Heinrich Giesen
    <giesenH...>
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today