import java.awt.image.*; import java.awt.*; import java.io.*; class ImageProcessor extends Thread { Image image; int width; int height; Preferences prefs; int[] pixels; ImageProcessor(Image pimage, int pwidth, int pheight, Preferences pprefs) { image = pimage; width = pwidth; height = pheight; prefs = pprefs; } // ImageProcessor constructor public void run() { // Get the pixels of the image pixels = new int[width * height]; PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width); try { pixelGrabber.grabPixels(); } catch (InterruptedException e) { Image2HTML.ErrorExit("Interrupted waiting for pixels!"); return; } if ((pixelGrabber.getStatus() & ImageObserver.ABORT) != 0) { Image2HTML.ErrorExit("Image fetch aborted or errored"); return; } PrintWriter printWriter = null; try { OutputStream htmlout; if (prefs.toFile != null) htmlout = new FileOutputStream(prefs.toFile); else htmlout = System.out; printWriter = new PrintWriter(htmlout, true); } catch (Exception e) { e.printStackTrace(); } image2html(printWriter); } // run() void image2html(PrintWriter html) { if (prefs.cgi) { html.print("TABLE_DATA_BEGIN\n"); } // Write the HTML header if (prefs.fullpage) { html.print("\n"); html.print("Image2HTML Output\n"); html.print("\n"); } // int f = 0; int xc = 0; int yc = 0; // Start the table html.print("\n"); int x = width; int y = height; for (int xi = 0; xi < width; xi++) for (int yi = 0; yi < height; yi++) pixels[yi * width + xi] &= 0x00FFFFFF; // If we're going to use compression, add a dummy line // to the top of the table to keep it from collapsing any rows if (prefs.compress) { html.print(""); for (xc = 0; xc < x; xc++) html.print(""); html.print("\n"); } // if compress // Convert the image to an HTML table for (yc = 0; yc < y; yc++) { // Start this line html.print(""); // We haven't added any "); // We just made a "); // End the row html.print("\n"); } // for yc // End the table html.print("
tags yet boolean noTDs = true ; // Size of block we will fill with this TD (this will be computed) int bx = 0; int by = 0; // Scan forward to the right for (xc = 0; xc < x; xc++) { // If this pixel has not yet been generated, consider adding it to the current block if ((pixels[yc * width + xc] & 0xFF000000) == 0) { // Make sure we're doing compress (if not, all blocks are 1x1) if (prefs.compress) { // Now we scan downward, making blocks of solid color of different sizes, // with their upper left at the current pixel. Look for the one // with the largest area. int limitx = x; int maxx = x - 1; int maxy = yc; int maxArea = 0; int ry; for (ry = yc; ry < y; ry++) { // If this pixel is a different color than the block we're forming, we can't use it if (pixels[yc * width + xc] != pixels[ry * width + xc] ) break; // If this pixel is the same color as our block, we might be able to use it else { // Scan to the rightm expanding the block if we can int fellOff = limitx ; int rx; for (rx = xc + 1; rx < limitx; rx++) { // If this is a pixel of a different color, the block can't expand any // more in this direction; remember how big it got. if (pixels[yc * width + xc] != pixels[ry * width + rx]) { limitx = rx ; if (maxArea < (((ry - yc) + 1) * (rx - xc))) { maxx = rx - 1; maxy = ry; maxArea = ((ry - yc) + 1) * (rx - xc); } // if found new max break; } // if pixel of different color } // for rx // If we fell off the right of the bitmap, remember that as our maximum. if (rx == fellOff) { if (maxArea < (((ry - yc) + 1) * (rx - xc))) { maxx = fellOff - 1; maxy = ry; maxArea = ((ry - yc) + 1) * (rx - xc); } // if found new max } // if fell off right } // if pixel of same color } // for ry // If we're at the bottom, then we fell off the bottom of the bitmap; // remember that as our maximum, if it's larger than what we've seen before. if (ry == y) { if (maxArea < (((ry - yc) + 1) * (limitx - xc))) { maxy = ry - 1; maxx = limitx - 1; } // if found new max } // if fell off bottom // We've now found the largest block; remember its size, // and mark every pixel in is as "used" bx = (maxx - xc) + 1; by = (maxy - yc) + 1 ; int xcc, ycc; for (xcc = xc; xcc <= maxx; xcc++) for (ycc = yc ; ycc <= maxy ; ycc++) pixels[ycc * width + xcc] = pixels[ycc * width + xcc] | 0xFF000000; } // if RLE // No compression; block is always one pixel else { bx = 1; by = 1; } // Get the color of this block int blockColor = pixels[yc * width + xc]; // Make a tag for this block, of the appropriate color html.print(""); if (prefs.spacers) { html.print(""); } html.print(" tag, so we not have at least one in this row noTDs = false; } // if this pixel has not been processed } // for xc // If there were never any 's added, make an empty row if (noTDs) html.print("
\n"); // End the page if (prefs.fullpage) { html.print("\n"); html.print("\n"); } html.println(""); // System.out.println("Done generating image."); System.exit(0); } //==== image2html() ====// } // class ImageProcessor