c++ - How can I flip a buffer vertically during an YUV to RGB conversion -


edit : question not clear sorry, updated , added details.

i have buffer image data (yuv format) convert rgb format. problem is, flip image vertically (invert y-position).

what i'm able moment convert yuv data rgb data in buffer, flip buffer vertically.

here working code :

unsigned char* decklinkcapturedelegate::convertyuvtorgb(void* framebytes) {     unsigned char *mycopy = new unsigned char[height*width*3];     unsigned char *flippedcopy = new unsigned char[height*width*3];     unsigned char* pdata = (unsigned char *) framebytes;       //conversion yuv rgb      for(int = 0, j=0; < width * height * 3; i+=6, j+=4)     {         unsigned char v = pdata[j];         unsigned char y = pdata[j+1];         unsigned char u = pdata[j+2];          mycopy[i+2] = 1.0*y + 8 + 1.402*(v-128);               // r         mycopy[i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g         mycopy[i] = 1.0*y + 1.772*(u-128) + 0;                            // b          y = pdata[j+3];         mycopy[i+5] = 1.0*y + 8 + 1.402*(v-128);               // r         mycopy[i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g         mycopy[i+3] = 1.0*y + 1.772*(u-128) + 0;     }      //vertical flip      (int = 0; < width; ++i) {         (int j = 0; j < height; ++j) {             (int k = 0; k < 3; ++k) {                 flippedcopy[(i + j * width) * 3 + k] = mycopy[(i + (height - 1 - j) * width) * 3 + k];             }         }     }      return flippedcopy;    } 

what gain performance flip buffer during conversion yuv rgb. had no idea how , yusuf answer helped me, here have moment :

unsigned char* decklinkcapturedelegate::convertyuvtorgb(void* framebytes) {     unsigned char *mycopy = new unsigned char[height*width*3];     unsigned char* pdata = (unsigned char *) framebytes;     int k = height - 1;      for(int = 0, j=0; < width * height * 3; i+=6, j+=4)     {             unsigned char v = pdata[j];             unsigned char y = pdata[j+1];             unsigned char u = pdata[j+2];              mycopy[(width*k*3) + i+2] = 1.0*y + 8 + 1.402*(v-128);               // r             mycopy[(width*k*3) + i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g             mycopy[(width*k*3) + i] = 1.0*y + 1.772*(u-128) + 0;                            // b              y = pdata[j+3];             mycopy[(width*k*3) + i+5] = 1.0*y + 8 + 1.402*(v-128);               // r             mycopy[(width*k*3) + i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g             mycopy[(width*k*3) + i+3] = 1.0*y + 1.772*(u-128) + 0;              if (<i multiple of (width*3)-1>){                  k = k - 2;             }      }      return mycopy; } 

if i'm correct, should work, assuming if condition right. don't know how express if condition, since i incremented 6 each time, might "skip" right moment decrement k

i hope i'm clear enough. thanks

i assume width even, otherwise "reduce in new line"-if complicated, must use 2 loops. didn't tested, should this;

unsigned char* decklinkcapturedelegate::convertyuvtorgb(void* framebytes) {     unsigned char *mycopy = new unsigned char[height*width*3];     unsigned char* pdata = (unsigned char *) framebytes;     unsigned int k = height - 1;      for(int = 0, j=0; < width * height * 3; i+=6, j+=4)     {         unsigned char v = pdata[j];         unsigned char y = pdata[j+1];         unsigned char u = pdata[j+2];          mycopy[(width*k*3) + i+2] = 1.0*y + 8 + 1.402*(v-128);               // r         mycopy[(width*k*3) + i+1] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g         mycopy[(width*k*3) + i] = 1.0*y + 1.772*(u-128) + 0;                            // b          y = pdata[j+3];         mycopy[(width*k*3) + i+5] = 1.0*y + 8 + 1.402*(v-128);               // r         mycopy[(width*k*3) + i+4] = 1.0*y - 0.34413*(u-128) - 0.71414*(v-128);   // g         mycopy[(width*k*3) + i+3] = 1.0*y + 1.772*(u-128) + 0;          if (mod(i, width*3) == 0) //reduce in new line (i not sure how reduce it, should think here)             k = k - 2;     }      return mycopy; } 

and tag question imageprocessing etc not c++


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -