<commit_before>//
//
//  screen_shot.cpp
//
//  Created by <NAME> on 3/18/13.
//  MIT-LIcense Copyright (c) 2013 <NAME>.
//

#include <ApplicationServices/ApplicationServices.h>
#include "screen_shot.h"


Screenshot::Screenshot(CGContextRef bitmap_context) : _bitmap_context(bitmap_context) {}

Screenshot::~Screenshot() {
  CGContextRelease(_bitmap_context); 
}

void* Screenshot::get_bitmap() {
  return CGBitmapContextGetData(_bitmap_context);
}

size_t Screenshot::get_width() {
  return CGBitmapContextGetWidth(_bitmap_context);
}

size_t Screenshot::get_height() {
  return CGBitmapContextGetHeight(_bitmap_context);
}

size_t Screenshot::get_bits_per_pixel() {
  return CGBitmapContextGetBitsPerPixel(_bitmap_context);
}

bool Screenshot::write_png(const char* filepath) {
  CFURLRef url = CFURLCreateWithFileSystemPath(
    NULL, 
    CFStringCreateWithCString(NULL, filepath, kCFStringEncodingUTF8),
    kCFURLPOSIXPathStyle,
    false);
  CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
  
  store_image(destination);
    
  CFRelease(destination);

  return true;
}

CFMutableDataRef Screenshot::create_png_data(size_t &length) {
  
  length = get_width() * get_height() * (get_bits_per_pixel()/8);
  
  CFMutableDataRef png_data         = CFDataCreateMutable(NULL, 0);
  CGImageDestinationRef destination = CGImageDestinationCreateWithData(png_data, kUTTypePNG, 1, NULL);
  
  if (!store_image(destination)) {
    length = 0;
  }
  
  CFRelease(destination);
  
  return png_data;  
}

bool Screenshot::store_image(CGImageDestinationRef destination) {
  CGImageRef image = CGBitmapContextCreateImage(_bitmap_context);

  CGImageDestinationAddImage(destination, image, nil);

  if (!CGImageDestinationFinalize(destination)) {
      fprintf(stderr, "Failed to write image to destination\n");
      return false;
  }
  
  CFRelease(image);<commit_msg>Specify the length to avoid issues with pointer out of bounds problems should the png generated take less space than expected.<commit_after>//
//
//  screen_shot.cpp
//
//  Created by <NAME> on 3/18/13.
//  MIT-LIcense Copyright (c) 2013 <NAME>.
//

#include <ApplicationServices/ApplicationServices.h>
#include "screen_shot.h"


Screenshot::Screenshot(CGContextRef bitmap_context) : _bitmap_context(bitmap_context) {}

Screenshot::~Screenshot() {
  CGContextRelease(_bitmap_context); 
}

void* Screenshot::get_bitmap() {
  return CGBitmapContextGetData(_bitmap_context);
}

size_t Screenshot::get_width() {
  return CGBitmapContextGetWidth(_bitmap_context);
}

size_t Screenshot::get_height() {
  return CGBitmapContextGetHeight(_bitmap_context);
}

size_t Screenshot::get_bits_per_pixel() {
  return CGBitmapContextGetBitsPerPixel(_bitmap_context);
}

bool Screenshot::write_png(const char* filepath) {
  CFURLRef url = CFURLCreateWithFileSystemPath(
    NULL, 
    CFStringCreateWithCString(NULL, filepath, kCFStringEncodingUTF8),
    kCFURLPOSIXPathStyle,
    false);
  CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
  
  store_image(destination);
    
  CFRelease(destination);

  return true;
}

CFMutableDataRef Screenshot::create_png_data(size_t &length) {
  
  length = get_width() * get_height() * (get_bits_per_pixel()/8);
  
  CFMutableDataRef png_data         = CFDataCreateMutable(NULL, length);
  CGImageDestinationRef destination = CGImageDestinationCreateWithData(png_data, kUTTypePNG, 1, NULL);
  
  if (!store_image(destination)) {
    length = 0;
  }
  
  CFRelease(destination);
  
  return png_data;  
}

bool Screenshot::store_image(CGImageDestinationRef destination) {
  CGImageRef image = CGBitmapContextCreateImage(_bitmap_context);

  CGImageDestinationAddImage(destination, image, nil);

  if (!CGImageDestinationFinalize(destination)) {
      fprintf(stderr, "Failed to write image to destination\n");
      return false;
  }
  
  CFRelease(image);
{'fraction_non_alphanumeric': 0.10350966429298067, 'fraction_numerical': 0.007375381485249237, 'mean_word_length': 5.405537459283388, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7171705', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1309, 'n_words': 229}