File: MyCocoaFilesReader.m

package info (click to toggle)
lynkeos.app 3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,740 kB
  • sloc: objc: 40,528; ansic: 811; cpp: 150; sh: 68; makefile: 27
file content (763 lines) | stat: -rw-r--r-- 21,559 bytes parent folder | download
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
//
//  Lynkeos
//  $Id$
//
//  Created by Jean-Etienne LAMIAUD on Fri Mar 03 2005.
//  Copyright (c) 2005-2018. Jean-Etienne LAMIAUD
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 

/** The size by which the "time" array is enlarged. */
#define K_TIME_PAGE_SIZE 1024

#include <AppKit/NSAffineTransform.h>

#include <pthread.h>

#include "processing_core.h"
#include "MyCachePrefs.h"

#include "MyCocoaFilesReader.h"

/** Mutex used to avoid a deadlock when drawing offscreen from many threads */
static pthread_mutex_t offScreenLock;

/** Common function to retrieve a sample from an NSImage. */
static void getImageSample( REAL * const * const sample,
                            u_char nPlanes,
                            u_short atx, u_short aty, u_short atw, u_short ath, 
                            u_short width,
                            NSImage* image, LynkeosIntegerSize imageSize )
{
   NSRect cocoaRect;
   NSImageRep* srcImageRep;
   NSImage* offScreenImage;
   NSBitmapImageRep* bitMap;
   u_char *plane[5];
   int x, y, redIndex, greenIndex, blueIndex;
   u_short rowSize, pixelSize, bitsPerSample, sampleSize;
   NSBitmapFormat format;
   BOOL planar;
   float color[3];

   if ( nPlanes <= 0 || nPlanes > 3 )
   {
      NSLog( @"Invalid number of planes %d", nPlanes );
      return;
   }
   if( image == nil )
      return;

   // Convert this rectangle to Cocoa coordinates system
   cocoaRect = NSMakeRect(atx, imageSize.height - ath - aty, atw, ath );

   // Create an image to draw the NSImage in
   offScreenImage = [[[NSImage alloc] initWithSize:cocoaRect.size] autorelease];
   pthread_mutex_lock( &offScreenLock );
   [offScreenImage lockFocus];

   // Force graphic context into low resolution mode
#if !GNUSTEP
   CGContextRef g = [NSGraphicsContext currentContext].CGContext;
   const CGAffineTransform t = CGContextGetCTM(g);
   const CGFloat det = t.a*t.d - t.b*t.c;
#else
#ifndef __fp16
#define __fp16 float
#endif
   const NSAffineTransformStruct t
     = [[GSCurrentContext() GSCurrentCTM] transformStruct];
   const CGFloat det = t.m11*t.m22 - t.m12*t.m21;
#endif
   if (det > 0.0)
   {
      const CGFloat scale = sqrt(det);

      NSAffineTransform * xform = [NSAffineTransform transform];
      [xform scaleBy:1.0/scale];
      [xform translateXBy: -cocoaRect.origin.x yBy: -cocoaRect.origin.y];
      [xform concat];

      srcImageRep = [image bestRepresentationForRect: NSMakeRect(0,0,imageSize.width,imageSize.height)
                                             context: nil
                                               hints: nil];
      // Force full pixel scale
      [srcImageRep drawInRect:NSMakeRect(0,0,imageSize.width,imageSize.height)];
   }
   else
      NSLog(@"Invalid transformation matrix in offscreen image");
   bitMap = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:cocoaRect] autorelease];
   [offScreenImage unlockFocus];
   pthread_mutex_unlock( &offScreenLock );

   // Access the data
   [bitMap getBitmapDataPlanes:plane];
   format = [bitMap bitmapFormat];
   rowSize = [bitMap bytesPerRow];
   bitsPerSample = [bitMap bitsPerSample];
   pixelSize = [bitMap bitsPerPixel]/8;
   if ((bitsPerSample % 8) != 0)
   {
      NSLog(@"Samples are not aligned on byte boundary : %d bits", bitsPerSample);
      return;
   }
   sampleSize = bitsPerSample/8;
   if ((format & NSBitmapFormatAlphaFirst) != 0)
   {
      redIndex = 1;
      greenIndex = 2;
      blueIndex = 3;
   }
   else
   {
      redIndex = 0;
      greenIndex = 1;
      blueIndex = 2;
   }
   planar = [bitMap isPlanar];

   for ( y = 0; y < ath; y++ )
   {
      for ( x = 0; x < atw; x++ )
      {
         u_short c;
         void *redSample, *greenSample, *blueSample;

         // Read the data in the bitmap
         if ( planar )
         {
            redSample = &plane[0][y*rowSize+x*sampleSize];
            greenSample = &plane[1][y*rowSize+x*sampleSize];
            blueSample = &plane[2][y*rowSize+x*sampleSize];
         }
         else
         {
            redSample = &plane[0][y*rowSize+x*pixelSize];
            greenSample = &plane[0][y*rowSize+x*pixelSize+sampleSize];
            blueSample = &plane[0][y*rowSize+x*pixelSize+2*sampleSize];
         }

         switch (bitsPerSample)
         {
            case 8:
               color[RED_PLANE] = (float)*(uint8_t*)redSample;
               color[GREEN_PLANE] = (float)*(uint8_t*)greenSample;
               color[BLUE_PLANE] = (float)*(uint8_t*)blueSample;
               break;
            case 16:
               if ((format & NSBitmapFormatFloatingPointSamples))
               {
                  color[RED_PLANE] = 256.0*(float)*(__fp16*)redSample;
                  color[GREEN_PLANE] = 256.0*(float)*(__fp16*)greenSample;
                  color[BLUE_PLANE] = 256.0*(float)*(__fp16*)blueSample;
               }
               else
               {
                  color[RED_PLANE] = (float)*(uint16_t*)redSample;
                  color[GREEN_PLANE] = (float)*(uint16_t*)greenSample;
                  color[BLUE_PLANE] = (float)*(uint16_t*)blueSample;
               }
               break;
            case 32:
               if ((format & NSBitmapFormatFloatingPointSamples))
               {
                  color[RED_PLANE] = *(float*)redSample;
                  color[GREEN_PLANE] = *(float*)greenSample;
                  color[BLUE_PLANE] = *(float*)blueSample;
               }
               else
               {
                  color[RED_PLANE] = (float)*(uint32_t*)redSample;
                  color[GREEN_PLANE] = (float)*(uint32_t*)greenSample;
                  color[BLUE_PLANE] = (float)*(uint32_t*)blueSample;
               }
               break;
            default:
               break;
         }
         // Convert to monochrome, if needed
         if ( nPlanes == 1 )
            color[0] = (color[RED_PLANE] 
                        + color[GREEN_PLANE] 
                        + color[BLUE_PLANE]) / 3.0;

         // Fill in the sample buffer
         for( c = 0; c < nPlanes; c++ )
            SET_SAMPLE(sample[c], x, y, width, color[c]);
      }
   }
}

@implementation MyCocoaImageReader

+ (void) initialize
{
   pthread_mutex_init( &offScreenLock, NULL );
}

+ (void) lynkeosFileTypes:(NSArray**)fileTypes
{
   *fileTypes = [NSImage imageFileTypes];
}

- (id) initWithURL:(NSURL*)url
{
   self = [super init];

   if ( self != nil )
   {
      NSImage *image;

      _path = [[url path] retain];

      image = [self getNSImage];
      if ( image != nil )
      {
         NSRect r = {{0.0, 0.0}, [image size]};
         NSImageRep *rep = [image bestRepresentationForRect:r
                                                    context:nil
                                                      hints:nil];

         _size = LynkeosMakeIntegerSize([rep pixelsWide],[rep pixelsHigh]);
      }
      else
      {
         [self release];
         self = nil;
      }
   }

   return( self );
}

- (void) dealloc
{
   [_path release];
   [super dealloc];
}

- (void) imageWidth:(u_short*)w height:(u_short*)h
{
   *w = _size.width;
   *h = _size.height;
}

// We deal only with RGB images
- (u_short) numberOfPlanes
{
   return( 3 );
}

- (void) getMinLevel:(double*)vmin maxLevel:(double*)vmax
{
   *vmin = 0.0;
   *vmax = 255.0;
}

- (NSImage*) getNSImage
{
   return( [[[NSImage alloc] initWithContentsOfFile: _path] autorelease] );
}

- (void) getImageSample:(REAL * const * const)sample
             withPlanes:(u_short)nPlanes
                    atX:(short)x Y:(short)y W:(u_short)w H:(u_short)h
              lineWidth:(u_short)width
{
   getImageSample( sample, nPlanes, x, y, w, h, width, [self getNSImage], _size );
}

- (NSDictionary*) getMetaData
{
   return( nil );
}

@end

#if !defined GNUSTEP
/*!
 * @abstract ObjC container for a CVPixelBuffer
 */
@interface MyCGImageContainer : NSObject
{
@public
   CGImageRef _img; //!< The pixel buffer we wrap around
}
//! Dedicated initializer
- (id) initWithImage:(CGImageRef)pixbuf ;
@end

@implementation MyCGImageContainer
- (id) initWithImage:(CGImageRef)pixbuf
{
   if ( (self = [self init]) != nil )
   {
      _img = pixbuf;
      CGImageRetain( _img );
   }
   return( self );
}

- (void) dealloc
{
   CGImageRelease(_img);
   [super dealloc];
}
@end

/*!
 * @abstract Private methods of MyQuickTimeReader
 */
@interface MyQuickTimeReader(Private)
//! Get a pixel buffer for the specified image, in cache or in the movie
//! The returned image is owned by the caller
- (CGImageRef) getCGImageAtIndex:(u_long)index ;
@end

@implementation MyQuickTimeReader(Private)
- (CGImageRef) getCGImageAtIndex:(u_long)index
{
   NSString *key = [_url stringByAppendingFormat:@"&%06ld",index];
   LynkeosObjectCache *movieCache = [LynkeosObjectCache movieCache];
   MyCGImageContainer *pix;
   CGImageRef img;
   NSError *err;

   if ( movieCache != nil &&
        (pix=(MyCGImageContainer*)[movieCache getObjectForKey:key]) != nil )
   {
      return( CGImageRetain(pix->_img) );
   }

   NSAssert( index < _imageNumber, @"Access outside the movie" );

   // Go to the required time
   // Try to avoid skipping frames because of multiprocessing
   [_avLock lock];
   BOOL doSkip = ( movieCache == nil
                   || index < _currentImage
                   || index > (_currentImage+numberOfCpus+1) );
   do
   {
      if ( index != _currentImage )
      {
         if ( doSkip )
            _currentImage = index;
         else
            _currentImage++;  // This fills the cache with images in between
      }

      // And get the pixel buffer content
      img = [_imageGenerator copyCGImageAtTime:_times[_currentImage] actualTime:nil error:&err];
      if ( img == NULL )
         NSLog( @"Error reading movie image, %@", err != nil ? [err localizedDescription] : @"" );

      else if ( movieCache != nil )
         [movieCache setObject:
            [[[MyCGImageContainer alloc] initWithImage:img] autorelease]
                        forKey:[_url stringByAppendingFormat:@"&%06ld",_currentImage]];
   } while ( index != _currentImage );
   [_avLock unlock];

   return( img );
}
@end

@implementation MyQuickTimeReader

+ (void) load
{
   // Nothing to do, this is just to force the runtime to load this class
}

+ (void) initialize
{
   // Things to do before first instantiation
}

+ (void) lynkeosFileTypes:(NSArray**)fileTypes
{
   *fileTypes = [NSArray arrayWithObjects: [NSNumber numberWithInteger: 1], AVFileTypeQuickTimeMovie,
                                           [NSNumber numberWithInteger: 1], AVFileTypeMPEG4,
                                           [NSNumber numberWithInteger: 1], AVFileTypeAppleM4V,
                                           nil];
}

