~josejuan-sanchez/+junk/original-jhv-experimental-version

« back to all changes in this revision

Viewing changes to src/viewmodel/src/org/helioviewer/viewmodel/imagedata/SingleChannelShortImageData.java

  • Committer: José Juan Sánchez Hernández
  • Date: 2013-02-05 13:32:08 UTC
  • Revision ID: josejuan.sanchez@gmail.com-20130205133208-dfz1sh1uge5pjkny
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.helioviewer.viewmodel.imagedata;
 
2
 
 
3
import java.awt.Point;
 
4
import java.awt.image.BufferedImage;
 
5
import java.awt.image.DataBufferUShort;
 
6
import java.awt.image.Raster;
 
7
 
 
8
import org.helioviewer.viewmodel.imageformat.ImageFormat;
 
9
import org.helioviewer.viewmodel.imageformat.SingleChannelImageFormat;
 
10
import org.helioviewer.viewmodel.imagetransport.ImageTransport;
 
11
import org.helioviewer.viewmodel.imagetransport.Short16ImageTransport;
 
12
 
 
13
/**
 
14
 * Representation of image data in single channel format, using 9 to 16 bits per
 
15
 * pixel.
 
16
 * 
 
17
 * <p>
 
18
 * Note that this is the only implementation of ImageData, which is supposed to
 
19
 * handle a variable number of bits per pixel.
 
20
 * 
 
21
 * @author Ludwig Schmidt
 
22
 * @author Markus Langenberg
 
23
 * 
 
24
 */
 
25
public class SingleChannelShortImageData extends AbstractImageData {
 
26
 
 
27
    private SingleChannelImageFormat format;
 
28
    private Short16ImageTransport imageTransport;
 
29
 
 
30
    /**
 
31
     * Constructor, given an array as data source.
 
32
     * 
 
33
     * <p>
 
34
     * This constructor receives the raw data as a data source. If the caller
 
35
     * handles raw data as well, the use of this constructor is recommended.
 
36
     * <p>
 
37
     * The pixel data has to be given as a one-dimensional array containing the
 
38
     * pixel data line by line. Each array element represents one pixel.
 
39
     * 
 
40
     * @param newWidth
 
41
     *            width of the image
 
42
     * @param newHeight
 
43
     *            height of the image
 
44
     * @param newBitDepth
 
45
     *            number of bits per pixel
 
46
     * @param newPixelData
 
47
     *            pixel data
 
48
     * @param newColorMask
 
49
     *            color mask of the image
 
50
     */
 
51
    public SingleChannelShortImageData(int newWidth, int newHeight, int newBitDepth, short[] newPixelData, ColorMask newColorMask) {
 
52
        super(newWidth, newHeight, newColorMask);
 
53
        imageTransport = new Short16ImageTransport(newPixelData);
 
54
        format = new SingleChannelImageFormat(newBitDepth);
 
55
    }
 
56
 
 
57
    /**
 
58
     * Constructor, given an array as data source.
 
59
     * 
 
60
     * <p>
 
61
     * This constructor receives the raw data as a data source. If the caller
 
62
     * handles raw data as well, the use of this constructor is recommended.
 
63
     * <p>
 
64
     * The pixel data has to be given as a one-dimensional array containing the
 
65
     * pixel data line by line. Each array element represents one pixel.
 
66
     * 
 
67
     * @param base
 
68
     *            original ImageData-object
 
69
     * @param newPixelData
 
70
     *            pixel data
 
71
     */
 
72
    public SingleChannelShortImageData(ImageData base, short[] newPixelData) {
 
73
        super(base);
 
74
        imageTransport = new Short16ImageTransport(newPixelData);
 
75
        format = (SingleChannelImageFormat) base.getImageFormat();
 
76
    }
 
77
 
 
78
    /**
 
79
     * Constructor, given an BufferedImage as data source.
 
80
     * 
 
81
     * <p>
 
82
     * This constructor receives a BufferedImage as data source. If the caller
 
83
     * operates on BufferedImages as well, the use of this constructor is
 
84
     * recommended.
 
85
     * 
 
86
     * @param newBitDepth
 
87
     *            number of bits per pixel
 
88
     * @param newImage
 
89
     *            pixel data
 
90
     * @param newColorMask
 
91
     *            color mask of the image
 
92
     */
 
93
    public SingleChannelShortImageData(int newBitDepth, BufferedImage newImage, ColorMask newColorMask) {
 
94
        super(newImage.getWidth(), newImage.getHeight(), newColorMask);
 
95
        image = newImage;
 
96
        imageTransport = new Short16ImageTransport(((DataBufferUShort) newImage.getRaster().getDataBuffer()).getData());
 
97
        format = new SingleChannelImageFormat(newBitDepth);
 
98
    }
 
99
 
 
100
    /**
 
101
     * Constructor, given an BufferedImage as data source.
 
102
     * 
 
103
     * <p>
 
104
     * This constructor receives a BufferedImage as data source. If the caller
 
105
     * operates on BufferedImages as well, the use of this constructor is
 
106
     * recommended.
 
107
     * 
 
108
     * @param base
 
109
     *            original ImageData-object
 
110
     * @param newImage
 
111
     *            pixel data
 
112
     */
 
113
    public SingleChannelShortImageData(ImageData base, BufferedImage newImage) {
 
114
        super(base);
 
115
        image = newImage;
 
116
        imageTransport = new Short16ImageTransport(((DataBufferUShort) newImage.getRaster().getDataBuffer()).getData());
 
117
        format = (SingleChannelImageFormat) base.getImageFormat();
 
118
    }
 
119
 
 
120
    /**
 
121
     * {@inheritDoc}
 
122
     */
 
123
    public ImageFormat getImageFormat() {
 
124
        return format;
 
125
    }
 
126
 
 
127
    /**
 
128
     * {@inheritDoc}
 
129
     */
 
130
    public ImageTransport getImageTransport() {
 
131
        return imageTransport;
 
132
    }
 
133
 
 
134
    /**
 
135
     * {@inheritDoc}
 
136
     */
 
137
    protected BufferedImage createBufferedImageFromImageTransport() {
 
138
        BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
 
139
        DataBufferUShort dataBuffer = new DataBufferUShort(imageTransport.getShort16PixelData(), width * height);
 
140
 
 
141
        // create the appropriate bit mask
 
142
        int mask = 0xffffffff;
 
143
        mask = mask >>> (32 - format.getBitDepth());
 
144
 
 
145
        Raster raster = Raster.createPackedRaster(dataBuffer, width, height, width, new int[] { mask }, new Point(0, 0));
 
146
        newImage.setData(raster);
 
147
        return newImage;
 
148
    }
 
149
}