Advertisement
Guest User

screen capture

a guest
Oct 9th, 2013
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.67 KB | None | 0 0
  1. void screenCapture(void)
  2. {
  3.    int rc;
  4.    NSRect e;
  5.    int H;
  6.    int W;
  7.    CGRect rect;
  8.    CGImageRef image;
  9.    float screenshotWidth;
  10.    float screenshotHeight;
  11.    float requestedWidth;
  12.    float requestedHeight;
  13.    NSPoint mouseLoc;
  14.    NSImage *overlay;
  15.    NSPoint offset;
  16.    int x;
  17.    int y;
  18.    int w;
  19.    int h;
  20.    int org_x;
  21.    int org_y;
  22.    size_t height;
  23.    size_t width;
  24.    size_t bytesPerRow;
  25.    unsigned int * imgData;
  26.    CGRect bgBoundingBox;
  27.    CGContextRef context;
  28.    CGImageRef imageWithMousePointer;
  29.    CGImageRef imageWithMousePointerRescaled;
  30.    CGImageRef imageWithMousePointerRescaledInBox;
  31.    CFDataRef rawData;
  32.  
  33.    float wscale;
  34.    float hscale;
  35.    float scale;
  36.    float newWidth;
  37.    float newHeight;
  38.    float rescaledWidth;
  39.    float rescaledHeight;
  40.    float origX;
  41.    float origY;
  42.    size_t boundingBoxWidth;
  43.    size_t boundingBoxHeight;
  44.  
  45.    float imageWidth;
  46.    float imageHeight;
  47.    
  48.    UInt8* buf;
  49.    unsigned long byteLen;
  50.    unsigned long bmpIdx;
  51.    
  52.    void*          bitmapData;
  53.    int             bitmapByteCount;
  54.    int             bitmapBytesPerRow;
  55.    int n_width;
  56.    int n_height;
  57.    CGColorSpaceRef colorspace;
  58.  
  59.    rc = 1;
  60.    e = [[NSScreen mainScreen] frame];
  61.    H = (int)e.size.height;
  62.    W = (int)e.size.width;
  63.    screenshotWidth = (float) W;
  64.    screenshotHeight = (float) H;
  65.    rect.size.height = H;
  66.    rect.size.width = W;
  67.    rect.origin.x = 0;
  68.    rect.origin.y = 0;
  69.    
  70.    requestedWidth = 1280.0f;
  71.    requestedHeight = 720.0f;
  72.    
  73.    /***************************** Screenshot *************************************/  
  74.    image = CGWindowListCreateImage(rect, kCGWindowListOptionIncludingWindow|kCGWindowListOptionOnScreenBelowWindow, 0, kCGWindowImageDefault);
  75.    
  76.    
  77.    /***************************** Render cursor *************************************/  
  78.    mouseLoc   = [NSEvent mouseLocation];
  79.    overlay    = [[[NSCursor arrowCursor] image] copy];
  80.    offset     = [[NSCursor arrowCursor] hotSpot];
  81.    x = (int)mouseLoc.x;
  82.    y = (int)mouseLoc.y;
  83.    w = (int)[overlay size].width;
  84.    h = (int)[overlay size].height;
  85.    org_x = (x - w/2) - offset.x;
  86.    org_y = (y - h/2) - offset.y;
  87.    height = CGImageGetHeight(image);
  88.    width =  CGImageGetWidth(image);
  89.    bytesPerRow = CGImageGetBytesPerRow(image);
  90.    imgData = (unsigned int*)malloc(height*bytesPerRow);
  91.    bgBoundingBox = CGRectMake (0, 0, width,height);
  92.    context =  CGBitmapContextCreate(imgData, width, height, 8,  bytesPerRow, CGImageGetColorSpace(image), CGImageGetBitmapInfo(image));
  93.    CGContextDrawImage(context,bgBoundingBox,image);
  94.    CGContextDrawImage(context,CGRectMake(0, 0, width,height),image);
  95.    CGContextDrawImage(context,CGRectMake(org_x, org_y, w,h),[overlay CGImageForProposedRect: NULL context: NULL hints: NULL] );
  96.    imageWithMousePointer = CGBitmapContextCreateImage(context);
  97.    CGContextRelease(context);
  98.    free(imgData);
  99.    
  100.  
  101.    /***************************** Rescale *************************************/  
  102.    wscale = requestedWidth / screenshotWidth;
  103.    hscale = requestedHeight / screenshotHeight;
  104.    scale = hscale;
  105.    if (wscale < hscale) scale = wscale;
  106.    newWidth = ((float) CGImageGetWidth(imageWithMousePointer)) * scale;
  107.    newHeight = ((float) CGImageGetHeight(imageWithMousePointer)) * scale;
  108.    n_width = CGImageGetWidth(imageWithMousePointer) * scale;
  109.    n_height = CGImageGetHeight(imageWithMousePointer) * scale;
  110.    bitmapBytesPerRow   = (n_width * 4);
  111.    bitmapByteCount     = (bitmapBytesPerRow * n_height);
  112.    bitmapData = malloc( bitmapByteCount );
  113.    colorspace = CGImageGetColorSpace(imageWithMousePointer);
  114.    context = CGBitmapContextCreate (bitmapData,n_width,n_height,8,bitmapBytesPerRow, colorspace,kCGImageAlphaNoneSkipFirst);
  115.    //CGColorSpaceRelease(colorspace);
  116.    CGContextDrawImage(context, CGRectMake(0,0,n_width, n_height), imageWithMousePointer);
  117.    imageWithMousePointerRescaled = CGBitmapContextCreateImage(context);
  118.    CGContextRelease(context);
  119.    free(bitmapData);
  120.    origX = (requestedWidth - newWidth) / 2.0f;
  121.    origY = (requestedHeight - newHeight) / 2.0f;
  122.    
  123.    /************** Insert in box ********************************/
  124.    boundingBoxWidth = (size_t) requestedWidth;
  125.    boundingBoxHeight = (size_t) requestedHeight;
  126.    bytesPerRow = boundingBoxWidth * (8 + 8 + 8 + 8) / 8;
  127.    imgData = (unsigned int*)malloc(boundingBoxHeight*bytesPerRow);
  128.    imageWidth = (float) CGImageGetWidth(imageWithMousePointerRescaled);
  129.    imageHeight = (float) CGImageGetHeight(imageWithMousePointerRescaled);
  130.    memset(imgData, 0, boundingBoxHeight*bytesPerRow);
  131.    context =  CGBitmapContextCreate(imgData, boundingBoxWidth, boundingBoxHeight, 8, bytesPerRow, CGImageGetColorSpace(imageWithMousePointerRescaled), CGImageGetBitmapInfo(imageWithMousePointerRescaled));
  132.    CGContextDrawImage(context, CGRectMake(origX,origY,imageWidth, imageHeight), imageWithMousePointerRescaled);
  133.    imageWithMousePointerRescaledInBox = CGBitmapContextCreateImage(context);
  134.    CGContextRelease(context);    
  135.    free(imgData);
  136.  
  137.    /*********** Convert from RGBA to RGB **********/
  138.    rawData = CGDataProviderCopyData(CGImageGetDataProvider(imageWithMousePointerRescaledInBox));
  139.    buf = (UInt8*) CFDataGetBytePtr(rawData);
  140.  
  141.    for (int x = 0; x < 1280; x++)
  142.    {
  143.       for (int v = 0; v < 720; v++)
  144.       {
  145.          int pixelStartIndex = (x + (v * 1280)) * 4;
  146.          int newPixelStartIndex = (x + (v * 1280)) * 3;
  147.          UInt8 R  = buf[pixelStartIndex + 1];
  148.          UInt8 G = buf[pixelStartIndex + 2];
  149.          UInt8 B = buf[pixelStartIndex + 3];
  150.          bmpimg[newPixelStartIndex] = R;
  151.          bmpimg[newPixelStartIndex + 1] = G;
  152.          bmpimg[newPixelStartIndex + 2] = B;
  153.       }
  154.    }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement