comparison servlet/src/digilib/image/ImageLoaderDocuImage.java @ 863:1641fab276ee stream

fixing bugs from cleanup...
author robcast
date Wed, 09 Mar 2011 11:03:29 +0100
parents 74062b7d9b22
children 83e747b2a98f
comparison
equal deleted inserted replaced
861:74062b7d9b22 863:1641fab276ee
34 import java.awt.image.ConvolveOp; 34 import java.awt.image.ConvolveOp;
35 import java.awt.image.Kernel; 35 import java.awt.image.Kernel;
36 import java.awt.image.LookupOp; 36 import java.awt.image.LookupOp;
37 import java.awt.image.LookupTable; 37 import java.awt.image.LookupTable;
38 import java.awt.image.RescaleOp; 38 import java.awt.image.RescaleOp;
39 import java.awt.image.WritableRaster;
39 import java.io.IOException; 40 import java.io.IOException;
40 import java.io.OutputStream; 41 import java.io.OutputStream;
41 import java.io.RandomAccessFile; 42 import java.io.RandomAccessFile;
42 import java.util.Arrays; 43 import java.util.Arrays;
43 import java.util.Iterator; 44 import java.util.Iterator;
395 AffineTransformOp scaleOp = new AffineTransformOp(AffineTransform 396 AffineTransformOp scaleOp = new AffineTransformOp(AffineTransform
396 .getScaleInstance(scale, scale), renderHint); 397 .getScaleInstance(scale, scale), renderHint);
397 logger.debug("scaling..."); 398 logger.debug("scaling...");
398 img = scaleOp.filter(img, null); 399 img = scaleOp.filter(img, null);
399 logger.debug("SCALE: " + scale + " ->" + img.getWidth() + "x" 400 logger.debug("SCALE: " + scale + " ->" + img.getWidth() + "x"
400 + img.getHeight() + " type=" + img.getType()); 401 + img.getHeight() + " type=" + img);
401 } 402 }
402 403
403 public void blur(int radius) throws ImageOpException { 404 public void blur(int radius) throws ImageOpException {
404 logger.debug("blur: " + radius); 405 logger.debug("blur: " + radius);
405 // minimum radius is 2 406 // minimum radius is 2
406 int klen = Math.max(radius, 2); 407 int klen = Math.max(radius, 2);
407 Kernel blur = null; 408 Kernel blur = null;
408 if (klen < convolutionKernels.length) { 409 if (klen < convolutionKernels.length) {
410 // use precalculated Kernel
409 blur = convolutionKernels[klen]; 411 blur = convolutionKernels[klen];
410 } else { 412 } else {
411 // calculate our own kernel 413 // calculate our own kernel
412 int ksize = klen * klen; 414 int ksize = klen * klen;
413 // kernel is constant 1/k 415 // kernel is constant 1/k
419 blur = new Kernel(klen, klen, kern); 421 blur = new Kernel(klen, klen, kern);
420 } 422 }
421 // blur with convolve operation 423 // blur with convolve operation
422 ConvolveOp blurOp = new ConvolveOp(blur, ConvolveOp.EDGE_NO_OP, 424 ConvolveOp blurOp = new ConvolveOp(blur, ConvolveOp.EDGE_NO_OP,
423 renderHint); 425 renderHint);
424 /* blur needs explicit destination image type for color *Java2D BUG* 426 /* blur needs explicit destination image type for color *Java2D BUG* */
427 BufferedImage dest = null;
425 if (img.getType() == BufferedImage.TYPE_3BYTE_BGR) { 428 if (img.getType() == BufferedImage.TYPE_3BYTE_BGR) {
426 logger.debug("blur: fixing destination image type"); 429 logger.debug("blur: fixing destination image type for "+img);
427 blurredImg = new BufferedImage(img.getWidth(), img.getHeight(), img 430 dest = new BufferedImage(img.getWidth(), img.getHeight(), img
428 .getType()); 431 .getType());
429 } */ 432 }
430 img = blurOp.filter(img, null); 433 img = blurOp.filter(img, dest);
434 logger.debug("blurred: "+img);
431 } 435 }
432 436
433 public void crop(int x_off, int y_off, int width, int height) 437 public void crop(int x_off, int y_off, int width, int height)
434 throws ImageOpException { 438 throws ImageOpException {
435 // setup Crop 439 // setup Crop
469 * The number of constants must match the number of bands in the image. 473 * The number of constants must match the number of bands in the image.
470 * We do only 3 (RGB) bands. 474 * We do only 3 (RGB) bands.
471 */ 475 */
472 int ncol = img.getColorModel().getNumColorComponents(); 476 int ncol = img.getColorModel().getNumColorComponents();
473 if ((ncol != 3) || (rgbm.length != 3) || (rgba.length != 3)) { 477 if ((ncol != 3) || (rgbm.length != 3) || (rgba.length != 3)) {
474 logger.debug("ERROR(enhance): unknown number of color bands or coefficients (" 478 logger.error("enhanceRGB: unknown number of color bands or coefficients ("
475 + ncol + ")"); 479 + ncol + ")");
476 return; 480 return;
477 } 481 }
478 RescaleOp scaleOp = new RescaleOp(rgbOrdered(rgbm), rgbOrdered(rgba), 482 RescaleOp scaleOp = new RescaleOp(rgbOrdered(rgbm), rgbOrdered(rgba),
479 null); 483 null);
555 return; 559 return;
556 } 560 }
557 float[][] combineFn = new float[1][4]; 561 float[][] combineFn = new float[1][4];
558 combineFn[0] = rgbOrdered(new float[] { 0.299f, 0.114f, 0.587f, 0f }); 562 combineFn[0] = rgbOrdered(new float[] { 0.299f, 0.114f, 0.587f, 0f });
559 BandCombineOp op = new BandCombineOp(combineFn, renderHint); 563 BandCombineOp op = new BandCombineOp(combineFn, renderHint);
560 // unfortunately BandCombineOp only works on Rasters so we create a 564 // BandCombineOp only works on Rasters so we create a
561 // new image and use its Raster 565 // new image and use its Raster
562 BufferedImage dest = new BufferedImage(img.getWidth(), 566 BufferedImage dest = new BufferedImage(img.getWidth(),
563 img.getHeight(), BufferedImage.TYPE_BYTE_GRAY); 567 img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
564 op.filter(img.getRaster(), dest.getRaster()); 568 op.filter(img.getRaster(), dest.getRaster());
565 img = dest; 569 img = dest;
566 } else if (colop == ColorOp.INVERT) { 570 } else if (colop == ColorOp.INVERT) {
567 /* 571 /*
568 * invert colors i.e. invert every channel 572 * invert colors i.e. invert every channel
569 */ 573 */
570 logger.debug("Color op: inverting"); 574 logger.debug("Color op: inverting");
571 // TODO: is this enough for all image types?
572 LookupTable invtbl = null; 575 LookupTable invtbl = null;
573 ColorModel cm = img.getColorModel(); 576 ColorModel cm = img.getColorModel();
574 if (needsInvertRgba && cm.hasAlpha()) { 577 if (needsInvertRgba && cm.hasAlpha()) {
575 /* 578 /*
576 * should work with one array for all channels, but JDK 1.6 in 579 * should work with one array for all channels, but JDK 1.6 in
622 double yoff = rotbounds.getY(); 625 double yoff = rotbounds.getY();
623 // move image back in line 626 // move image back in line
624 trafo.preConcatenate(AffineTransform.getTranslateInstance(-xoff, -yoff)); 627 trafo.preConcatenate(AffineTransform.getTranslateInstance(-xoff, -yoff));
625 // transform image 628 // transform image
626 rotOp = new AffineTransformOp(trafo, renderHint); 629 rotOp = new AffineTransformOp(trafo, renderHint);
627 rotOp.filter(img, img); 630 img = rotOp.filter(img, null);
628 // calculate new bounding box 631 // calculate new bounding box
629 // Rectangle2D bounds = rotOp.getBounds2D(img); 632 // Rectangle2D bounds = rotOp.getBounds2D(img);
630 // crop new image (with self-made rounding) 633 // crop new image (with self-made rounding)
631 /* 634 /*
632 * img = rotImg.getSubimage( (int) (bounds.getX()+0.5), (int) 635 * img = rotImg.getSubimage( (int) (bounds.getX()+0.5), (int)
657 mx = -1; 660 mx = -1;
658 tx = getWidth(); 661 tx = getWidth();
659 } 662 }
660 AffineTransformOp mirOp = new AffineTransformOp(new AffineTransform(mx, 663 AffineTransformOp mirOp = new AffineTransformOp(new AffineTransform(mx,
661 0, 0, my, tx, ty), renderHint); 664 0, 0, my, tx, ty), renderHint);
662 mirOp.filter(img, img); 665 img = mirOp.filter(img, null);
663 } 666 }
664 667
665 public void dispose() { 668 public void dispose() {
666 // is this necessary? 669 // is this necessary?
667 img = null; 670 img = null;