Prime
Prime

Reputation: 4251

C code not compiling correctly

I wrote a simple loop to aid in billboarding that will check if a pixel is white. if so, it will set it to 100% transparency. i wrote it in native code because the java equivalent of this loop took 19 seconds to run for a 256x256 bitmap, too slow.

when compiling:

#include "org_me_renderscene_Billboard.h"

#include <stdio.h>
#include <stdlib.h>

JNIEXPORT jintArray JNICALL Java_org_me_renderscene_Billboard_NativeSetAlphaWhereWhite
  (JNIEnv *envptr, jclass jClass, jintArray pixels, jint length)
{
    int *mPixels = (*int)malloc(length * 4);

    static int currentcolor;
    static int writecolor;
    static int red, green, blue;

    for(int x = 0; x < length; x++)
    {
        currentcolor = pixels[x];

        red = currentcolor << 16;
        green = currentcolor << 8;
        blue = currentcolor;
        if((red == 0) && (green == 0) && (blue == 0))
        {
            mPixels[x] = 0x00000000;
        }
        else
        {
            mPixels[x] = currentcolor;
        }
    }

    return mPixels;

}

the auto-generated stub for which is:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_me_renderscene_Billboard */

#ifndef _Included_org_me_renderscene_Billboard
#define _Included_org_me_renderscene_Billboard
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     org_me_renderscene_Billboard
 * Method:    NativeSetAlphaWhereWhite
 * Signature: ([II)[I
 */
JNIEXPORT jintArray JNICALL Java_org_me_renderscene_Billboard_NativeSetAlphaWhereWhite
  (JNIEnv *, jclass, jintArray, jint);

#ifdef __cplusplus
}
#endif
#endif

i get these errors:

thomas@THOMASDESKLINUX:~/Documents/LinuxProgramming/EclipseWorkspace/RenderScene$ /home/thomas/Documents/LinuxProgramming/AndroidSDKs/android-ndk-r4b/ndk-build
Compile thumb  : Billboardlib <= /home/thomas/Documents/LinuxProgramming/EclipseWorkspace/RenderScene/jni/org_me_renderscene_Billboard.c
/home/thomas/Documents/LinuxProgramming/EclipseWorkspace/RenderScene/jni/org_me_renderscene_Billboard.c: In function 'Java_org_me_renderscene_Billboard_NativeSetAlphaWhereWhite':
/home/thomas/Documents/LinuxProgramming/EclipseWorkspace/RenderScene/jni/org_me_renderscene_Billboard.c:9: error: expected expression before 'int'
/home/thomas/Documents/LinuxProgramming/EclipseWorkspace/RenderScene/jni/org_me_renderscene_Billboard.c:9: error: expected ',' or ';' before 'malloc'
/home/thomas/Documents/LinuxProgramming/EclipseWorkspace/RenderScene/jni/org_me_renderscene_Billboard.c:15: error: 'for' loop initial declarations are only allowed in C99 mode
/home/thomas/Documents/LinuxProgramming/EclipseWorkspace/RenderScene/jni/org_me_renderscene_Billboard.c:15: note: use option -std=c99 or -std=gnu99 to compile your code
/home/thomas/Documents/LinuxProgramming/EclipseWorkspace/RenderScene/jni/org_me_renderscene_Billboard.c:17: warning: dereferencing 'void *' pointer
/home/thomas/Documents/LinuxProgramming/EclipseWorkspace/RenderScene/jni/org_me_renderscene_Billboard.c:17: error: void value not ignored as it ought to be
make: *** [/home/thomas/Documents/LinuxProgramming/EclipseWorkspace/RenderScene/obj/local/armeabi/objs/Billboardlib/org_me_renderscene_Billboard.o] Error 1

why is this happening? my C code should be fine, and these errors dont make much sense.

Upvotes: 1

Views: 2017

Answers (3)

Tim
Tim

Reputation: 9172

int *mPixels = (*int)malloc(length * 4);

should be

int *mPixels = (int*)malloc(length * 4);

or even better

int *mPixels = (int*)malloc(length * sizeof(int));

and also note that this will not properly separate red, green, and blue:

red = currentcolor << 16;
green = currentcolor << 8;
blue = currentcolor;

Given that you're just checking for zero, and you don't really care about the individual RGB values, you can probably just get away with:

if ( (currentcolor & 0x00FFFFFF) == 0)

This will zero out the Alpha from the pixel, leaving behind just the RGB portion. If that whole thing is zero, each color must be zero, so there's no need to check each color individually.

Final thought:

I haven't done much with Android specifically, but isn't 0x000000 black and 0xFFFFFF white? So you're actually matching on black instead of white here.

Upvotes: 2

Jason Rogers
Jason Rogers

Reputation: 19344

what flags are you using in you Android.mk ?

did you set LOCAL_CFLAGS := -std=c99

also

you need to change to this

int *mPixels = (int*)malloc(length * 4);

Upvotes: 3

Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27652

Try (int*) instead of (*int)

Upvotes: 3

Related Questions