简体   繁体   English

C代码无法正确编译

[英]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. 如果是这样,它将设置为100%透明度。 i wrote it in native code because the java equivalent of this loop took 19 seconds to run for a 256x256 bitmap, too slow. 我用本机代码编写它,因为等效于此循环的Java花了19秒才能运行256x256位图,太慢了。

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. 我的C代码应该没问题,这些错误没有多大意义。

尝试使用(int*)而不是(*int)

what flags are you using in you Android.mk ? 您在Android.mk中使用哪些标志?

did you set LOCAL_CFLAGS := -std=c99 您是否设置了LOCAL_CFLAGS:= -std = c99

also

you need to change to this 您需要更改为此

int *mPixels = (int*)malloc(length * 4);
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: 鉴于您只是在检查零,并且您实际上并不在乎各个RGB值,因此您可能可以摆脱:

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

This will zero out the Alpha from the pixel, leaving behind just the RGB portion. 这会将像素的Alpha值清零,仅留下RGB部分。 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? 我没有专门为Android做很多事情,但是0x000000黑色和0xFFFFFF白色不是吗? So you're actually matching on black instead of white here. 因此,您实际上是在这里匹配黑色而不是白色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM