C代码无法正确编译

时间:2010-11-25 07:12:29

标签: android compilation android-ndk

我写了一个简单的循环来帮助广告牌,以检查像素是否为白色。如果是这样,它会将其设置为100%透明度。我用本机代码编写它,因为这个循环的java等价物用了19秒来运行256x256位图,太慢了。

编译时

#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;

}

自动生成的存根,其中包括:

/* 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

我收到这些错误:

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

为什么会这样?我的C代码应该没问题,这些错误没有多大意义。

3 个答案:

答案 0 :(得分:3)

尝试使用(int*)代替(*int)

答案 1 :(得分:3)

你在Android.mk中使用了什么标志?

你设置了LOCAL_CFLAGS:= - std = c99

您需要更改为

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

答案 2 :(得分:2)

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

应该是

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

甚至更好

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

并注意到这将无法正确分隔红色,绿色和蓝色:

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

鉴于您只是检查零,并且您并不真正关心单个RGB值,您可能只是逃避:

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

这会将Alpha从像素中清零,只留下RGB部分。如果整个事物为零,则每种颜色必须为零,因此无需单独检查每种颜色。

最后的想法:

我没有特别使用Android,但不是0x000000黑色和0xFFFFFF白色?所以你实际上在黑色而不是白色匹配。