+ } finally {
+ // Uncomment these lines when turning on exception tracking in ReaderException.
+ //System.out.println("Threw " + ReaderException.getExceptionCountAndReset() + " exceptions");
+ //System.out.println("Throwers:\n" + ReaderException.getThrowersAndReset());
+ }
+ }
+
+ // Writes out a single PNG which is three times the width of the input image, containing from left
+ // to right: the original image, the row sampling monochrome version, and the 2D sampling
+ // monochrome version.
+ // TODO: Update to compare different Binarizer implementations.
+ private static void dumpBlackPoint(URI uri, BufferedImage image, BinaryBitmap bitmap) {
+ String inputName = uri.getPath();
+ if (inputName.contains(".mono.png")) {
+ return;
+ }
+
+ int width = bitmap.getWidth();
+ int height = bitmap.getHeight();
+ int stride = width * 3;
+ int[] pixels = new int[stride * height];
+
+ // The original image
+ int[] argb = new int[width];
+ for (int y = 0; y < height; y++) {
+ image.getRGB(0, y, width, 1, argb, 0, width);
+ System.arraycopy(argb, 0, pixels, y * stride, width);
+ }
+
+ // Row sampling
+ BitArray row = new BitArray(width);
+ for (int y = 0; y < height; y++) {
+ try {
+ row = bitmap.getBlackRow(y, row);
+ } catch (NotFoundException nfe) {
+ // If fetching the row failed, draw a red line and keep going.
+ int offset = y * stride + width;
+ for (int x = 0; x < width; x++) {
+ pixels[offset + x] = 0xffff0000;
+ }
+ continue;
+ }
+
+ int offset = y * stride + width;
+ for (int x = 0; x < width; x++) {
+ if (row.get(x)) {
+ pixels[offset + x] = 0xff000000;
+ } else {
+ pixels[offset + x] = 0xffffffff;
+ }
+ }
+ }
+
+ // 2D sampling
+ try {
+ for (int y = 0; y < height; y++) {
+ BitMatrix matrix = bitmap.getBlackMatrix();
+ int offset = y * stride + width * 2;
+ for (int x = 0; x < width; x++) {
+ if (matrix.get(x, y)) {
+ pixels[offset + x] = 0xff000000;
+ } else {
+ pixels[offset + x] = 0xffffffff;
+ }
+ }
+ }
+ } catch (NotFoundException nfe) {
+ }
+
+ // Write the result
+ BufferedImage result = new BufferedImage(stride, height, BufferedImage.TYPE_INT_ARGB);
+ result.setRGB(0, 0, stride, height, pixels, 0, stride);
+
+ // Use the current working directory for URLs
+ String resultName = inputName;
+ if ("http".equals(uri.getScheme())) {
+ int pos = resultName.lastIndexOf('/');
+ if (pos > 0) {
+ resultName = '.' + resultName.substring(pos);
+ }
+ }
+ int pos = resultName.lastIndexOf('.');
+ if (pos > 0) {
+ resultName = resultName.substring(0, pos);
+ }
+ resultName += ".mono.png";
+ OutputStream outStream = null;
+ try {
+ outStream = new FileOutputStream(resultName);
+ ImageIO.write(result, "png", outStream);
+ } catch (FileNotFoundException e) {
+ System.err.println("Could not create " + resultName);
+ } catch (IOException e) {
+ System.err.println("Could not write to " + resultName);
+ } finally {
+ try {
+ if (outStream != null) {
+ outStream.close();
+ }
+ } catch (IOException ioe) {
+ // continue
+ }