2010-11-25 99 views
1

我寫了一個簡單的循環來幫助顯示廣告牌,檢查像素是否是白色的。如果是這樣,它會將其設置爲100%透明度。我把它寫在本地代碼中,因爲這個循環的java等價物需要19秒才能運行256x256位圖,速度太慢。C代碼編譯不正確

編譯時:

#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 

我得到這些錯誤:

[email protected]:~/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代碼應該沒問題,這些錯誤沒有多大意義。

回答

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) 

這將零出阿爾法從像素開始,只留下RGB部分。如果整個事物爲零,每種顏色必須爲零,所以不需要單獨檢查每種顏色。

最後的思考:

我還沒有與Android做了很多具體,而不是0x000000處的黑色和白色0XFFFFFF?所以你實際上在黑色而不是白色匹配。

3

嘗試的(int*)代替(*int)

3

你在你的Android.mk中使用了哪些標誌?

你設置LOCAL_CFLAGS:= -std = C99

您需要更改這個

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