36 * dealing with. Some conversions cannot be done automatically,
37 * however (for example, the AnyInt or AnyDCM surface types), so
38 * we require the caller to pass in a ColorModel object so that
39 * we can calculate the pixel values in these generic cases as well.
40 */
41 public class PixelConverter {
42
43 /**
44 * Default object, used as a fallback for any surface types where
45 * we do not know enough about the surface to calculate the
46 * conversions directly. We use the ColorModel object to assist
47 * us in these cases.
48 */
49 public static final PixelConverter instance = new PixelConverter();
50
51
52 protected int alphaMask = 0;
53
54 protected PixelConverter() {}
55
56 public int rgbToPixel(int rgb, ColorModel cm) {
57 Object obj = cm.getDataElements(rgb, null);
58 switch (cm.getTransferType()) {
59 case DataBuffer.TYPE_BYTE:
60 byte[] bytearr = (byte[]) obj;
61 int pix = 0;
62
63 switch(bytearr.length) {
64 default: // bytearr.length >= 4
65 pix = bytearr[3] << 24;
66 // FALLSTHROUGH
67 case 3:
68 pix |= (bytearr[2] & 0xff) << 16;
69 // FALLSTHROUGH
70 case 2:
71 pix |= (bytearr[1] & 0xff) << 8;
72 // FALLSTHROUGH
73 case 1:
74 pix |= (bytearr[0] & 0xff);
75 }
|
36 * dealing with. Some conversions cannot be done automatically,
37 * however (for example, the AnyInt or AnyDCM surface types), so
38 * we require the caller to pass in a ColorModel object so that
39 * we can calculate the pixel values in these generic cases as well.
40 */
41 public class PixelConverter {
42
43 /**
44 * Default object, used as a fallback for any surface types where
45 * we do not know enough about the surface to calculate the
46 * conversions directly. We use the ColorModel object to assist
47 * us in these cases.
48 */
49 public static final PixelConverter instance = new PixelConverter();
50
51
52 protected int alphaMask = 0;
53
54 protected PixelConverter() {}
55
56 @SuppressWarnings("fallthrough")
57 public int rgbToPixel(int rgb, ColorModel cm) {
58 Object obj = cm.getDataElements(rgb, null);
59 switch (cm.getTransferType()) {
60 case DataBuffer.TYPE_BYTE:
61 byte[] bytearr = (byte[]) obj;
62 int pix = 0;
63
64 switch(bytearr.length) {
65 default: // bytearr.length >= 4
66 pix = bytearr[3] << 24;
67 // FALLSTHROUGH
68 case 3:
69 pix |= (bytearr[2] & 0xff) << 16;
70 // FALLSTHROUGH
71 case 2:
72 pix |= (bytearr[1] & 0xff) << 8;
73 // FALLSTHROUGH
74 case 1:
75 pix |= (bytearr[0] & 0xff);
76 }
|