cocoa - Using libjpeg from Objective C in XCode -


basically doing screen capture app optimum image size.

i trying capture mac screen , save jpeg file. wanted capture screen using cocoa api [cgwindowlistcreateimage] , save file jpeg using libjpeg9. included static library [libjpeg.a] , header files[jpeglib.h, jconfig.h, jerror.h , jmorecfg.h] using "add files project_name" option in xcode.

the code used save jpeg file is

int write_jpeg_file( char *filename ) {     struct jpeg_compress_struct cinfo;     struct jpeg_error_mgr jerr;      /* pointer 1 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 parameters of 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, shouldn't worried these */     jpeg_set_defaults( &cinfo );     /* compression .. */     jpeg_start_compress( &cinfo, true );     /* reading file, time write 1 row @ 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 read file, clean after we're done compressing */     jpeg_finish_compress( &cinfo );     jpeg_destroy_compress( &cinfo );     fclose( outfile );     /* success code 1! */     return 1; } 

when build , run project getting following error

parse issue

expected }

in line

typedef enum { false = 0, true = 1 } boolean;

in jmorecfg.h libjpeg header file. not sure why getting error. please me.


i know there way save file using cocoa api instead of using libjpeg. first tried in xcode using cocoa. able jpeg file. file size relatively bigger(~200kb). when create image in c using libjpeg file size ~100kb.

the code 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 options creating 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]; } 

adding following line jconfig.h solved problem. after adding this, not getting compile error , able use libjpeg inside objetive-c program.

#define use_mac_memmgr

in jmemmac.c, found following code helped me fix it. adding above line worked!

#ifndef use_mac_memmgr  /* make sure user got configuration right */   forgot define use_mac_memmgr in jconfig.h. /* deliberate syntax error */ #endif 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -