/*
 * Copyright © Dynamsoft Corporation. All rights reserved.
 */

#import "DSImageData+HostObject.h"
#include "ImageDataHostObject.hpp"

@implementation DSImageData (HostObject)

- (ImageDataHostObject)hostObject {
    int width = (int)self.width;
    int height = (int)self.height;
    int stride = (int)self.stride;
    int format = (int)self.format;
    int orientation = (int)self.orientation;
    NSData *data = self.bytes;
    NSUInteger length = data.length;
    uint8_t *buffer = (uint8_t *)malloc(length);
    if (buffer != NULL) {
        memcpy(buffer, data.bytes, length);
    }
    int bufferSize = (int)length;
    ImageDataHostObject object = ImageDataHostObject(width, height, stride, format, orientation, buffer, bufferSize);
    return object;
}

+ (DSImageData *)imageDataFromHostObject:(ImageDataHostObject)object {
    NSData *data = [NSData dataWithBytes:(const void *)object.buffer length:object.bufferSize];
    NSUInteger width = object.width;
    NSUInteger height = object.height;
    NSUInteger stride = object.stride;
    DSImagePixelFormat format = (DSImagePixelFormat)object.format;
    NSUInteger orientation = object.orientation;
    DSImageData *imageData = [[DSImageData alloc] initWithBytes:data width:width height:height stride:stride format:format orientation:orientation tag:nil];
    return imageData;
}

@end
