Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author dosse
- */
- public class Renderer {
- private static int nThread = 4;
- public static int[] renderInt(Randomatic r, int offx, int offy, int w, int h, float amp) {
- System.out.println("from " + offy + " to " + (offy + h - 1));
- int[] pixels = new int[w * h];
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- pixels[y * w + x] = (int) (r.getValue(x + offx, y + offy, amp)*255);
- }
- }
- return pixels;
- }
- private static class RenderThread extends Thread {
- private Randomatic r;
- private int offx, offy, w, h;
- private float amp;
- private boolean done=false;
- private int[] data;
- public RenderThread(Randomatic r, int offx, int offy, int w, int h, float amp) {
- this.r = r;
- this.offx = offx;
- this.offy = offy;
- this.w = w;
- this.h = h;
- this.amp = amp;
- }
- @Override
- public void run() {
- data=renderInt(r, offx, offy, w, h, amp);
- done=true;
- }
- public boolean isDone(){return done;}
- public int[] getData(){return data;}
- }
- public static int[] renderIntMT(Randomatic r, int offx, int offy, int w, int h, float amp) {
- if (h <= 256) {
- return renderInt(r, offx, offy, w, h, amp);
- }
- int[] pixels = new int[w * h];
- int portion = (int) Math.floor(h / nThread);
- int c = 0;
- RenderThread[] threads=new RenderThread[nThread+1];
- int[][] rendered = new int[nThread+1][];
- for (int i = 0; i < h; i += portion) {
- int ty = i, th = (i + portion) > h ? h - i : portion;
- threads[c]=new RenderThread(r, offx, i, w, th, amp);
- threads[c].start();
- c++;
- }
- boolean completed=false;
- System.out.println("waiting");
- while(!completed){
- try {Thread.sleep(100);} catch (InterruptedException ex) {}
- completed=true;
- for(int i=0;i<threads.length;i++){
- if(threads[i]==null) continue;
- if(threads[i].isDone()) rendered[i]=threads[i].getData(); else completed=false;
- }
- }
- int k=0; long timestamp=System.nanoTime();
- for(int i=0;i<rendered.length;i++){
- if(rendered[i]==null) continue;
- for(int j=0;j<rendered[i].length;j++){
- pixels[k]=rendered[i][j];
- k++;
- }
- }
- System.out.println("Reassembled in "+(System.nanoTime()-timestamp)/1000000+" ms");
- return pixels;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement