Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

143
LINES

< > BotCompany Repo | #672 // Focal points (developing)

JavaX source code - run with: x30.jar

!636

!629 // standard functions
!658 // image classes
!673 // thread {

import java.awt.*;
import java.awt.image.*;
import java.util.List;
import javax.imageio.*;
import java.lang.reflect.*;

main {
  static Point[] usersFocalPoints = {
    new Point(205, 233),
    new Point(266, 172),
    new Point(327, 229),
    new Point(266, 294)
  };
  
  static int spatialTolerance = 10; // generous
  
  static RGBImage originalImage;

  // returns a score (higher = better)
  static int checkFocalPoints(Point[] points, Point[] ref) {
    int correct = 0;
    List<Point> list = new ArrayList<Point>(Arrays.asList(points));
    for (Point p : ref) {
      Point u = findPoint(p, list);
      if (u != null) {
        ++correct;
        list.remove(u);
      }
    }
    int superfluous = list.size();
    return correct-superfluous;
  }
  
  static boolean nearEnough(Point p, Point u) {
    return sqr(p.x-u.x) + sqr(p.x-u.y) <= sqr(spatialTolerance);
  }
  
  static double sqr(double a) { return a*a; }
  
  static Point findPoint(Point p, List<Point> list) {
    for (Point u : list) {
      if (nearEnough(p, u))
        return u;
    }
    return null;
  }
  
  static void markPoints(RGBImage img, List<Point> list,
    Color color) {
    for (Point p : list) {
      for (int y = p.y-1; y <= p.y+1; y++)
        for (int x= p.x-1; x <= p.x+1; x++)
          img.setPixel(x, y, x == p.x && y == p.y ? Color.white : color);
    }
  }
  
  psvm {
    String imageID = "#1000335"; // Picture of a big dot
    if (args.length != 0) imageID = args[0];
    final String _imageID = imageID;
    
    final ImageSurface imageSurface = new ImageSurface();
    makeFrame(new JScrollPane(imageSurface));
    
    thread {
      originalImage = loadImage(_imageID);
      RGBImage img = new RGBImage(originalImage);
      markPoints(img, Arrays.asList(usersFocalPoints), Color.red);
      imageSurface.setImage(img);
    }.start();
  }
  
  static void makeFrame(Component panel) {
    JFrame frame = new JFrame("A JavaX Frame");
    frame.add(panel);
    frame.setBounds(100, 100, 600, 600);
    frame.setVisible(true);
    exitOnFrameClose(frame);
  }
  
  static boolean useImageCache = true;
  static RGBImage loadImage(String snippetID) {
   try {
    File dir = new File(System.getProperty("user.home"), ".tinybrain/image-cache");
    if (useImageCache) {
      dir.mkdirs();
      File file = new File(dir, snippetID + ".png");
      if (file.exists() && file.length() != 0)
        try {
          return new RGBImage(ImageIO.read(file));
        } catch (Throwable e) {
          e.printStackTrace();
          // fall back to loading from sourceforge
        }
    }

    String imageURL = getImageURL(parseSnippetID(snippetID));
    System.err.println("Loading image: " + imageURL);
    BufferedImage image = ImageIO.read(new URL(imageURL));

    if (useImageCache) {
      File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis());
      ImageIO.write(image, "png", tempFile);
      tempFile.renameTo(new File(dir, snippetID + ".png"));
      //Log.info("Cached image.");
    }

    //Log.info("Loaded image.");
    return new RGBImage(image);
   } catch (IOException e) {
    throw new RuntimeException(e);
   }
  }

  static String getImageURL(long snippetID) throws IOException {
    String url;
    if (snippetID == 1000010 || snippetID == 1000012)
      url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + snippetID;
    else
      url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + snippetID
        + "&contentType=image/png";
    return url;
  }

  public static long parseSnippetID(String snippetID) {
    return Long.parseLong(shortenSnippetID(snippetID));
  }

  private static String shortenSnippetID(String snippetID) {
    if (snippetID.startsWith("#"))
      snippetID = snippetID.substring(1);
    String httpBlaBla = "http://tinybrain.de/";
    if (snippetID.startsWith(httpBlaBla))
      snippetID = snippetID.substring(httpBlaBla.length());
    return snippetID;
  }
  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, qbtsjoyahagl, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #672
Snippet name: Focal points (developing)
Eternal ID of this version: #672/1
Text MD5: d98cc6c7b2700e11577fce17875a783a
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-07-16 17:08:51
Source code size: 4198 bytes / 143 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 619 / 490
Referenced in: [show references]