C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 1st, 2011, 09:28 AM
232studios 232studios is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 3 232studios User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 55 sec
Reputation Power: 0
Syntax errors - Converting Java to C

Hi there,

I'm trying to perform some image processing using the Android NDK. I have a snippet of Java code that converts a YUV bitmap to RGB format, however, I need to convert it to C.

This is what I have so far:

Code:
JNIEXPORT void JNICALL Java_com_twothreetwo_zoomplus_ZoomPlus_YUVtoRGB(JN  IEnv * env, jobject obj, jintArray rgb, jbyteArray yuv420sp, jint width, jint height)
{
	int             sz;
	int             i;
	int             j;
	int             Y;
	int             Cr = 0;
	int             Cb = 0;
	int             pixPtr = 0;
	int             jDiv2 = 0;
	int             R = 0;
	int             G = 0;
	int             B = 0;
	int             cOff;

	sz = width * height;
	 //if(out == null) throw new NullPointerException("buffer 'out' is null");
	 //if(out.length < sz) throw new IllegalArgumentException("buffer 'out' size " + out.length + " < minimum " + sz);
	 //if(fg == null) throw new NullPointerException("buffer 'fg' is null");
	 //if(fg.length < sz) throw new IllegalArgumentException("buffer 'fg' size " + fg.length + " < minimum " + sz * 3/ 2);
	 for(j = 0; j < height; j++) {
			 pixPtr = j * width;
			 jDiv2 = j >> 1;
			 for(i = 0; i < width; i++) {
					 Y = yuv420sp[pixPtr]; if(Y < 0) Y += 255;
					 if((i & 0x1) != 1) {
							 cOff = sz + jDiv2 * width + (i >> 1) * 2;
							 Cb = yuv420sp[cOff];
							 if(Cb < 0) Cb += 127; else Cb -= 128;
							 Cr = yuv420sp[cOff + 1];
							 if(Cr < 0) Cr += 127; else Cr -= 128;
					 }
					 R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
					 if(R < 0) R = 0; else if(R > 255) R = 255;
					 G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
					 if(G < 0) G = 0; else if(G > 255) G = 255;
					 B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
					 if(B < 0) B = 0; else if(B > 255) B = 255;
					 rgb[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
			 }
	 }

}


created from the Java version:

Code:
public static void decodeYUV(int[] out, byte[] fg, int width, int height) throws NullPointerException, IllegalArgumentException 
     { 
    		         final int sz = width * height; 
    		         if(out == null) throw new NullPointerException("buffer 'out' is null"); 
    		         if(out.length < sz) throw new IllegalArgumentException("buffer 'out' size " + out.length + " < minimum " + sz); 
    		         if(fg == null) throw new NullPointerException("buffer 'fg' is null"); 
    		         if(fg.length < sz) throw new IllegalArgumentException("buffer 'fg' size " + fg.length + " < minimum " + sz * 3/ 2); 
    		         int i, j; 
    		         int Y, Cr = 0, Cb = 0; 
    		         for(j = 0; j < height; j++) { 
    		                 int pixPtr = j * width; 
    		                 final int jDiv2 = j >> 1; 
    		                 for(i = 0; i < width; i++) { 
    		                         Y = fg[pixPtr]; if(Y < 0) Y += 255; 
    		                         if((i & 0x1) != 1) { 
    		                                 final int cOff = sz + jDiv2 * width + (i >> 1) * 2; 
    		                                 Cb = fg[cOff]; 
    		                                 if(Cb < 0) Cb += 127; else Cb -= 128; 
    		                                 Cr = fg[cOff + 1]; 
    		                                 if(Cr < 0) Cr += 127; else Cr -= 128; 
    		                         } 
    		                         int R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5); 
    		                         if(R < 0) R = 0; else if(R > 255) R = 255; 
    		                         int G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 
    		 3) + (Cr >> 4) + (Cr >> 5); 
    		                         if(G < 0) G = 0; else if(G > 255) G = 255; 
    		                         int B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6); 
    		                         if(B < 0) B = 0; else if(B > 255) B = 255; 
    		                         out[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R; 
    		                 } 
    		         } 
    		 } 


However I'm getting the following errors:

Code:
apps/zoomplusndk/jni/zoomplusndk.c:53: warning: dereferencing 'void *' pointer
apps/zoomplusndk/jni/zoomplusndk.c:53: error: void value not ignored as it ought to be
apps/zoomplusndk/jni/zoomplusndk.c:56: warning: dereferencing 'void *' pointer
apps/zoomplusndk/jni/zoomplusndk.c:56: error: void value not ignored as it ought to be
apps/zoomplusndk/jni/zoomplusndk.c:58: warning: dereferencing 'void *' pointer
apps/zoomplusndk/jni/zoomplusndk.c:58: error: void value not ignored as it ought to be
apps/zoomplusndk/jni/zoomplusndk.c:67: warning: dereferencing 'void *' pointer
apps/zoomplusndk/jni/zoomplusndk.c:67: error: invalid use of void expression


Does anyone know why I'm getting these errors? As you can probably tell I'm not a C developer :/.

Thank a lot!

Reply With Quote
  #2  
Old December 1st, 2011, 06:34 PM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
There are no identifiers declared as void* in the function (arguments or body) so it's relying on some global value of that type.

Unless it's one of the parameter types that's a typedef or #define to a void*.

void* kinda means "memory area (starting at ...)" without specifying how it should be accessed. As such, accessing it is an error. It seems your compiler is allowing that, producing a void value - which it then cannot use in an expression.

Look at the lines mentioned, and find out what identifier(s) has the void* type.
The way you're using it, it probably shouldn't be a void* to begin with. OR, you're expected to cast it to a pointer to something, and access through that.
__________________
Quote:
Programming by Coincidence
Fred types in some more code, tries it, and it still seems to work. [Then] the program suddenly stops working. [...] Fred doesnít know why the code is failing because he didnít know why it worked in the first place.
Undefined behavior results in: (worst to best)
-Erases your harddisk. Really.
-Appears to work - for now
-Delayed errors/crashes
-Crashes
-Compiler warning

Last edited by MaHuJa : December 1st, 2011 at 06:36 PM.

Reply With Quote
  #3  
Old December 2nd, 2011, 05:11 AM
232studios 232studios is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 3 232studios User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 55 sec
Reputation Power: 0
Quote:
Originally Posted by MaHuJa
There are no identifiers declared as void* in the function (arguments or body) so it's relying on some global value of that type.

Unless it's one of the parameter types that's a typedef or #define to a void*.

void* kinda means "memory area (starting at ...)" without specifying how it should be accessed. As such, accessing it is an error. It seems your compiler is allowing that, producing a void value - which it then cannot use in an expression.

Look at the lines mentioned, and find out what identifier(s) has the void* type.
The way you're using it, it probably shouldn't be a void* to begin with. OR, you're expected to cast it to a pointer to something, and access through that.


Thanks! I've tried adding void * and I'm still getting the same error. E.g:

Code:
Y = (void *)yuv420sp[pixPtr];


I guess I'm not using it correctly?

Reply With Quote
  #4  
Old December 2nd, 2011, 08:23 PM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
You're about 180 degrees from getting it right. Now you're converting a void to a void*. The original problem was converting from void to int.

I'm really guessing here - but what you want is probably more along the lines of
Y = ((unsigned char*)yuv420sp) [pixPtr];
In words, read the pointer to "memory" as a pointer to a char array, and access element pixPtr of that array.
In particular, I'm guessing that by byte array, it really means just that, and that you're only interested in that particular byte (as opposed to an int made of several bytes from there.) (A char is 1 byte by definition.)

An alternate solution would be to rename the parameter to jbyteArray yuv420sp_v, and make a new value const unsigned char* yuv420sp = yuv420sp_v; in the function body. This achieves the same thing in almost the same way.

Reply With Quote
  #5  
Old December 3rd, 2011, 07:00 AM
232studios 232studios is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 3 232studios User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 55 sec
Reputation Power: 0
Thanks a lot ! That works!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Syntax errors - Converting Java to C


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap