-
December 1st, 2011, 10:46 AM
#1
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(JNIEnv * 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!
P.S line 53 is: Y = yuv420sp[pixPtr]; if(Y < 0) Y += 255;
-
December 1st, 2011, 11:42 AM
#2
Re: Syntax errors - Converting Java to C
Originally Posted by 232studios
Does anyone know why I'm getting these errors? As you can probably tell I'm not a C developer :/.
Your code is not 'C' it is C++ as seen by you trying to throw an exception (even though it's commented out). The 'C' language has no "throw" syntax.
You could have whittled your program down to something even a beginner would understand:
Code:
int main()
{
void *p;
int x;
x = *p;// error
}
You cannot aereference a void pointer to point to anything just like that. The reason being that a void pointer doesn't point to any particular type. In the example above, the void pointer p has no idea that x is an int, so the dereferencing will not work.
The fix is to cast the void pointer to the appropriate type:
Code:
int main()
{
void *p[10];
int x;
x = *(int*)p;
}
This cast the void* to an int*. Now the outer * dereferences the integer from p.
So those variables you have in your code, something is declared as a void*, and you're doing the first code I posted above, which is illegal.
Code:
jbyteArray yuv420sp
Since you are in a C++ forum, you should let persons reading this forum know what a "jbyteArray" is in terms of C++. Since this is where you say the error occurs, it is important to give this information to us.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 1st, 2011 at 11:47 AM.
-
December 1st, 2011, 11:48 AM
#3
Re: Syntax errors - Converting Java to C
Originally Posted by 232studios
I need to convert it to C.
Do it little by little.
Start by making sure you can pass the parameters correctly. Then add code to the function in small increments and make sure you know what you're doing and that it works the same in both versions.
-
December 2nd, 2011, 05:17 AM
#4
Re: Syntax errors - Converting Java to C
Hi Paul,
Thanks for your help. The code I'm trying to convert it to is C not C++. It probably looks a lot like C++ because it's being ported from Java (see original post) and the commented out lines are the original from Java. I posted both the original Java as well as my poor attempt to convert it to C as I thought it would help.
The jbyteArray yuv420sp is just an array of bytes. It was auto generated when I created the .h (header?) file so I assumed it was ok. Should I convert it to an object instead?
I've tried adding void * at the start of each yuv420sp[] but I'm still getting the same errors. e.g:
Code:
Y = (void *)yuv420sp[pixPtr];
Am I implementing it incorrectly?
Thanks a lot!!
-
December 2nd, 2011, 05:38 AM
#5
Re: Syntax errors - Converting Java to C
writing c ( or c++ ) code without having at least a basic understanding of the language is simply masochistic ; even if you get your code to compile you could end up with a program not working as expected or, worse, showing erratical unreliable behavior.
Originally Posted by 232studios
The jbyteArray yuv420sp is just an array of bytes
indeed, I've never used JNI nor Java, but jbyteArray does not appear as an array of bytes and the compiler error that you showed us probably implies that all those "jX" things are just opaque handles. In fact, a quick google search gives this link explaining how you actually access those objects, like, for instance, the GetByteArrayElements() function.
EDIT: BTW, are you converting Java to c code or interfacing Java to a c function ? I ask, because in the former case using JNI seems pointless ...
Last edited by superbonzo; December 2nd, 2011 at 05:45 AM.
-
December 2nd, 2011, 07:06 AM
#6
Re: Syntax errors - Converting Java to C
Originally Posted by 232studios
Thanks for your help. The code I'm trying to convert it to is C not C++. It probably looks a lot like C++ because it's being ported from Java (see original post) and the commented out lines are the original from Java. I posted both the original Java as well as my poor attempt to convert it to C as I thought it would help.
As superbonzo pointed out, doing a line-by-line conversion from Java to C++ is not recommended, especially with the code you have.
The jbyteArray yuv420sp is just an array of bytes.
Please show the C++ definition of what a jbyteArray is -- don't describe it, actually show us how it's defined. We need the exact definition.
It was auto generated when I created the .h (header?) file so I assumed it was ok. Should I convert it to an object instead?
You don't seem to understand.
There is no such thing in C++ as a "jbyteArray" or any of those "JNIxxx" types. Those are typedefs defined somewhere in this header file you generated. Those typedefs map to bonafide C++ types in some manner, and you are to show us what they are in their true C++ form.
I've tried adding void * at the start of each yuv420sp[] but I'm still getting the same errors. e.g:
Of course you get the errors, because you're doing exactly what I was saying was wrong. You cannot turn a pointer to a void to something concrete without casting it to a pointer to the concrete type -- that was the error all along, but you did nothing to solve it except repeated the error.
The bottom line is to actually learn C++, so that you don't go down the road of creating code that is faulty. Unlike Java, when you make a mistake in C++ (other than syntax errors), there is no guarantee that the mistake will show up when you run the program. In other words, C++ has something Java doesn't have -- programs behaving in an undefined manner at runtime, all due to errors in programming.
Regards,
Paul McKenzie
-
December 2nd, 2011, 07:43 AM
#7
Re: Syntax errors - Converting Java to C
I'll add you're not doing yourself any favors with that writing style.
if(G < 0) G = 0; else if(G > 255) G = 255;
That's 5 separate lines of code there. Very hard to read and close to impossible to debug.
Code:
if(G < 0)
G = 0;
else
if(G > 255)
G = 255;
Sure it takes up more space, but it's a whole lot easier to see what's happening and your debugger can step to each line so you can watch it run.
-
December 3rd, 2011, 07:12 AM
#8
Re: Syntax errors - Converting Java to C
Ok fair enough, this is trickier that I thought...although I'm still not sure why C++ is still being mentioned as I keep saying it's C I'm after?
To get it to compile I added (unsigned char*) to each yuv420sp[pixPtr]...but as you've all mentioned this isn't enough to get it working so I'm looking at another way of doing what I need.
Thanks for all your help!
-
December 3rd, 2011, 08:18 AM
#9
Re: Syntax errors - Converting Java to C
Originally Posted by 232studios
Ok fair enough, this is trickier that I thought...although I'm still not sure why C++ is still being mentioned as I keep saying it's C I'm after?
Just a quick glance at your code I see the words throw and new. Those are only valid in C++.
-
December 3rd, 2011, 11:21 AM
#10
Re: Syntax errors - Converting Java to C
Originally Posted by 232studios
Ok fair enough, this is trickier that I thought...although I'm still not sure why C++ is still being mentioned as I keep saying it's C I'm after?
I already mentioned that the code you had was C++ due to the calls to "new" and "throw". These keywords do not exist in the C language.
Second, regardless of what we've stated so far, it makes no difference if it's C or C++. That doesn't change the fact that there is no such thing as a jbyteArray or JNIEnv or any of those proprietary types in C or C++.
It also doesn't change the fact that bad coding (usually as a result of mismanaging pointers) in either C or C++ can produce programs that have undefined behaviour -- the program may work today and fail tomorrow, it could work on your machine but fail on another machine etc, even though the data given to the program is exactly the same. In Java, there is no such thing as "undefined behaviour" -- either the program has a logical bug and the program behaves the wrong way all the time, or the Java language itself has bounds checking and other such things, always ready to throw an exception if you do something wrong.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|