| 164 | |
| 165 | |
| 166 | static public void fromNativeImage(Image img, PImage out) { |
| 167 | out.format = RGB; |
| 168 | out.pixels = null; |
| 169 | |
| 170 | if (img instanceof BufferedImage) { |
| 171 | BufferedImage bi = (BufferedImage) img; |
| 172 | out.width = bi.getWidth(); |
| 173 | out.height = bi.getHeight(); |
| 174 | |
| 175 | int type = bi.getType(); |
| 176 | if (type == BufferedImage.TYPE_3BYTE_BGR || |
| 177 | type == BufferedImage.TYPE_4BYTE_ABGR) { |
| 178 | out.pixels = new int[out.width * out.height]; |
| 179 | bi.getRGB(0, 0, out.width, out.height, out.pixels, 0, out.width); |
| 180 | if (type == BufferedImage.TYPE_4BYTE_ABGR) { |
| 181 | out.format = ARGB; |
| 182 | // } else { |
| 183 | // opaque(); |
| 184 | } |
| 185 | } else { |
| 186 | DataBuffer db = bi.getRaster().getDataBuffer(); |
| 187 | if (db instanceof DataBufferInt) { |
| 188 | out.pixels = ((DataBufferInt) db).getData(); |
| 189 | if (type == BufferedImage.TYPE_INT_ARGB) { |
| 190 | out.format = ARGB; |
| 191 | // } else if (type == BufferedImage.TYPE_INT_RGB) { |
| 192 | // opaque(); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | if ((out.pixels != null) && (out.format == RGB)) { |
| 198 | for (int i = 0; i < out.pixels.length; i++) { |
| 199 | out.pixels[i] |= 0xFF000000; |
| 200 | } |
| 201 | } |
| 202 | // Implements fall-through if not DataBufferInt above, or not a |
| 203 | // known type, or not DataBufferInt for the data itself. |
| 204 | if (out.pixels == null) { // go the old school Java 1.0 route |
| 205 | out.width = img.getWidth(null); |
| 206 | out.height = img.getHeight(null); |
| 207 | out.pixels = new int[out.width * out.height]; |
| 208 | PixelGrabber pg = |
| 209 | new PixelGrabber(img, 0, 0, out.width, out.height, out.pixels, 0, out.width); |
| 210 | try { |
| 211 | pg.grabPixels(); |
| 212 | } catch (InterruptedException ignored) { } |
| 213 | } |
| 214 | out.pixelDensity = 1; |
| 215 | out.pixelWidth = out.width; |
| 216 | out.pixelHeight = out.height; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | // /** Set the high bits of all pixels to opaque. */ |