- (id) initWithURL:(NSURL*)url
{
   NSError *qtErr = nil;
   u_long arraySize = K_TIME_PAGE_SIZE;
   CGImageRef img;

   if ( (self = [self init]) == nil )
      return( self );

   // Initialize the variables and open the movie
   _avLock = [[NSLock alloc] init];
   _times = (CMTime*)malloc( arraySize*sizeof(CMTime) );
   _imageNumber = 0;
   _movie = [[AVURLAsset alloc] initWithURL: url
                                    options: [NSDictionary dictionaryWithObjectsAndKeys:
                                                [NSNumber numberWithBool:YES],
                                                AVURLAssetPreferPreciseDurationAndTimingKey,
                                                nil]];

   if ( _movie == nil )
   {
      NSLog( @"Error creating QTMovie, %@",
             qtErr != nil ? [qtErr localizedDescription] : @"" );
      [self release];
      return( nil );
   }

   _imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:_movie];
   if (_imageGenerator == nil)
   {
      NSLog(@"Could not allocate an image generator");
      [self release];
      return( nil );
   }

   _url = [[url absoluteString] retain];

   // Extract the time of each frame
   const CMTime duration = [_movie duration];
   AVAssetTrack *track = [[_movie tracksWithMediaType:AVMediaTypeVideo] firstObject];
   if (track == nil)
   {
      NSLog( @"Could not access to the track" );
      [self release];
      return( nil );
   }
   AVSampleCursor *cursor = [track makeSampleCursorAtFirstSampleInDecodeOrder];
   if (cursor == nil)
   {
      NSLog( @"Movie does not provide cursors" );
      [self release];
      return( nil );
   }

   // Find the last image time, as the cursor tends to iterate past the real movie end...
   CMTime lastImageTime = duration;
   img = [_imageGenerator copyCGImageAtTime:duration actualTime:&lastImageTime error:NULL];
   if (img != nil)
      CGImageRelease(img);


   CMTime movieTime = cursor.decodeTimeStamp;
   for (;;)
   {
      if ( _imageNumber >= arraySize )
      {
         arraySize += K_TIME_PAGE_SIZE;
         _times = (CMTime*)realloc( _times, arraySize*sizeof(CMTime) );
      }

      _times[_imageNumber] = movieTime;
      _imageNumber++;

      if ([cursor stepInDecodeOrderByCount:1] == 0)
         // At movie end
         break;
      movieTime = cursor.decodeTimeStamp;

      if (CMTimeCompare(movieTime, lastImageTime) == NSOrderedDescending)
         break; // Past last frame
   }

   _currentImage = _imageNumber;

   if ( _imageNumber == 0 )
   {
      NSLog( @"No image found in movie %@", url );
      [self release];
      return nil;
   }

   const CMTime frameDuration = [track minFrameDuration];
   const CMTime lastPlusOne = CMTimeAdd(movieTime, frameDuration);
   if ( CMTimeCompare(duration, lastPlusOne) == NSOrderedDescending )
   {
      // The reader failed to read all the movie, let FFMpeg try do to better
      NSLog( @"Failed to read entire movie %@", url );
      [self release];
      return nil;
   }

   _imageGenerator.requestedTimeToleranceBefore = kCMTimeZero;
   _imageGenerator.requestedTimeToleranceAfter = kCMTimeZero;

   // Extract the first image and check its characteristics
   img = [_imageGenerator copyCGImageAtTime:_times[0] actualTime:nil error:&qtErr];
   if ( img == NULL )
   {
      NSLog( @"Error reading movie characteristic image, %@",
             qtErr != nil ? [qtErr localizedDescription] : @"" );
      [self release];
      return( nil );
   }

   const size_t bpp = CGImageGetBitsPerPixel(img);
   const size_t bpc = CGImageGetBitsPerComponent(img);
   if ((bpp % 8) == 0 && (bpc % 8) == 0)
   {
      const CGBitmapInfo bmInfo = CGImageGetBitmapInfo(img);

      switch (CGColorSpaceGetModel(CGImageGetColorSpace(img)))
      {
         case kCGColorSpaceModelMonochrome:
            _nPlanes = 1;
            break;
         case kCGColorSpaceModelRGB:
            _nPlanes = 3;
            break;
         default:
            NSLog(@"Unsupported color space");
            _nPlanes = 0;
            break;
      }

      _bytesPerPixel = bpp/8;

      switch (bpc)
      {
         case 8:
            _pixelType = integer8;
            break;
         case 16:
            _pixelType = integer16;
            break;
         case 32:
            if (bmInfo & kCGBitmapFloatInfoMask)
               _pixelType = float32;
            else
               _pixelType = integer32;
            break;
         case 64:
            if (bmInfo & kCGBitmapFloatInfoMask)
               _pixelType = float64;
            else
               _pixelType = integer64;
            break;
      }

      switch (bmInfo & kCGBitmapByteOrderMask)
      {
         case kCGBitmapByteOrderDefault:
            _cgBigEndian = YES;
            break;
         case kCGBitmapByteOrder16Big:
            _cgBigEndian = YES;
            break;
         case kCGBitmapByteOrder16Little:
            _cgBigEndian = NO;
            break;
         case kCGBitmapByteOrder32Big:
            _cgBigEndian = YES;
            break;
         case kCGBitmapByteOrder32Little:
            _cgBigEndian = NO;
            break;
         default:
            _cgBigEndian = YES;
            break;
      }

      switch (CGImageGetAlphaInfo(img))
      {
         case kCGImageAlphaNone:
         case kCGImageAlphaPremultipliedLast:
         case kCGImageAlphaLast:
         case kCGImageAlphaNoneSkipLast:
            _skipFirst = 0;
            break;
         case kCGImageAlphaPremultipliedFirst:
         case kCGImageAlphaFirst:
         case kCGImageAlphaNoneSkipFirst:
            _skipFirst = bpp - _nPlanes*bpc;
            break;
         case kCGImageAlphaOnly:
            NSLog(@"Alpha only movieisnotsupported");
            _nPlanes = 0;
            break;
      }
   }
   else
   {
      NSLog(@"Non byte aligned pixels are not supported");
      _nPlanes = 0;
   }

   if ( _nPlanes == 0 )
   {
      NSLog( @"Unsupported movie format" );
      [self release];
      return( nil );
   }

   _size = LynkeosMakeIntegerSize(CGImageGetWidth(img), CGImageGetHeight(img));

   CGImageRelease(img);

   return( self );
}

