C deep copy struct with pointer -
i'm new c , need solve following problem.
i have project reading small bmp (512x512) image. i've managed change colors , have mirrored (both horizontal vertical). though need turn -90°. function can't manage work deepcopybitmap().
i keep getting following error on *copy->raster[i]
:
indirection requires pointer operand ('pixel' (aka 'struct _pixel') invalid)
rotation (512x512)
typedef struct _pixel { unsigned char blue; unsigned char green; unsigned char red; } pixel; typedef struct _bitmap { char file_path[path_max+1]; char magic_number[3]; unsigned int size; unsigned char application[5]; unsigned int start_offset; unsigned int bitmapheadersize; unsigned int width; unsigned int height; unsigned short int depth; unsigned char* header; pixel* raster; } bitmap; void rotate(bitmap* bmp) { int i; int j; pixel* originalpixel; bitmap* originalbmp; deepcopybitmap(bmp, originalbmp); for(j=1; j <= bmp->height; j++) { for(i=1; <= bmp->width; i++) { originalpixel=getpixel(originalbmp->raster, bmp->width, bmp->height, j, i); setpixel(bmp->raster, bmp->width, bmp->height, (bmp->width + 1 - i), j, originalpixel); } } } void deepcopybitmap(bitmap* bmp, bitmap* copy) { *copy = *bmp; if (copy->raster) { copy->raster = malloc(sizeof(*copy->raster)); (int = 0; < copy->height; i++) { copy->raster[i] = malloc(sizeof(*copy->raster[i])); memcpy(copy->raster[i], bmp->raster[i], sizeof(*copy->raster[i])); } } }
update
void deepcopybitmap(bitmap* bmp, bitmap* copy) { copy = malloc(sizeof(bitmap)); *copy = *bmp; if (copy->raster) { size_t total_size = copy->height * copy->width * sizeof(pixel); copy->raster = malloc(total_size); memcpy(copy->raster, bmp->raster, total_size); } }
you allocate 1 pixel
here:
copy->raster = malloc(sizeof(*copy->raster));
however, need @ least copy->height
pixel
s iteration work:
(int = 0; < copy->height; i++) {
here, allocate 1 pixel
again:
copy->raster[i] = malloc(sizeof(*copy->raster[i]));
but, intend copy copy->width
pixel
s, not one:
memcpy(copy->raster[i], bmp->raster[i], sizeof(*copy->raster[i])); }
what want allocate copy->height * copy->width
pixel
s, , copy them original.
size_t total_size = copy->height * copy->width * sizeof(pixel); copy->raster = malloc(total_size); memcpy(copy->raster, bmp->raster, total_size);
Comments
Post a Comment