<commit_before>//******************************************************************************
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************

#include "Starboard.h"
#include "CoreGraphics/CGContext.h"
#include "CGColorSpaceInternal.h"
#include "CGGradientInternal.h"

@interface CGNSGradient : NSObject {
}
@end

@implementation CGNSGradient
- (void)dealloc {
    delete (__CGGradient*)self;
}

- (id)retain {
    CGGradientRetain((CGGradientRef)self);

    return self;
}

- (void)release {
    CGGradientRelease((CGGradientRef)self);
}
@end

__CGGradient::__CGGradient() {
    isa = NULL;
    object_setClass((id) this, [CGNSGradient class]);
}

__CGGradient::~__CGGradient() {
    free(_components);
    free(_locations);
}

void __CGGradient::initWithColorComponents(const float* components, const float* locations, size_t count, CGColorSpaceRef colorspace) {
    DWORD componentCount = 0;
    __CGColorSpace* cs = (__CGColorSpace*)colorspace;
    _colorSpace = cs->colorSpace;

    switch (_colorSpace) {
        case _ColorRGB:
            componentCount = 3;
            break;

        case _ColorRGBA:
            componentCount = 4;
            break;

        case _ColorGrayscale:
            componentCount = 2;
            break;

        default:
            assert(0);
            break;
    }

    _components = (float*)malloc(sizeof(float) * count * componentCount);
    memcpy(_components, components, sizeof(float) * count * componentCount);

    _locations = (float*)malloc(sizeof(float) * count);

    if (locations) {
        memcpy(_locations, locations, sizeof(float) * count);
    } else {
        for (unsigned i = 0; i < count; i++) {
            _locations[i] = (float)i / (float)(count - 1);
        }
    }

    _count = count;
}

void __CGGradient::initWithColors(CFArrayRef componentsArr, const float* locations, CGColorSpaceRef colorspace) {
    NSArray* components = (NSArray*)componentsArr;
    size_t componentCount = 0;
    __CGColorSpace* cs = (__CGColorSpace*)colorspace;

    _colorSpace = _ColorRGBA;

    switch (_colorSpace) {
        case _ColorRGB:
            componentCount = 3;
            break;

        case _ColorRGBA:
            componentCount = 4;
            break;

        case _ColorGrayscale:
            componentCount = 2;
            break;

        default:
            assert(0);
            break;
    }

    int count = [components count];

    _components = (float*)malloc(sizeof(float) * count * componentCount);

    for (int i = 0; i < count; i++) {
        id curColor = [components objectAtIndex:i];

        float color[4];
        [curColor getColors:color];
        memcpy(&_components[i * componentCount], color, sizeof(float) * count * componentCount);
    }

    _locations = (float*)malloc(sizeof(float) * count);

    if (locations) {
        memcpy(_locations, locations, sizeof(float) * count);
    } else {
        for (int i = 0; i < count; i++) {
            _locations[i] = (float)i / (float)(count - 1);
        }
    }

    _count = count;
}

/**
 @Status Interoperable
*/
CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef colorSpace,
                                                  const float* components,
                                                  const float* locations,
                                                  size_t count) {<commit_msg>Fixed CGGradient crash<commit_after>//******************************************************************************
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************

#include "Starboard.h"
#include "CoreGraphics/CGContext.h"
#include "CGColorSpaceInternal.h"
#include "CGGradientInternal.h"

@interface CGNSGradient : NSObject {
}
@end

@implementation CGNSGradient
- (void)dealloc {
    delete (__CGGradient*)self;
}

- (id)retain {
    CGGradientRetain((CGGradientRef)self);

    return self;
}

- (void)release {
    CGGradientRelease((CGGradientRef)self);
}
@end

__CGGradient::__CGGradient() :
    _components(NULL), 
    _locations(NULL) {
    isa = NULL;
    object_setClass((id) this, [CGNSGradient class]);
}

__CGGradient::~__CGGradient() {
    if (_components) {
        delete [] _components;
        _components = NULL;
    }
    if (_locations) {
        delete [] _locations;
        _locations = NULL;
    }
}

void __CGGradient::initWithColorComponents(const float* components, const float* locations, size_t count, CGColorSpaceRef colorspace) {
    DWORD componentCount = 0;
    __CGColorSpace* cs = (__CGColorSpace*)colorspace;
    _colorSpace = cs->colorSpace;

    switch (_colorSpace) {
        case _ColorRGB:
            componentCount = 3;
            break;

        case _ColorRGBA:
            componentCount = 4;
            break;

        case _ColorGrayscale:
            componentCount = 2;
            break;

        default:
            assert(0);
            break;
    }

    _components = new float[count * componentCount];
    memcpy(_components, components, sizeof(float) * count * componentCount);

    _locations = new float[count];

    if (locations) {
        memcpy(_locations, locations, sizeof(float) * count);
    } else {
        for (unsigned i = 0; i < count; i++) {
            _locations[i] = (float)i / (float)(count - 1);
        }
    }

    _count = count;
}

void __CGGradient::initWithColors(CFArrayRef componentsArr, const float* locations, CGColorSpaceRef colorspace) {
    NSArray* components = (NSArray*)componentsArr;
    size_t componentCount = 0;
    __CGColorSpace* cs = (__CGColorSpace*)colorspace;

    _colorSpace = _ColorRGBA;

    switch (_colorSpace) {
        case _ColorRGB:
            componentCount = 3;
            break;

        case _ColorRGBA:
            componentCount = 4;
            break;

        case _ColorGrayscale:
            componentCount = 2;
            break;

        default:
            assert(0);
            break;
    }

    int count = [components count];

    _components = new float[count * componentCount];

    for (int i = 0; i < count; i++) {
        id curColor = [components objectAtIndex:i];

        float color[4];
        [curColor getColors:color];
        memcpy(&_components[i * componentCount], color, sizeof(float) * componentCount);
    }

    _locations = new float[count];

    if (locations) {
        memcpy(_locations, locations, sizeof(float) * count);
    } else {
        for (int i = 0; i < count; i++) {
            _locations[i] = (float)i / (float)(count - 1);
        }
    }

    _count = count;
}

/**
 @Status Interoperable
*/
CGGradientRef CGGradientCreateWithColorComponents(CGColorSpaceRef colorSpace,
                                                  const float* components,
                                                  const float* locations,
                                                  size_t count) {
{'fraction_non_alphanumeric': 0.13746000492247107, 'fraction_numerical': 0.004922471080482402, 'mean_word_length': 2.474561778537837, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2475669', 'n_tokens_mistral': 2327, 'n_tokens_neox': 2140, 'n_words': 619}