1

I'm developing an Android app what uses some native code to process Bitmap. In the native code, I call three methods in sequency from Java code and each one gives a value to the extern ints. The 3rd method needs the values of the first and second extern ints produced by the method 1 and 2 but they return 0, the default value of declaration.

/*header.cpp*/
extern int red1=0;
extern int green1=0;
extern int blu1=0;

/*effect.c*/

static void harris1(AndroidBitmapInfo* info, void* pixels){
int xx, yy, green, red;
uint32_t* line;

for(yy = 0; yy < info->height; yy++){
        line = (uint32_t*)pixels;
        for(xx =0; xx < info->width; xx++){

            blu1 = (int) ((line[xx] & 0x00FF0000) >> 16);
            //private int
            green = (int)((line[xx] & 0x0000FF00) >> 8);
            red = (int) (line[xx] & 0x00000FF );

            blu1 = rgb_clamp((int)(blu1));
            green = rgb_clamp((int)(green));
            red = rgb_clamp((int)(red));

            line[xx] =
                     ((blu1 << 16) & 0x00FF0000) |
                     ((green << 8) & 0x0000FF00) |
                     (red & 0x000000FF);
                    }

            pixels = (char*)pixels + info->stride;
      }}




static void harris2(AndroidBitmapInfo* info, void* pixels){
int xx, yy, blue, red;
    uint32_t* line;

    for(yy = 0; yy < info->height; yy++){
            line = (uint32_t*)pixels;
            for(xx =0; xx < info->width; xx++){
                //private int
                blue = (int) ((line[xx] & 0x00FF0000) >> 16);

                green1 = (int)((line[xx] & 0x0000FF00) >> 8);

                //private int
                red = (int) (line[xx] & 0x00000FF );

                blue = rgb_clamp((int)(blue));
                green1 = rgb_clamp((int)(green1));
                red = rgb_clamp((int)(red));

                line[xx] =
                         ((blue << 16) & 0x00FF0000) |
                         ((green1 << 8) & 0x0000FF00) |
                         (red & 0x000000FF);
                        }

                pixels = (char*)pixels + info->stride;
    }}




static void harris3(AndroidBitmapInfo* info, void* pixels){
int xx, yy, blue, green;
    uint32_t* line;

    for(yy = 0; yy < info->height; yy++){
            line = (uint32_t*)pixels;
            for(xx =0; xx < info->width; xx++){
                //local int
                blue = (int) ((line[xx] & 0x00FF0000) >> 16);
                green = (int)((line[xx] & 0x0000FF00) >> 8);
                //the only values displayed in the final Bitmap
                red1 = (int) (line[xx] & 0x00000FF );

                blue = rgb_clamp((int)(blue));
                green = rgb_clamp((int)(green));
                red1 = rgb_clamp((int)(red1));

             //HERE I USE blu1, green1, red1
                line[xx] =
                         ((blu1 << 16) & 0x00FF0000) |
                         ((green1 << 8) & 0x0000FF00) |
                         (red1 & 0x000000FF);
                        }
            //blu1 and green1 are 0, red has the correct value

                pixels = (char*)pixels + info->stride;

      }}

The result is 0 for blu1, 0 for green1 and the correct value for red1. How can I keep the values of blu1 and green1 after the re-call of the class?

Fabrizio Billeci
  • 572
  • 2
  • 10
  • 18
  • 2
    Please post the code for all three functions. But first make sure that you're not using any local (or member) variables with the same names as your globals. – molbdnilo Nov 02 '15 at 10:00
  • I'm sure the problem is when I re-call the class effect.c . When I use the same code but all in one method everything works well and I get blu1, green1, red1 values. But in the call of the second and the third methods the extern ints return to 0 because is the default value and I don't know how to keep it – Fabrizio Billeci Nov 02 '15 at 10:24
  • When I call the first method, I get the correct value of blu1 but when I call again the class for the second method, then I lose the value blu1 because it returns to 0 as declared into the header. The same for the second and third methods – Fabrizio Billeci Nov 02 '15 at 10:27
  • 1
    There is no class called "effect.c", so there is no chance that the problem is in "re-calling" it. Don't post a vague description of the code, post the actual code. Not an excerpt, not "something like this", but actual code. – molbdnilo Nov 02 '15 at 11:41
  • I edited the question: harris1 gives blu1 ; harris2 gives green1 ; harris3 gives red1 and rebuild the bitmap with the new matrix RGB from blu1, green1, red1 – Fabrizio Billeci Nov 02 '15 at 11:51
  • What exactly do those functions do? Do you really intend to use blue and green from the corner pixel with red from every pixel? – molbdnilo Nov 02 '15 at 12:42
  • I shot three pictures, first bitmap for the blu1 Matrix, second for green1 Matrix and third for red1 matrix and then the program (harris3) build the new bitmap from this combined RGB Matrix. The other private ints exist only because the app crashes without them but I don't use them for the final Bitmap. With these functions I give every pixels for blue and green matrix too but not in this case for the reason I explained, thanks – Fabrizio Billeci Nov 02 '15 at 18:43
  • Here is a link to a very similar question, The linked question should contain enough info to enable you to properly pass the data: – user3629249 Nov 03 '15 at 04:29
  • If your program crashes if you remove the "private" (I suppose you mean *local*) variables, you most likely have a buffer overflow somewhere, or some other memory-related issue. (`line[xx-20]` and `line[xx+10]` look extremely suspicious). You should fix the crashes *properly* first, as it's meaningless to try and fix this problem while your program is undefined. – molbdnilo Nov 03 '15 at 05:39
  • @molbdnilo sorry I posted a wrong version of code. I don't receive errors because the "bug" is that the final Bitmap contains only the R matrix and GB are 0 as default so I suppose their values are not kept before the re-calling – Fabrizio Billeci Nov 03 '15 at 17:47

0 Answers0