Question

Basically I am doing a screen capture app with optimum image size.

I am trying to capture my Mac screen and save it as JPEG file. I wanted to capture the screen using Cocoa API [CGWindowListCreateImage] and save the file as JPEG using libjpeg9. I included the static library [libjpeg.a] and the header files[jpeglib.h, jconfig.h, jerror.h and jmorecfg.h] using the "Add Files to Project_Name" option in XCode.

The code I used to save as JPEG file is

int write_jpeg_file( char *filename )
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    
    /* this is a pointer to one row of image data */
    JSAMPROW row_pointer[1];
    FILE *outfile = fopen( filename, "wb" );
    
    if ( !outfile )
    {
        printf("Error opening output jpeg file %s\n!", filename );
        return -1;
    }
    cinfo.err = jpeg_std_error( &jerr );
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);
    
    /* Setting the parameters of the output file here */
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = bytes_per_pixel;
    cinfo.in_color_space = color_space;
    /* default compression parameters, we shouldn't be worried about these */
    jpeg_set_defaults( &cinfo );
    /* Now do the compression .. */
    jpeg_start_compress( &cinfo, TRUE );
    /* like reading a file, this time write one row at a time */
    while( cinfo.next_scanline < cinfo.image_height )
    {
        row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width *  cinfo.input_components];
        jpeg_write_scanlines( &cinfo, row_pointer, 1 );
    }
    /* similar to read file, clean up after we're done compressing */
    jpeg_finish_compress( &cinfo );
    jpeg_destroy_compress( &cinfo );
    fclose( outfile );
    /* success code is 1! */
    return 1;
}

When I Build and Run the project I am getting following error

Parse Issue

Expected }

in the line

typedef enum { FALSE = 0, TRUE = 1 } boolean;

in jmorecfg.h which is libjpeg header file. I am not sure why I am getting this error. Please help me.


I know there is a way to save the file using Cocoa API instead of using libjpeg. I first tried in XCode using Cocoa. I am able to get the JPEG file. The file size is relatively bigger(~200KB). When I create the image in C using libjpeg the file size is ~100KB.

The code I used:

CGFloat imageCompression = 0.6;

- (NSImage *)captureImageForRect:(NSRect)rect {
    CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
    NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:screenShot];
    NSLog(@"Bits per pixel : %ld", [imageRep bitsPerPixel]);
    NSImage *result = [[NSImage alloc] init];
    [result addRepresentation:imageRep];
    screenShot=nil;
    imageRep=nil;
    return result;
}

-(NSData *)jpegReresentationOfImage:(NSImage *) image {
    NSBitmapImageRep* myBitmapImageRep=nil;
    NSSize imageSize = [image size];
    [image lockFocus];
    NSRect imageRect = NSMakeRect(0, 0, imageSize.width, imageSize.height);
    myBitmapImageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
    [image unlockFocus];
    
    // set up the options for creating a JPEG
    NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
                   [NSNumber numberWithDouble:imageCompression], NSImageCompressionFactor,
                   [NSNumber numberWithBool:NO], NSImageProgressive,
                   nil];
    
    NSData* imageData = [myBitmapImageRep representationUsingType:NSJPEGFileType properties:options];
    myBitmapImageRep=nil;
    return ( imageData);
}


-(void)captureAndSave {
    NSString *fileName = @"/Volumes/Official/images/output/Mac_Window_1.jpg";
    NSImage *screenshot = [self captureImageForRect:[[NSScreen mainScreen] frame]];
    NSData *jpgRep = [self jpegReresentationOfImage:screenshot];
    [jpgRep writeToFile:[fileName stringByExpandingTildeInPath] atomically:NO];
    [NSApp terminate:self];
}
Was it helpful?

Solution

Adding the following line to jconfig.h solved the problem. After adding this, I am not getting compile error and I am able to use libjpeg inside my Objetive-C program.

#define USE_MAC_MEMMGR

In jmemmac.c, I found the following code which helped me to fix it. Just adding the above line worked!

#ifndef USE_MAC_MEMMGR  /* make sure user got configuration right */
  You forgot to define USE_MAC_MEMMGR in jconfig.h. /* deliberate syntax error */
#endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top