Rotating a ppm image 90 degrees to the right in C -


i have following problem rotating ppm image right first 2 lines in result image black (or color rainbow)

here's code sets buffer image (the variables g_width , g_height set function)

struct pixel *image = malloc(sizeof(struct pixel) * g_width * g_height); 

here's function pointer passed it

void rotate90(struct pixel *img) {     int i, j, size, th;     size = sizeof(struct pixel) * g_width * g_height;     struct pixel *buffer = malloc(size);      if (buffer == null) {         fprintf(stderr, "unable allocate memory\n");         exit(exit_failure);     }      (i = 0; < g_height; i++) {         (j=0; j < g_width; j++) {             buffer[(g_height*j)+(g_height-i)] = img[(g_width*i) + j];         }     }      //copy buffer image pointer     memcpy(img, buffer, size);      //free buffer , swap width , height around     free(buffer);     th = g_height;     g_height = g_width;     g_width = th; } 

if print image buffer comes out fine, if rotate it comes out (note first 2 lines of pixels)

https://www.dropbox.com/s/vh8l6s26enbxj42/t3.png?dl=0

it's if last 2 lines aren't being swapped @ all, please help

edit: solved second black line @ least, still need last line

as said mix first line (and overflow)

void rotate90(struct pixel *img) {     int i, j, size, th;     size = sizeof(struct pixel) * g_width * g_height;     struct pixel *buffer = malloc(size);      if (buffer == null) {         fprintf(stderr, "unable allocate memory\n");         exit(exit_failure);     }      (i = 0; < g_height; i++) {         (j=0; j < g_width; j++) {             buffer[(g_height*j)+(g_height-i -- 1)] = img[(g_width*i) + j];         }     }      //copy buffer image pointer     memcpy(img, buffer, size);      //free buffer , swap width , height around     free(buffer);     th = g_height;     g_height = g_width;     g_width = th; } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -