I am doing some tests in C (& Java) and one benchmark. I've got code for image deform (from my first answer) and I am rewriting it to C. I want to compare the speed. I"ve done simple image "codec" - colors are stored without compression, RGB (or BGR?) and struct called OneArrayImage (ArrayImage = OneArrayImage*) with some methods and there is an error. When i try to create instance of struct with char* and 3x ints, these data are broken (eg. w = 0, len = -24562...). How should I adjust this struct and methods (ArrayImage image(4) and ArrayImage image_empty(2)) to work? Thanks.
.
(2 main Sources:)
Image.c: http://pastebin.com/c0ZmuRbU
Deformer.c: http://pastebin.com/kCbkGSzm
.
An Error might be in:
struct OneArrayImage {
int* w;
int* h;
int* len;
unsigned char* pix;
};
typedef struct OneArrayImage* ArrayImage;
/*** Prepare ***/
/*** New ***/
ArrayImage image(char* pix, int len, int w, int h) {
if(w < 1 || h < 1 || len < 1 || pix == NULL) {
return NULL;
}
int len2 = (w * h * 3);
if(len2 > len) {
return NULL;
}
struct OneArrayImage img = {&w, &h, &len2, pix};
ArrayImage ret = &img;
return ret;
}
ArrayImage image_empty(int w, int h) {
if(w < 1 || h < 1) {
return NULL;
}
int len = (w * h * 3);
unsigned char* pix = (unsigned char*) malloc(len);
struct OneArrayImage img;// = {&w, &h, &len, pix};
img.w = &w;
img.h = &h;
img.len = &len;
img.pix = pix;
ArrayImage ret = &img;
return ret;
}
main() is in Deformer.c, output:
Starting!
S I : 830835 0
PIX : NOT_NULL
IMG L: NOT_NULL
IMG E: NOT_NULL
PIX L: NULL
PIX E: NULL
.
Working EDIT
/*** Typedef ***/
struct OneArrayImage {
int w;
int h;
int len;
unsigned char* pix;
};
typedef struct OneArrayImage* ArrayImage;
/*** Prepare ***/
/*** New ***/
ArrayImage image(char* pix, int w, int h) {
if(w < 1 || h < 1 || pix == NULL) {
return NULL;
}
int len = (w * h * 3);
ArrayImage image = (ArrayImage) malloc(sizeof(struct OneArrayImage));
image->w = w;
image->h = h;
image->len = len;
image->pix = pix;
return image;
}
ArrayImage image_empty(int w, int h) {
if(w < 1 || h < 1) {
return NULL;
}
int len = (w * h * 3);
unsigned char* pix = (unsigned char*) malloc(len);
ArrayImage image = (ArrayImage) malloc(sizeof(struct OneArrayImage));
image->w = w;
image->h = h;
image->len = len;
image->pix = pix;
return image;
}