| 1 |
/* |
| 2 |
* Copyright (c) 2009, Takeyuki Nagao |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or |
| 6 |
* without modification, are permitted provided that the |
| 7 |
* following conditions are met: |
| 8 |
* |
| 9 |
* * Redistributions of source code must retain the above |
| 10 |
* copyright notice, this list of conditions and the |
| 11 |
* following disclaimer. |
| 12 |
* * Redistributions in binary form must reproduce the above |
| 13 |
* copyright notice, this list of conditions and the |
| 14 |
* following disclaimer in the documentation and/or other |
| 15 |
* materials provided with the distribution. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 18 |
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 19 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 29 |
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 30 |
* OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
package jp.sourceforge.dvibrowser.dvicore.render; |
| 34 |
|
| 35 |
import jp.sourceforge.dvibrowser.dvicore.DviException; |
| 36 |
import jp.sourceforge.dvibrowser.dvicore.DviRect; |
| 37 |
import jp.sourceforge.dvibrowser.dvicore.DviResolution; |
| 38 |
import jp.sourceforge.dvibrowser.dvicore.api.GammaCorrector; |
| 39 |
import jp.sourceforge.dvibrowser.dvicore.api.ImageDevice; |
| 40 |
import jp.sourceforge.dvibrowser.dvicore.gui.swing.ViewSpec; |
| 41 |
|
| 42 |
|
| 43 |
public class IntRGBImage |
| 44 |
{ |
| 45 |
private final int [] buf; |
| 46 |
private final int width; |
| 47 |
private final int height; |
| 48 |
|
| 49 |
public IntRGBImage(int width, int height) { |
| 50 |
this.buf = new int [width * height]; |
| 51 |
this.width = width; |
| 52 |
this.height = height; |
| 53 |
} |
| 54 |
|
| 55 |
public IntRGBImage(int [] buf, int width, int height) { |
| 56 |
this.buf = buf; |
| 57 |
this.width = width; |
| 58 |
this.height = height; |
| 59 |
} |
| 60 |
|
| 61 |
public int [] getBuffer() { return buf; } |
| 62 |
|
| 63 |
public int width() { return width; } |
| 64 |
public int height() { return height; } |
| 65 |
|
| 66 |
public void fill(int c) { |
| 67 |
for (int i=0; i<buf.length; i++) |
| 68 |
// buf[i] = 0xff000000 | c; |
| 69 |
buf[i] = c; |
| 70 |
} |
| 71 |
|
| 72 |
public ImageDevice getImageDevice(DviResolution res, GammaCorrector gc) { |
| 73 |
return new ImageDeviceImpl(res, gc); |
| 74 |
} |
| 75 |
public ImageDevice getImageDevice(DviResolution res) { |
| 76 |
return getImageDevice(res, null); |
| 77 |
} |
| 78 |
|
| 79 |
private class ImageDeviceImpl |
| 80 |
extends AbstractDevice |
| 81 |
implements ImageDevice |
| 82 |
{ |
| 83 |
private final GammaCorrector originalGammaCorrector; |
| 84 |
private GammaCorrector gammaCorrector; |
| 85 |
private int maxval; |
| 86 |
protected ImageDeviceImpl(DviResolution res, GammaCorrector gammaCorrector) { |
| 87 |
super(res); |
| 88 |
this.originalGammaCorrector = (gammaCorrector == null) ? ViewSpec.getDefaultGammaCorrector() : gammaCorrector; |
| 89 |
if (originalGammaCorrector == null) |
| 90 |
throw new IllegalStateException("Unable to determine the gamma corrector."); |
| 91 |
} |
| 92 |
|
| 93 |
public DviRect getBounds() { |
| 94 |
return new DviRect(-point.x, -point.y, width, height); |
| 95 |
} |
| 96 |
|
| 97 |
public void begin(int maxval) { |
| 98 |
this.maxval = maxval; |
| 99 |
gammaCorrector = GammaCorrectorCache.wrap(originalGammaCorrector); |
| 100 |
} |
| 101 |
|
| 102 |
public void end() { |
| 103 |
} |
| 104 |
|
| 105 |
private int ptr = 0; |
| 106 |
// private int gw; |
| 107 |
// private int gh; |
| 108 |
public boolean beginImage(int w, int h) |
| 109 |
throws DviException |
| 110 |
{ |
| 111 |
// gw = w; |
| 112 |
// gh = h; |
| 113 |
ptr = point.x + point.y * width; |
| 114 |
return true; |
| 115 |
} |
| 116 |
public void endImage() { |
| 117 |
} |
| 118 |
|
| 119 |
public void putLine(int [] l_buf, int off, int len) |
| 120 |
throws DviException |
| 121 |
{ |
| 122 |
final int color = getColor().toIntRGB(); |
| 123 |
for (int i=0; i<len; i++) { |
| 124 |
final int alpha10 = gammaCorrector.correctGamma(l_buf[off + i], maxval); |
| 125 |
if (alpha10 != 0) |
| 126 |
buf[ptr + i] = blend(buf[ptr + i], color, alpha10); |
| 127 |
} |
| 128 |
ptr += width; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
private static int blendARGB(int c1, int c2, final int alpha10) { |
| 133 |
int r, g, b; |
| 134 |
|
| 135 |
b = c1 & 0xff; |
| 136 |
b += (alpha10 * ((c2 & 0xff) - b)) >>> 10; |
| 137 |
b &= 0xff; |
| 138 |
c1 >>>= 8; |
| 139 |
c2 >>>= 8; |
| 140 |
|
| 141 |
g = c1 & 0xff; |
| 142 |
g += (alpha10 * ((c2 & 0xff) - g)) >>> 10; |
| 143 |
g &= 0xff; |
| 144 |
c1 >>>= 8; |
| 145 |
c2 >>>= 8; |
| 146 |
|
| 147 |
r = c1 & 0xff; |
| 148 |
r += (alpha10 * ((c2 & 0xff) - r)) >>> 10; |
| 149 |
r &= 0xff; |
| 150 |
|
| 151 |
return 0xff000000 | (r << 16) | (g << 8) | b; |
| 152 |
} |
| 153 |
|
| 154 |
private static int blend(int c1, int c2, final int alpha10) { |
| 155 |
int r, g, b; |
| 156 |
|
| 157 |
b = c1 & 0xff; |
| 158 |
b += (alpha10 * ((c2 & 0xff) - b)) >>> 10; |
| 159 |
b &= 0xff; |
| 160 |
c1 >>>= 8; |
| 161 |
c2 >>>= 8; |
| 162 |
|
| 163 |
g = c1 & 0xff; |
| 164 |
g += (alpha10 * ((c2 & 0xff) - g)) >>> 10; |
| 165 |
g &= 0xff; |
| 166 |
c1 >>>= 8; |
| 167 |
c2 >>>= 8; |
| 168 |
|
| 169 |
r = c1 & 0xff; |
| 170 |
r += (alpha10 * ((c2 & 0xff) - r)) >>> 10; |
| 171 |
r &= 0xff; |
| 172 |
|
| 173 |
return (r << 16) | (g << 8) | b; |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
//BenchMark: dvidump: 535 samples in 19.174 sec. 27.902 samples/sec. 35.839 msec./sample. |