/** Loads the resources */ void initResources() { final Class<ControlExample> clazz = ControlExample.class; if (resourceBundle != null) { try { if (images == null) { images = new Image[imageLocations.length]; for (int i = 0; i < imageLocations.length; ++i) { InputStream sourceStream = clazz.getResourceAsStream(imageLocations[i]); ImageData source = new ImageData(sourceStream); if (imageTypes[i] == SWT.ICON) { ImageData mask = source.getTransparencyMask(); images[i] = new Image(null, source, mask); } else { images[i] = new Image(null, source); } try { sourceStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return; } catch (Throwable t) { } } String error = (resourceBundle != null) ? getResourceString("error.CouldNotLoadResources") : "Unable to load resources"; //$NON-NLS-1$ freeResources(); throw new RuntimeException(error); }
@Override ImageData[] loadFromByteStream() { int[] fileHeader = loadFileHeader(); byte[] infoHeader = new byte[BMPHeaderFixedSize]; try { inputStream.read(infoHeader); } catch (Exception e) { SWT.error(SWT.ERROR_IO, e); } int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); if (height < 0) height = -height; int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); this.compression = (infoHeader[16] & 0xFF) | ((infoHeader[17] & 0xFF) << 8) | ((infoHeader[18] & 0xFF) << 16) | ((infoHeader[19] & 0xFF) << 24); PaletteData palette = loadPalette(infoHeader); if (inputStream.getPosition() < fileHeader[4]) { // Seek to the specified offset try { inputStream.skip(fileHeader[4] - inputStream.getPosition()); } catch (IOException e) { SWT.error(SWT.ERROR_IO, e); } } byte[] data = loadData(infoHeader); this.importantColors = (infoHeader[36] & 0xFF) | ((infoHeader[37] & 0xFF) << 8) | ((infoHeader[38] & 0xFF) << 16) | ((infoHeader[39] & 0xFF) << 24); int xPelsPerMeter = (infoHeader[24] & 0xFF) | ((infoHeader[25] & 0xFF) << 8) | ((infoHeader[26] & 0xFF) << 16) | ((infoHeader[27] & 0xFF) << 24); int yPelsPerMeter = (infoHeader[28] & 0xFF) | ((infoHeader[29] & 0xFF) << 8) | ((infoHeader[30] & 0xFF) << 16) | ((infoHeader[31] & 0xFF) << 24); this.pelsPerMeter = new Point(xPelsPerMeter, yPelsPerMeter); int type = (this.compression == 1 /*BMP_RLE8_COMPRESSION*/) || (this.compression == 2 /*BMP_RLE4_COMPRESSION*/) ? SWT.IMAGE_BMP_RLE : SWT.IMAGE_BMP; return new ImageData[] { ImageData.internal_new( width, height, bitCount, palette, 4, data, 0, null, null, -1, -1, type, 0, 0, 0, 0) }; }
/** * Return a DeviceIndependentImage representing the image block at the current position in the * input stream. Throw an error if an error occurs. */ ImageData readImageBlock(PaletteData defaultPalette) { int depth; PaletteData palette; byte[] block = new byte[9]; try { inputStream.read(block); } catch (IOException e) { SWT.error(SWT.ERROR_IO, e); } int left = (block[0] & 0xFF) | ((block[1] & 0xFF) << 8); int top = (block[2] & 0xFF) | ((block[3] & 0xFF) << 8); int width = (block[4] & 0xFF) | ((block[5] & 0xFF) << 8); int height = (block[6] & 0xFF) | ((block[7] & 0xFF) << 8); byte bitField = block[8]; boolean interlaced = (bitField & 0x40) != 0; // boolean sorted = (bitField & 0x20) != 0; if ((bitField & 0x80) != 0) { // Local palette. depth = (bitField & 0x7) + 1; palette = readPalette(1 << depth); } else { // No local palette. depth = defaultDepth; palette = defaultPalette; } /* Work around: Ignore the case where a GIF specifies an * invalid index for the transparent pixel that is larger * than the number of entries in the palette. */ if (transparentPixel > 1 << depth) { transparentPixel = -1; } // Promote depth to next highest supported value. if (!(depth == 1 || depth == 4 || depth == 8)) { if (depth < 4) depth = 4; else depth = 8; } if (palette == null) { palette = grayRamp(1 << depth); } int initialCodeSize = -1; try { initialCodeSize = inputStream.read(); } catch (IOException e) { SWT.error(SWT.ERROR_IO, e); } if (initialCodeSize < 0) { SWT.error(SWT.ERROR_INVALID_IMAGE); } ImageData image = ImageData.internal_new( width, height, depth, palette, 4, null, 0, null, null, -1, transparentPixel, SWT.IMAGE_GIF, left, top, disposalMethod, delayTime); LZWCodec codec = new LZWCodec(); codec.decode(inputStream, loader, image, interlaced, initialCodeSize); return image; }