calculating histogram using rsAtomicInc in Renderscript only returns address instead of the count

别说谁变了你拦得住时间么 提交于 2019-12-11 14:56:11

问题


I am trying to write a program for histogram calculation. So i have created a random array of 10 numbers treating it as the image array in 1D. The following is my renderscript and Java code. I want the histogram o be returned as an array with counts of each possible no generated but it only returns the first address of that number encountered. Any help would be much appreciated!

Java code: public class Histcal extends Activity {

private int[] image;   
private int[] luminance;
private float[] nettime;
private int[] difference;
private RenderScript mRS;
private Allocation mInimage;
private Allocation mOutlumhistogram;  
private Allocation mOutallocation2;
private TextView t1;
private ScriptC_histo mScript;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_histcal);
    t1= new TextView(this);
    t1= (TextView)findViewById(R.id.textview1);
    image=randomArray(10) ;

    createScript();
}

public static int[] randomArray(int n) {
    int[] randomArray = new int[n];
    Random randNumGenerator = new Random();
    for (int i = 0; i < n; i++) {
        randomArray[i] = randNumGenerator.nextInt(5);
        log("Generated : " + randomArray[i]);
    }
    return randomArray;


}

private static void log(String aMessage){
    System.out.println(aMessage);
}

private void createScript() {
     int[] luminance = new int[6];
        int[] difference=new int[6];
    log ("i'm in createscript");
     mRS = RenderScript.create(this);

     mInimage = Allocation.createSized(mRS, Element.I32(mRS), image.length);
     mInimage.copyFrom(image);
    mOutlumhistogram = Allocation.createSized(mRS, Element.I32(mRS),luminance.length);
   mScript = new ScriptC_histo(mRS, getResources(), R.raw.histo);

    mScript.bind_gOutarray(mOutlumhistogram);
    mScript.set_gIn(mInimage);
    mScript.set_gOut(mOutlumhistogram);
    mScript.set_gScript(mScript);
    mScript.invoke_increment();
    mOutlumhistogram.copyTo(difference);
        for(int i=0;i<10;i++){
            log("generated histogram counts:"+difference[i]);

        }
}

}


the renderscript code:

#pragma version(1)

#pragma rs java_package_name(com.example.histcal)
#pragma rs_fp_imprecise
#include "rs_time.rsh"
#include "rs_atomic.rsh"


volatile int32_t *gOutarray;
rs_allocation gIn;
rs_allocation gOut;
rs_script gScript; 

  void root(const int32_t *v_in, int32_t *v_out, const void *usrData, uint32_t x,  uint32_t y) {
  int32_t lum=*v_in;
  rsDebug("present value:", *v_in);
  volatile int32_t* addr=gOutarray+lum;
  //rsDebug("present gOutarray address:", *gOutarray);
  rsDebug("present address:", *addr);
 int32_t rsAtomicInc(volatile int32_t * addr);  
   }  

 void increment() {
 rsForEach(gScript, gIn, gOut, NULL); 
  }      

the logcat display where it shows the histogram output is the first address of the first time it encounters each number. Also I'm puzzled seeing how can 2 elements in an array have the same address?

  I/System.out( 1527): Generated : 0
  I/System.out( 1527): Generated : 1
  I/System.out( 1527): Generated : 3
  I/System.out( 1527): Generated : 4
   I/System.out( 1527): Generated : 0
 I/System.out( 1527): Generated : 2
 I/System.out( 1527): Generated : 1
 I/System.out( 1527): Generated : 1
 I/System.out( 1527): Generated : 2
 I/System.out( 1527): Generated : 2
  I/System.out( 1527): i'm in createscript
  V/RenderScript( 1527): rsContextCreate dev=0x2a14ea80
 V/ScriptC ( 1527): Create script for resource = histo
 D/StopWatch( 1527): StopWatch bcc: RSCompilerDriver::loadScriptCache time (us): 2942 
D/StopWatch( 1527): StopWatch bcc: RSCompilerDriver::build time (us): 3763 
 D/RenderScript( 1527): present value: 0  0x0
  D/RenderScript( 1527): present address: 706096640  0x2a162e00
 D/RenderScript( 1527): present value: 1  0x1
 D/RenderScript( 1527): present address: 1074179188  0x4006ac74
 D/RenderScript( 1527): present value: 3  0x3
D/RenderScript( 1527): present address: 0  0x0
 D/RenderScript( 1527): present value: 4  0x4
D/RenderScript( 1527): present address: 0  0x0
  D/RenderScript( 1527): present value: 0  0x0
 D/RenderScript( 1527): present address: 706096640  0x2a162e00
  D/RenderScript( 1527): present value: 2  0x2
  D/RenderScript( 1527): present address: 0  0x0
 D/RenderScript( 1527): present value: 1  0x1
 D/RenderScript( 1527): present address: 1074179188  0x4006ac74
  D/RenderScript( 1527): present value: 1  0x1
 D/RenderScript( 1527): present address: 1074179188  0x4006ac74
 D/RenderScript( 1527): present value: 2  0x2
  D/RenderScript( 1527): present address: 0  0x0
 D/RenderScript( 1527): present value: 2  0x2
 D/RenderScript( 1527): present address: 0  0x0
 I/System.out( 1527): generated histogram counts:706096640
I/System.out( 1527): generated histogram counts:1074179188
I/System.out( 1527): generated histogram counts:0
I/System.out( 1527): generated histogram counts:0

I/System.out( 1527): generated histogram counts:0 I/System.out( 1527): generated histogram counts:-1879038464

D/AndroidRuntime( 1527): Shutting down VM
W/dalvikvm( 1527): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
E/AndroidRuntime( 1527): FATAL EXCEPTION: main

回答1:


-1- I tested your code on API 19 and Nexus 5 and I see that difference[] prints are coming out correctly. My Java code is the same as yours, below is the script code with some minor differences.

#pragma version(1)
#pragma rs java_package_name(com.clearvision.androidapps.clearview)
#pragma rs_fp_full

volatile int32_t *gOutarray;
rs_allocation gIn;
rs_allocation gOut;
rs_script gScript; 

  void root(const int32_t *v_in, int32_t *v_out, const void *usrData, uint32_t x,  uint32_t y) {
      int32_t lum=*v_in;
      rsDebug("present value:", *v_in);
      volatile int32_t* addr=gOutarray + lum;
      //rsDebug("present gOutarray address:", *gOutarray);
      rsDebug("present address:", *addr);
      rsAtomicInc(addr);  
  }  

 void increment() {
        rsForEach(gScript, gIn, gOut); 
} 

-2- I was previously incorrect. Below code is fine.

volatile int32_t* addr=gOutarray + lum;

-3- Your logcat probably shows error because of the following

    for(int i=0;i<10;i++){
        log("generated histogram counts:"+difference[i]);
    }

difference array is defined with 6 int elements, while you are looping till 10. This is a problem.

Can you recheck?



来源:https://stackoverflow.com/questions/22336832/calculating-histogram-using-rsatomicinc-in-renderscript-only-returns-address-ins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!