- (void) dealloc
{
   free( _times );
   [_avLock release];
   [_movie release];
   [_url release];

   [super dealloc];
}

- (void) imageWidth:(u_short*)w height:(u_short*)h
{
   *w = _size.width;
   *h = _size.height;
}

// We deal only with RGB images
- (u_short) numberOfPlanes
{
   return( _nPlanes );
}

- (void) getMinLevel:(double*)vmin maxLevel:(double*)vmax
{
   *vmin = 0.0;
   *vmax = 255.0;
}

- (u_long) numberOfFrames
{
   return( _imageNumber );
}

- (NSImage*) getNSImageAtIndex:(u_long)index
{
   CGImageRef img = [self getCGImageAtIndex:index];
   NSImage *res = [[[NSImage alloc] initWithCGImage:img size:NSSizeFromIntegerSize(_size)] autorelease];

   CGImageRelease(img);
   return(res);
}

- (void) getImageSample:(REAL * const * const)sample atIndex:(u_long)index
             withPlanes:(u_short)nPlanes
                    atX:(short)x Y:(short)y W:(u_short)w H:(u_short)h
              lineWidth:(u_short)width
{
   CGImageRef img = [self getCGImageAtIndex: index];
   CGColorSpaceRef colorSpace;
   u_short         dx, dy, p;

   if (img == NULL)
      return;

   colorSpace = CGColorSpaceCreateWithName(nPlanes == 3 ? kCGColorSpaceExtendedLinearSRGB
                                                        : kCGColorSpaceExtendedLinearGray);
   if (colorSpace == NULL)
   {
      CGImageRelease(img);
      fprintf(stderr, "Error allocating color space for reading movie image\n");
      return;
   }

   const u_short nComponents = (nPlanes == 3 ? 4 : 1);
   const int rowSamples = nComponents * w;
   const size_t rowSize = rowSamples * sizeof(float);
   const size_t bitmapByteCount = rowSize * h;
   float *bitmapData = (float*)malloc( bitmapByteCount );
   if (bitmapData == NULL)
   {
      NSLog (@"Could not allocate buffer for reading movie image");
      CGColorSpaceRelease( colorSpace );
      CGImageRelease(img);
      return;
   }

   CGContextRef context = CGBitmapContextCreate (bitmapData,
                                    w,
                                    h,
                                    sizeof(float) * 8,
                                    rowSize,
                                    colorSpace,
                                    kCGBitmapFloatComponents | kCGBitmapByteOrder32Host |
                                     (nPlanes == 3 ? kCGImageAlphaNoneSkipLast : 0));
   if (context == NULL)
   {
      free (bitmapData);
      CGColorSpaceRelease( colorSpace );
      CGImageRelease(img);
      fprintf (stderr, "Could not create context for reading movie image");
      return;
   }

   // Draw the image as to retrieve the sample in the bitmap (coordinates are Cocoa oriented)
   CGRect r = {.origin = {-(CGFloat)x, (CGFloat)y + (CGFloat)h - _size.height},
               .size = {(CGFloat)_size.width, (CGFloat)_size.height}};
   CGContextSetBlendMode(context, kCGBlendModeCopy);
   CGContextDrawImage(context, r, img);
   CGContextFlush(context);

   if (nComponents == sizeof(REALVECT)/sizeof(REAL))
   {
      for ( dy = 0; dy < h; dy++ )
      {
         REAL * const linePtr = &bitmapData[dy*rowSamples];
         for ( dx = 0; dx < w; dx ++ )
         {
            REALVECT v = *((REALVECT*)&linePtr[dx*nComponents]);
            for( p = 0; p < nPlanes; p++ )
            {
               // Read the data in the bitmap
               SET_SAMPLE(sample[p], dx, dy, width, v[p] * 256.0);
            }
         }
      }
   }
   else
   {
      for ( dy = 0; dy < h; dy++ )
      {
         REAL * const linePtr = &bitmapData[dy*rowSamples];
         for ( dx = 0; dx < w; dx ++ )
         {
            REAL * const pixPtr = &linePtr[dx*nComponents];
            for( p = 0; p < nPlanes; p++ )
            {
               // Read the data in the bitmap
               SET_SAMPLE(sample[p], dx, dy, width, pixPtr[p] * 256.0);
            }
         }
      }
   }

   free (bitmapData);
   CGColorSpaceRelease( colorSpace );
   CGContextRelease(context);
   CGImageRelease(img);
}

- (NSDictionary*) getMetaData
{
   return( nil );
}

@end
#endif