Colorspace conversion problem

Problems with Canvas, OpenGL, etc...

Colorspace conversion problem

Postby rahulbudhiraja » Fri Sep 23, 2011 4:01 am

Hi All,

I am trying to build an AR app and would like to display the image captured from the camera on an opengl plane.The source file on the link :http://nhenze.net/?p=172 shows a texture which was created from the YUV byte array as captured from the camera.I tried to convert the YUV texture provided by OpenCV into RGB using the Img.cvt color and changed the glTexImage2D parameters but that did not work .I then looked at another post http://nhenze.net/?p=253 and read about how to convert the YUV to RGB .However,this app exits abnormally when I try it on the Google Nexus -s .The code on the first link compiles fine though.

My code snippets are :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3. void bindCameraTexture(GL10 gl) {
  4.                        
  5.                 synchronized(this) {
  6.                         if (cameraTexture==null)
  7.                                 cameraTexture=new int[1];
  8.                         else
  9.                                 gl.glDeleteTextures(1, cameraTexture, 0);
  10.                        
  11.                         gl.glGenTextures(1, cameraTexture, 0);
  12.                         int tex = cameraTexture[0];
  13.                        
  14.                         gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
  15.                        
  16.                         gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_LUMINANCE, 256, 256,0, GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE,bytebuffer);
  17.                         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
  18.                        
  19.                
  20.                 }
  21.  
  22.  
  23. public void onPreviewFrame(byte[] yuvs, Camera camera)
  24.         {      
  25.                 int bwCounter=0;
  26.                 int yuvsCounter=0;
  27.                 byteBuffer = ByteBuffer.allocateDirect(yuvs.length);
  28.         imageconversion(yuvs,240,160,byteBuffer,240,160); // Camera Preview size is 240*160 ,imageconversion is the native function.,.
  29.                
  30.         }
  31.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


and on the Native side .... :

Syntax: [ Download ] [ Hide ]
Using cpp Syntax Highlighting
  1. void toRGB565(unsigned short *yuvs, int widthIn, int heightIn, unsigned int *rgbs, int widthOut, int heightOut)
  2. {
  3.           int half_widthIn = widthIn >> 1;
  4.  
  5.           //the end of the luminance data
  6.           int lumEnd = (widthIn * heightIn) >> 1;
  7.           //points to the next luminance value pair
  8.           int lumPtr = 0;
  9.           //points to the next chromiance value pair
  10.           int chrPtr = lumEnd;
  11.           //the end of the current luminance scanline
  12.           int lineEnd = half_widthIn;
  13.  
  14.           int x,y;
  15.           for (y=0;y<heightIn;y++)
  16.           {
  17.             int yPosOut=(y*widthOut) >> 1;
  18.             for (x=0;x<half_widthIn;x++)
  19.             {
  20.               //read the luminance and chromiance values
  21.               int Y1 = yuvs[lumPtr++];
  22.               int Y2 = (Y1 >> 8   ) & 0xff;
  23.               Y1 = Y1 & 0xff;
  24.               int Cr = yuvs[chrPtr++];
  25.               int Cb = ((Cr >> 8  ) & 0xff) - 128;
  26.               Cr = (Cr & 0xff) - 128;
  27.  
  28.               int R, G, B;
  29.               //generate first RGB components
  30.               B = Y1 + ((454 * Cb) >> 8     ) ;
  31.               if (B < 0) B = 0; if (B > 255) B = 255;
  32.               G = Y1 - (( 88 * Cb + 183 * Cr) >> 8    ) ;
  33.               if (G < 0) G = 0; if (G > 255) G = 255;
  34.               R = Y1 + ((359 * Cr) >> 8    ) ;
  35.               if (R < 0) R = 0; if (R > 255) R = 255;
  36.               int val = ((R & 0xf8) << 8     )| ((G & 0xfc) << 3) | (B >> 3) ;
  37.  
  38.               //generate second RGB components
  39.               B = Y1 + ((454 * Cb) >> 8      );
  40.               if (B < 0) B = 0; if (B > 255) B = 255;
  41.               G = Y1 - ((88 * Cb + 183 * Cr) > > 8    );
  42.               if (G < 0) G = 0; if (G > 255) G = 255;
  43.               R = Y1 + ((359 * Cr) > > 8   ) ;
  44.               if (R < 0) R = 0; if (R > 255) R = 255;
  45.  
  46.               rgbs[yPosOut+x] = val | ((((R & 0xf8) << 8 ) | ((G & 0xfc) << 3) | (B >> 3)) << 16);
  47.  
  48.             }
  49.             //skip back to the start of the chromiance values when necessary
  50.             chrPtr = lumEnd + ((lumPtr  >> 1) / half_widthIn) * half_widthIn;
  51.             lineEnd += half_widthIn;
  52.           }
  53. }
  54.  
  55.  
  56. JNIEXPORT void JNICALL Java_com_abc_android_GLLayer_imageconversion (JNIEnv *env, jclass clazz,jbyteArray imageIn, jint widthIn, jint heightIn, jobject imageOut, jint widthOut, jint heightOut,jint filter)
  57. {
  58.  
  59.         jbyte *cImageIn = (env)->GetByteArrayElements(imageIn, NULL);
  60.         jbyte *cImageOut = (jbyte*)(env)->GetDirectBufferAddress(imageOut);
  61.  
  62.         toRGB565((unsigned short*)cImageIn, widthIn, heightIn, (unsigned int*)cImageOut, widthOut, heightOut);
  63.  
  64.         (env)->ReleaseByteArrayElements(imageIn, cImageIn,0);
  65. }
  66.  
Parsed in 0.016 seconds, using GeSHi 1.0.8.4


Can anyone please tell me why the app exits with an error.If I comment the line

// rgbs[yPosOut+x] = val | ((((R & 0xf8) << 8 ) | ((G & 0xfc) << 3) | (B >> 3)) << 16);

The program does not exit .I do not understand y the program exits when it reaches this line .Please can anyone give any ideas what could be the error.

Thanks a lot !

Cheers,
Rahul
rahulbudhiraja
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Sep 23, 2011 3:35 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests