| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2019 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
* Christoph Läubrich - refactor to use a more consistent API that allows error propagation to the caller |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.pde.internal.swt.tools; |
| 16 |
|
| 17 |
import java.io.*; |
| 18 |
import java.text.MessageFormat; |
| 19 |
import java.util.*; |
| 20 |
import java.util.function.Consumer; |
| 21 |
|
| 22 |
/** |
| 23 |
* Customize the icon of a Windows exe |
| 24 |
* |
| 25 |
* WARNING! This class is not part of SWT API. It is NOT API. It is an internal |
| 26 |
* tool that may be changed or removed at anytime. |
| 27 |
* |
| 28 |
* Based on MSDN "An In-Depth Look into the Win32 Portable Executable File Format". |
| 29 |
* |
| 30 |
* Win x64 support (Bug #238001) based on MSDN "x64 Primer: Everything You Need To |
| 31 |
* Know To Start Programming 64-Bit Windows Systems". |
| 32 |
*/ |
| 33 |
public class IconExe { |
| 34 |
|
| 35 |
/** |
| 36 |
* Replace the Desktop icons provided in the Windows executable program |
| 37 |
* with matching icons provided by the user. |
| 38 |
* |
| 39 |
* Takes 2 arguments |
| 40 |
* argument 0: the Windows executable e.g c:/eclipse/eclipse.exe |
| 41 |
* argument 1: The .ico file to write to the given executable e.g. c:/myApp.ico |
| 42 |
* |
| 43 |
* Note 1. Write access to the executable program is required. As a result, that |
| 44 |
* program must not be currently running or edited elsewhere. |
| 45 |
* |
| 46 |
* Note 2. The Eclipse 3.4 launcher requires an .ico file with the following 7 images (in any order). |
| 47 |
* 1. 48x48, 32 bit (RGB / Alpha Channel) |
| 48 |
* 2. 32x32, 32 bit (RGB / Alpha Channel) |
| 49 |
* 3. 16x16, 32 bit (RGB / Alpha Channel) |
| 50 |
* 4. 48x48, 8 bit (256 colors) |
| 51 |
* 5. 32x32, 8 bit (256 colors) |
| 52 |
* 6. 24x24, 8 bit (256 colors) |
| 53 |
* 7. 16x16, 8 bit (256 colors) |
| 54 |
* |
| 55 |
* Note 3. The Eclipse 4.2 launcher requires an .ico file with the following 7 images (in any order). |
| 56 |
* 1. 256x256, 32 bit (RGB / Alpha Channel) |
| 57 |
* 2. 48x48, 32 bit (RGB / Alpha Channel) |
| 58 |
* 3. 32x32, 32 bit (RGB / Alpha Channel) |
| 59 |
* 4. 16x16, 32 bit (RGB / Alpha Channel) |
| 60 |
* 5. 48x48, 8 bit (256 colors) |
| 61 |
* 6. 32x32, 8 bit (256 colors) |
| 62 |
* 7. 16x16, 8 bit (256 colors) |
| 63 |
* |
| 64 |
* A user icon matching exactly the width/height/depth of an executable icon will be written |
| 65 |
* to the executable and will replace that executable icon. If an executable icon |
| 66 |
* does not match a user icon, it is silently left as is. |
| 67 |
* |
| 68 |
* Note 4. This function modifies the content of the executable program and may cause |
| 69 |
* its corruption. |
| 70 |
*/ |
| 71 |
public static void main(String[] args) throws Exception { |
| 72 |
if (args.length < 2) { |
| 73 |
System.err.println("Usage: IconExe <windows executable> <ico file>"); //$NON-NLS-1$ |
| 74 |
return; |
| 75 |
} |
| 76 |
File[] iconFiles = new File[args.length - 1]; |
| 77 |
for (int i = 0; i < iconFiles.length; i++) { |
| 78 |
iconFiles[i] = new File(args[i + 1]); |
| 79 |
|
| 80 |
} |
| 81 |
ImageData[] data = loadImages(iconFiles, System.err::println); |
| 82 |
|
| 83 |
Collection<IconResInfo> unchangedIcons = replaceIcons(new File(args[0]), data, System.err::println); |
| 84 |
if (unchangedIcons.size() != 0) { |
| 85 |
System.err.println(String.format("[IconExe] Error - %d original icon(s) not replaced in %s:", unchangedIcons.size(), args[0])); //$NON-NLS-1$ |
| 86 |
for (IconResInfo icon : unchangedIcons) { |
| 87 |
ImageData iconData = icon.data; |
| 88 |
System.err.println(String.format("\t- %dx%d, %d bits, %d byte(s) @ %d", iconData.height, iconData.width, iconData.depth, icon.size, icon.offset)); //$NON-NLS-1$ |
| 89 |
} |
| 90 |
System.err.println("[IconExe] For more details, see https://git.eclipse.org/c/equinox/rt.equinox.p2.git/tree/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/pde/internal/swt/tools/IconExe.java"); //$NON-NLS-1$ |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Loads images from files so they are prepared for used in replacing icons in a |
| 96 |
* launcher executable |
| 97 |
* |
| 98 |
* @param icons the icons to loader |
| 99 |
* @param errorMessageConsumer a consumer that is informed about skipable errors |
| 100 |
* while loading icons |
| 101 |
* @return an array of image data that could be used to replace launcher icons |
| 102 |
* @throws IOException if reading of files fails on the I/O level |
| 103 |
*/ |
| 104 |
public static ImageData[] loadImages(File[] icons, Consumer<String> errorMessageConsumer) throws IOException { |
| 105 |
ImageLoader loader = new ImageLoader(); |
| 106 |
List<ImageData> images = new ArrayList<>(); |
| 107 |
for (File file : icons) { |
| 108 |
if (!file.exists()) { |
| 109 |
errorMessageConsumer.accept(MessageFormat.format("The specified icon file {0} does not exits", file)); //$NON-NLS-1$ |
| 110 |
continue; |
| 111 |
} |
| 112 |
try (FileInputStream stream = new FileInputStream(file)) { |
| 113 |
try { |
| 114 |
ImageData[] imageDatas = loader.load(stream); |
| 115 |
images.addAll(Arrays.asList(imageDatas)); |
| 116 |
} catch (RuntimeException e) { |
| 117 |
errorMessageConsumer |
| 118 |
.accept(MessageFormat.format("Loading of image {0} failed: {1}", file, e.getMessage())); //$NON-NLS-1$ |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
return images.toArray(new ImageData[0]); |
| 123 |
} |
| 124 |
|
| 125 |
/* Implementation */ |
| 126 |
|
| 127 |
/** |
| 128 |
* Replace the Desktop icons provided in the Windows executable program |
| 129 |
* with icons provided by the user. |
| 130 |
* |
| 131 |
* Note 1. Write access to the executable program is required. As a result, that |
| 132 |
* program must not be currently running or edited elsewhere. |
| 133 |
* |
| 134 |
* Note 2. Use loadIcons to determine which set of icons (width, height, depth) |
| 135 |
* is required to replace the icons in the executable program. A user icon |
| 136 |
* matching exactly the width/height/depth of an executable icon will be written |
| 137 |
* to the executable and will replace that executable icon. If an executable icon |
| 138 |
* does not match a user icon, it is left as is. Verify the return value matches |
| 139 |
* the number of icons to write. Finally, use loadIcons after this operation |
| 140 |
* to verify the icons have changed as expected. |
| 141 |
* |
| 142 |
* Note 3. The Eclipse 3.4 launcher requires the following 7 images (in any order). |
| 143 |
* 1. 48x48, 32 bit (RGB / Alpha Channel) |
| 144 |
* 2. 32x32, 32 bit (RGB / Alpha Channel) |
| 145 |
* 3. 16x16, 32 bit (RGB / Alpha Channel) |
| 146 |
* 4. 48x48, 8 bit (256 colors) |
| 147 |
* 5. 32x32, 8 bit (256 colors) |
| 148 |
* 6. 24x24, 8 bit (256 colors) |
| 149 |
* 7. 16x16, 8 bit (256 colors) |
| 150 |
* |
| 151 |
* Note 4. The Eclipse 4.2 launcher requires the following 7 images (in any order). |
| 152 |
* 1. 256x256, 32 bit (RGB / Alpha Channel) |
| 153 |
* 2. 48x48, 32 bit (RGB / Alpha Channel) |
| 154 |
* 3. 32x32, 32 bit (RGB / Alpha Channel) |
| 155 |
* 4. 16x16, 32 bit (RGB / Alpha Channel) |
| 156 |
* 5. 48x48, 8 bit (256 colors) |
| 157 |
* 6. 32x32, 8 bit (256 colors) |
| 158 |
* 7. 16x16, 8 bit (256 colors) |
| 159 |
* |
| 160 |
* Note 4. This function modifies the content of the executable program and may cause |
| 161 |
* its corruption. |
| 162 |
* |
| 163 |
* @param launcherFile the Windows executable e.g c:/eclipse/eclipse.exe |
| 164 |
* @param icons to write to the given executable |
| 165 |
* @return the list of icons from the original program that were not successfully replaced (empty if success) |
| 166 |
*/ |
| 167 |
public static Collection<IconResInfo> replaceIcons(File launcherFile, ImageData[] icons, |
| 168 |
Consumer<String> errorMessageConsumer) |
| 169 |
throws FileNotFoundException, IOException { |
| 170 |
Collection<IconResInfo> iconInfo; |
| 171 |
try (RandomAccessFile raf = new RandomAccessFile(launcherFile, "rw") //$NON-NLS-1$ |
| 172 |
) { |
| 173 |
iconInfo = getIcons(raf); |
| 174 |
// Display an error if no icons found in target executable. |
| 175 |
if (iconInfo.isEmpty()) { |
| 176 |
errorMessageConsumer |
| 177 |
.accept(MessageFormat.format("no icons detected in {0}.", launcherFile.getAbsolutePath())); //$NON-NLS-1$ |
| 178 |
raf.close(); |
| 179 |
return Collections.emptyList(); |
| 180 |
} |
| 181 |
Iterator<IconResInfo> originalIconsIterator = iconInfo.iterator(); |
| 182 |
while (originalIconsIterator.hasNext()) { |
| 183 |
IconResInfo iconToReplace = originalIconsIterator.next(); |
| 184 |
for (ImageData iconToWrite : Arrays.asList(icons)) { |
| 185 |
if (iconToWrite == null) |
| 186 |
continue; |
| 187 |
|
| 188 |
if (iconToReplace.data.width == iconToWrite.width && iconToReplace.data.height == iconToWrite.height && iconToReplace.data.depth == iconToWrite.depth) { |
| 189 |
raf.seek(iconToReplace.offset); |
| 190 |
unloadIcon(raf, iconToWrite); |
| 191 |
originalIconsIterator.remove(); |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
return iconInfo; |
| 198 |
} |
| 199 |
|
| 200 |
public static final String VERSION = "20050124"; //$NON-NLS-1$ |
| 201 |
|
| 202 |
static final boolean DEBUG = false; |
| 203 |
|
| 204 |
public static class IconResInfo { |
| 205 |
ImageData data; |
| 206 |
int offset; |
| 207 |
int size; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Retrieve the Desktop icons provided in the Windows executable program. |
| 212 |
* These icons are typically shown in various places of the Windows desktop. |
| 213 |
* |
| 214 |
* Note. The Eclipse 3.4 launcher returns the following 7 images (in any order). |
| 215 |
* 1. 48x48, 32 bit (RGB / Alpha Channel) |
| 216 |
* 2. 32x32, 32 bit (RGB / Alpha Channel) |
| 217 |
* 3. 16x16, 32 bit (RGB / Alpha Channel) |
| 218 |
* 4. 48x48, 8 bit (256 colors) |
| 219 |
* 5. 32x32, 8 bit (256 colors) |
| 220 |
* 6. 24x24, 8 bit (256 colors) |
| 221 |
* 7. 16x16, 8 bit (256 colors) |
| 222 |
* |
| 223 |
* Note 1. The Eclipse 4.2 launcher requires an .ico file with the following 7 images (in any order). |
| 224 |
* 1. 256x256, 32 bit (RGB / Alpha Channel) |
| 225 |
* 2. 48x48, 32 bit (RGB / Alpha Channel) |
| 226 |
* 3. 32x32, 32 bit (RGB / Alpha Channel) |
| 227 |
* 4. 16x16, 32 bit (RGB / Alpha Channel) |
| 228 |
* 5. 48x48, 8 bit (256 colors) |
| 229 |
* 6. 32x32, 8 bit (256 colors) |
| 230 |
* 7. 16x16, 8 bit (256 colors) |
| 231 |
* |
| 232 |
* @param raf RandomAccessFile for the the Windows executable e.g c:/eclipse/eclipse.exe |
| 233 |
*/ |
| 234 |
static Collection<IconResInfo> getIcons(RandomAccessFile raf) throws IOException { |
| 235 |
|
| 236 |
IMAGE_DOS_HEADER imageDosHeader = new IMAGE_DOS_HEADER(); |
| 237 |
read(raf, imageDosHeader); |
| 238 |
if (imageDosHeader.e_magic != IMAGE_DOS_SIGNATURE) |
| 239 |
return Collections.emptyList(); |
| 240 |
int imageNtHeadersOffset = imageDosHeader.e_lfanew; |
| 241 |
raf.seek(imageNtHeadersOffset); |
| 242 |
IMAGE_NT_HEADERS imageNtHeaders = new IMAGE_NT_HEADERS(); |
| 243 |
read(raf, imageNtHeaders); |
| 244 |
if (imageNtHeaders.Signature != IMAGE_NT_SIGNATURE) |
| 245 |
return Collections.emptyList(); |
| 246 |
// DumpResources |
| 247 |
int resourcesRVA = imageNtHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress; |
| 248 |
if (resourcesRVA == 0) |
| 249 |
return Collections.emptyList(); |
| 250 |
if (DEBUG) |
| 251 |
System.out.println("* Resources (RVA= " + resourcesRVA + ")"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 252 |
IMAGE_SECTION_HEADER imageSectionHeader = new IMAGE_SECTION_HEADER(); |
| 253 |
int firstSectionOffset = imageNtHeadersOffset + IMAGE_NT_HEADERS.FIELD_OFFSET_OptionalHeader + imageNtHeaders.FileHeader.SizeOfOptionalHeader; |
| 254 |
raf.seek(firstSectionOffset); |
| 255 |
boolean found = false; |
| 256 |
for (int i = 0; i < imageNtHeaders.FileHeader.NumberOfSections; i++) { |
| 257 |
read(raf, imageSectionHeader); |
| 258 |
if (resourcesRVA >= imageSectionHeader.VirtualAddress && resourcesRVA < imageSectionHeader.VirtualAddress + imageSectionHeader.Misc_VirtualSize) { |
| 259 |
// could check the imageSectionHeader name is .rsrc |
| 260 |
found = true; |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
if (!found) |
| 265 |
return Collections.emptyList(); |
| 266 |
int delta = imageSectionHeader.VirtualAddress - imageSectionHeader.PointerToRawData; |
| 267 |
int imageResourceDirectoryOffset = resourcesRVA - delta; |
| 268 |
return dumpResourceDirectory(raf, imageResourceDirectoryOffset, imageResourceDirectoryOffset, delta, 0, 0, |
| 269 |
false); |
| 270 |
} |
| 271 |
|
| 272 |
static Collection<IconResInfo> dumpResourceDirectory(RandomAccessFile raf, int imageResourceDirectoryOffset, |
| 273 |
int resourceBase, int delta, int type, int level, boolean rt_icon_root) throws IOException { |
| 274 |
Collection<IconResInfo> iconInfo = new ArrayList<>(); |
| 275 |
if (DEBUG) |
| 276 |
System.out.println("** LEVEL " + level); //$NON-NLS-1$ |
| 277 |
|
| 278 |
IMAGE_RESOURCE_DIRECTORY imageResourceDirectory = new IMAGE_RESOURCE_DIRECTORY(); |
| 279 |
raf.seek(imageResourceDirectoryOffset); |
| 280 |
read(raf, imageResourceDirectory); |
| 281 |
|
| 282 |
if (DEBUG) { |
| 283 |
String sType = "" + type; //$NON-NLS-1$ |
| 284 |
// level 1 resources are resource types |
| 285 |
if (level == 1) { |
| 286 |
System.out.println("___________________________"); //$NON-NLS-1$ |
| 287 |
if (type == RT_ICON) |
| 288 |
sType = "RT_ICON"; //$NON-NLS-1$ |
| 289 |
if (type == RT_GROUP_ICON) |
| 290 |
sType = "RT_GROUP_ICON"; //$NON-NLS-1$ |
| 291 |
} |
| 292 |
System.out.println("Resource Directory [" + sType + "]" + " (Named " + imageResourceDirectory.NumberOfNamedEntries + ", ID " + imageResourceDirectory.NumberOfIdEntries + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ |
| 293 |
} |
| 294 |
|
| 295 |
IMAGE_RESOURCE_DIRECTORY_ENTRY[] imageResourceDirectoryEntries = new IMAGE_RESOURCE_DIRECTORY_ENTRY[imageResourceDirectory.NumberOfIdEntries]; |
| 296 |
for (int i = 0; i < imageResourceDirectoryEntries.length; i++) { |
| 297 |
imageResourceDirectoryEntries[i] = new IMAGE_RESOURCE_DIRECTORY_ENTRY(); |
| 298 |
read(raf, imageResourceDirectoryEntries[i]); |
| 299 |
} |
| 300 |
for (IMAGE_RESOURCE_DIRECTORY_ENTRY imageResourceDirectoryEntry : imageResourceDirectoryEntries) { |
| 301 |
if (imageResourceDirectoryEntry.DataIsDirectory) { |
| 302 |
iconInfo.addAll(dumpResourceDirectory(raf, imageResourceDirectoryEntry.OffsetToDirectory + resourceBase, |
| 303 |
resourceBase, delta, imageResourceDirectoryEntry.Id, level + 1, |
| 304 |
rt_icon_root ? true : type == RT_ICON)); |
| 305 |
} else { |
| 306 |
// Resource found |
| 307 |
/// pResDirEntry->Name |
| 308 |
IMAGE_RESOURCE_DIRECTORY_ENTRY irde = imageResourceDirectoryEntry; |
| 309 |
IMAGE_RESOURCE_DATA_ENTRY data = new IMAGE_RESOURCE_DATA_ENTRY(); |
| 310 |
raf.seek(imageResourceDirectoryEntry.OffsetToData + resourceBase); |
| 311 |
read(raf, data); |
| 312 |
if (DEBUG) |
| 313 |
System.out.println("Resource Id " + irde.Id + " Data Offset RVA " + data.OffsetToData + ", Size " + data.Size); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 314 |
if (rt_icon_root) { |
| 315 |
IconResInfo resInfo = new IconResInfo(); |
| 316 |
resInfo.data = parseIcon(raf, data.OffsetToData - delta, data.Size); |
| 317 |
resInfo.offset = data.OffsetToData - delta; |
| 318 |
resInfo.size = data.Size; |
| 319 |
iconInfo.add(resInfo); |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
return iconInfo; |
| 324 |
} |
| 325 |
|
| 326 |
static ImageData parseIcon(RandomAccessFile raf, int offset, int size) throws IOException { |
| 327 |
raf.seek(offset); |
| 328 |
BITMAPINFO bitmapInfo = new BITMAPINFO(); |
| 329 |
read(raf, bitmapInfo); |
| 330 |
bitmapInfo.bmiHeader.biHeight /= 2; |
| 331 |
int width = bitmapInfo.bmiHeader.biWidth; |
| 332 |
int height = bitmapInfo.bmiHeader.biHeight; |
| 333 |
int depth = bitmapInfo.bmiHeader.biBitCount; |
| 334 |
|
| 335 |
PaletteData palette = loadPalette(bitmapInfo.bmiHeader, raf); |
| 336 |
byte[] shapeData = loadData(bitmapInfo.bmiHeader, raf); |
| 337 |
bitmapInfo.bmiHeader.biBitCount = 1; |
| 338 |
byte[] maskData = loadData(bitmapInfo.bmiHeader, raf); |
| 339 |
maskData = convertPad(maskData, width, height, 1, 4, 2); |
| 340 |
bitInvertData(maskData, 0, maskData.length); |
| 341 |
return ImageData.internal_new(width, height, depth, palette, 4, shapeData, 2, maskData, null, -1, -1, SWT.IMAGE_ICO, 0, 0, 0, 0); |
| 342 |
} |
| 343 |
|
| 344 |
static byte[] bitInvertData(byte[] data, int startIndex, int endIndex) { |
| 345 |
// Destructively bit invert data in the given byte array. |
| 346 |
for (int i = startIndex; i < endIndex; i++) { |
| 347 |
data[i] = (byte) (255 - data[i - startIndex]); |
| 348 |
} |
| 349 |
return data; |
| 350 |
} |
| 351 |
|
| 352 |
static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) { |
| 353 |
if (pad == newPad) |
| 354 |
return data; |
| 355 |
int stride = (width * depth + 7) / 8; |
| 356 |
int bpl = (stride + (pad - 1)) / pad * pad; |
| 357 |
int newBpl = (stride + (newPad - 1)) / newPad * newPad; |
| 358 |
byte[] newData = new byte[height * newBpl]; |
| 359 |
int srcIndex = 0, destIndex = 0; |
| 360 |
for (int y = 0; y < height; y++) { |
| 361 |
System.arraycopy(data, srcIndex, newData, destIndex, newBpl); |
| 362 |
srcIndex += bpl; |
| 363 |
destIndex += newBpl; |
| 364 |
} |
| 365 |
return newData; |
| 366 |
} |
| 367 |
|
| 368 |
static PaletteData loadPalette(BITMAPINFOHEADER bih, RandomAccessFile raf) throws IOException { |
| 369 |
int depth = bih.biBitCount; |
| 370 |
if (depth <= 8) { |
| 371 |
int numColors = bih.biClrUsed; |
| 372 |
if (numColors == 0) { |
| 373 |
numColors = 1 << depth; |
| 374 |
} else { |
| 375 |
if (numColors > 256) |
| 376 |
numColors = 256; |
| 377 |
} |
| 378 |
byte[] buf = new byte[numColors * 4]; |
| 379 |
raf.read(buf); |
| 380 |
return paletteFromBytes(buf, numColors); |
| 381 |
} |
| 382 |
if (depth == 16) |
| 383 |
return new PaletteData(0x7C00, 0x3E0, 0x1F); |
| 384 |
if (depth == 24) |
| 385 |
return new PaletteData(0xFF, 0xFF00, 0xFF0000); |
| 386 |
return new PaletteData(0xFF00, 0xFF0000, 0xFF000000); |
| 387 |
} |
| 388 |
|
| 389 |
static PaletteData paletteFromBytes(byte[] bytes, int numColors) { |
| 390 |
int bytesOffset = 0; |
| 391 |
RGB[] colors = new RGB[numColors]; |
| 392 |
for (int i = 0; i < numColors; i++) { |
| 393 |
colors[i] = new RGB(bytes[bytesOffset + 2] & 0xFF, bytes[bytesOffset + 1] & 0xFF, bytes[bytesOffset] & 0xFF); |
| 394 |
bytesOffset += 4; |
| 395 |
} |
| 396 |
return new PaletteData(colors); |
| 397 |
} |
| 398 |
|
| 399 |
static byte[] loadData(BITMAPINFOHEADER bih, RandomAccessFile raf) throws IOException { |
| 400 |
int stride = (bih.biWidth * bih.biBitCount + 7) / 8; |
| 401 |
stride = (stride + 3) / 4 * 4; // Round up to 4 byte multiple |
| 402 |
byte[] data = loadData(bih, raf, stride); |
| 403 |
flipScanLines(data, stride, bih.biHeight); |
| 404 |
return data; |
| 405 |
} |
| 406 |
|
| 407 |
static void flipScanLines(byte[] data, int stride, int height) { |
| 408 |
int i1 = 0; |
| 409 |
int i2 = (height - 1) * stride; |
| 410 |
for (int i = 0; i < height / 2; i++) { |
| 411 |
for (int index = 0; index < stride; index++) { |
| 412 |
byte b = data[index + i1]; |
| 413 |
data[index + i1] = data[index + i2]; |
| 414 |
data[index + i2] = b; |
| 415 |
} |
| 416 |
i1 += stride; |
| 417 |
i2 -= stride; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
static byte[] loadData(BITMAPINFOHEADER bih, RandomAccessFile raf, int stride) throws IOException { |
| 422 |
int dataSize = bih.biHeight * stride; |
| 423 |
byte[] data = new byte[dataSize]; |
| 424 |
int cmp = bih.biCompression; |
| 425 |
if (cmp == 0) { // BMP_NO_COMPRESSION |
| 426 |
raf.read(data); |
| 427 |
} else { |
| 428 |
if (DEBUG) |
| 429 |
System.out.println("ICO cannot be compressed?"); //$NON-NLS-1$ |
| 430 |
} |
| 431 |
return data; |
| 432 |
} |
| 433 |
|
| 434 |
static void unloadIcon(RandomAccessFile raf, ImageData icon) throws IOException { |
| 435 |
int sizeImage = (((icon.width * icon.depth + 31) / 32 * 4) + ((icon.width + 31) / 32 * 4)) * icon.height; |
| 436 |
write4(raf, BMPHeaderFixedSize); |
| 437 |
write4(raf, icon.width); |
| 438 |
write4(raf, icon.height * 2); |
| 439 |
writeU2(raf, 1); |
| 440 |
writeU2(raf, icon.depth); |
| 441 |
write4(raf, 0); |
| 442 |
write4(raf, sizeImage); |
| 443 |
write4(raf, 0); |
| 444 |
write4(raf, 0); |
| 445 |
write4(raf, icon.palette.colors != null ? icon.palette.colors.length : 0); |
| 446 |
write4(raf, 0); |
| 447 |
|
| 448 |
byte[] rgbs = paletteToBytes(icon.palette); |
| 449 |
raf.write(rgbs); |
| 450 |
unloadShapeData(raf, icon); |
| 451 |
unloadMaskData(raf, icon); |
| 452 |
} |
| 453 |
|
| 454 |
static byte[] paletteToBytes(PaletteData pal) { |
| 455 |
int n = pal.colors == null ? 0 : (pal.colors.length < 256 ? pal.colors.length : 256); |
| 456 |
byte[] bytes = new byte[n * 4]; |
| 457 |
int offset = 0; |
| 458 |
for (int i = 0; i < n; i++) { |
| 459 |
RGB col = pal.colors[i]; |
| 460 |
bytes[offset] = (byte) col.blue; |
| 461 |
bytes[offset + 1] = (byte) col.green; |
| 462 |
bytes[offset + 2] = (byte) col.red; |
| 463 |
offset += 4; |
| 464 |
} |
| 465 |
return bytes; |
| 466 |
} |
| 467 |
|
| 468 |
static void unloadMaskData(RandomAccessFile raf, ImageData icon) { |
| 469 |
ImageData mask = icon.getTransparencyMask(); |
| 470 |
int bpl = (icon.width + 7) / 8; |
| 471 |
int pad = mask.scanlinePad; |
| 472 |
int srcBpl = (bpl + pad - 1) / pad * pad; |
| 473 |
int destBpl = (bpl + 3) / 4 * 4; |
| 474 |
byte[] buf = new byte[destBpl]; |
| 475 |
int offset = (icon.height - 1) * srcBpl; |
| 476 |
byte[] data = mask.data; |
| 477 |
try { |
| 478 |
for (int i = 0; i < icon.height; i++) { |
| 479 |
System.arraycopy(data, offset, buf, 0, bpl); |
| 480 |
bitInvertData(buf, 0, bpl); |
| 481 |
raf.write(buf, 0, destBpl); |
| 482 |
offset -= srcBpl; |
| 483 |
} |
| 484 |
} catch (IOException e) { |
| 485 |
SWT.error(SWT.ERROR_IO, e); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
static void unloadShapeData(RandomAccessFile raf, ImageData icon) { |
| 490 |
int bpl = (icon.width * icon.depth + 7) / 8; |
| 491 |
int pad = icon.scanlinePad; |
| 492 |
int srcBpl = (bpl + pad - 1) / pad * pad; |
| 493 |
int destBpl = (bpl + 3) / 4 * 4; |
| 494 |
byte[] buf = new byte[destBpl]; |
| 495 |
int offset = (icon.height - 1) * srcBpl; |
| 496 |
byte[] data = icon.data; |
| 497 |
try { |
| 498 |
for (int i = 0; i < icon.height; i++) { |
| 499 |
System.arraycopy(data, offset, buf, 0, bpl); |
| 500 |
raf.write(buf, 0, destBpl); |
| 501 |
offset -= srcBpl; |
| 502 |
} |
| 503 |
} catch (IOException e) { |
| 504 |
SWT.error(SWT.ERROR_IO, e); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
static boolean readIconGroup(RandomAccessFile raf, int offset, int size) throws IOException { |
| 509 |
raf.seek(offset); |
| 510 |
NEWHEADER newHeader = new NEWHEADER(); |
| 511 |
read(raf, newHeader); |
| 512 |
if (newHeader.ResType != RES_ICON) |
| 513 |
return false; |
| 514 |
RESDIR[] resDir = new RESDIR[newHeader.ResCount]; |
| 515 |
for (int i = 0; i < newHeader.ResCount; i++) { |
| 516 |
resDir[i] = new RESDIR(); |
| 517 |
read(raf, resDir[i]); |
| 518 |
} |
| 519 |
return true; |
| 520 |
} |
| 521 |
|
| 522 |
static void copyFile(String src, String dst) throws FileNotFoundException, IOException { |
| 523 |
File srcFile = new File(src); |
| 524 |
File dstFile = new File(dst); |
| 525 |
try (InputStream in = new BufferedInputStream(new FileInputStream(srcFile)); |
| 526 |
OutputStream out = new BufferedOutputStream(new FileOutputStream(dstFile))) { |
| 527 |
int c; |
| 528 |
while ((c = in.read()) != -1) |
| 529 |
out.write(c); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
/* IO utilities to parse Windows executable */ |
| 534 |
static final int IMAGE_DOS_SIGNATURE = 0x5a4d; |
| 535 |
static final int IMAGE_NT_SIGNATURE = 0x00004550; |
| 536 |
static final int IMAGE_DIRECTORY_ENTRY_RESOURCE = 2; |
| 537 |
static final int RES_ICON = 1; |
| 538 |
static final int RT_ICON = 3; |
| 539 |
static final int RT_GROUP_ICON = 14; |
| 540 |
static final int BMPHeaderFixedSize = 40; |
| 541 |
static final int IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b; |
| 542 |
static final int IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b; |
| 543 |
|
| 544 |
public static class IMAGE_DOS_HEADER { |
| 545 |
int e_magic; // WORD |
| 546 |
int e_cblp; // WORD |
| 547 |
int e_cp; // WORD |
| 548 |
int e_crlc; // WORD |
| 549 |
int e_cparhdr; // WORD |
| 550 |
int e_minalloc; // WORD |
| 551 |
int e_maxalloc; // WORD |
| 552 |
int e_ss; // WORD |
| 553 |
int e_sp; // WORD |
| 554 |
int e_csum; // WORD |
| 555 |
int e_ip; // WORD |
| 556 |
int e_cs; // WORD |
| 557 |
int e_lfarlc; // WORD |
| 558 |
int e_ovno; // WORD |
| 559 |
int[] e_res = new int[4]; // WORD[4] |
| 560 |
int e_oemid; // WORD |
| 561 |
int e_oeminfo; // WORD |
| 562 |
int[] e_res2 = new int[10]; // WORD[10] |
| 563 |
int e_lfanew; // LONG |
| 564 |
} |
| 565 |
|
| 566 |
public static class IMAGE_FILE_HEADER { |
| 567 |
int Machine; // WORD |
| 568 |
int NumberOfSections; // WORD |
| 569 |
int TimeDateStamp; // DWORD |
| 570 |
int PointerToSymbolTable; // DWORD |
| 571 |
int NumberOfSymbols; // DWORD |
| 572 |
int SizeOfOptionalHeader; // WORD |
| 573 |
int Characteristics; // WORD |
| 574 |
} |
| 575 |
|
| 576 |
public static class IMAGE_DATA_DIRECTORY { |
| 577 |
int VirtualAddress; // DWORD |
| 578 |
int Size; // DWORD |
| 579 |
} |
| 580 |
|
| 581 |
public static class IMAGE_OPTIONAL_HEADER { |
| 582 |
// Allocate enough storage for the 64 bit version of the header. |
| 583 |
int Magic; // WORD |
| 584 |
int MajorLinkerVersion; // BYTE |
| 585 |
int MinorLinkerVersion; // BYTE |
| 586 |
int SizeOfCode; // DWORD |
| 587 |
int SizeOfInitializedData; // DWORD |
| 588 |
int SizeOfUninitializedData; // DWORD |
| 589 |
int AddressOfEntryPoint; // DWORD |
| 590 |
int BaseOfCode; // DWORD |
| 591 |
int BaseOfData; // DWORD |
| 592 |
long ImageBase; // ULONGLONG |
| 593 |
int SectionAlignment; // DWORD |
| 594 |
int FileAlignment; // DWORD |
| 595 |
int MajorOperatingSystemVersion; // WORD |
| 596 |
int MinorOperatingSystemVersion; // WORD |
| 597 |
int MajorImageVersion; // WORD |
| 598 |
int MinorImageVersion; // WORD |
| 599 |
int MajorSubsystemVersion; // WORD |
| 600 |
int MinorSubsystemVersion; // WORD |
| 601 |
int Win32VersionValue; // DWORD |
| 602 |
int SizeOfImage; // DWORD |
| 603 |
int SizeOfHeaders; // DWORD |
| 604 |
int CheckSum; // DWORD |
| 605 |
int Subsystem; // WORD |
| 606 |
int DllCharacteristics; // WORD |
| 607 |
long SizeOfStackReserve; // ULONGLONG |
| 608 |
long SizeOfStackCommit; // ULONGLONG |
| 609 |
long SizeOfHeapReserve; // ULONGLONG |
| 610 |
long SizeOfHeapCommit; // ULONGLONG |
| 611 |
int LoaderFlags; // DWORD |
| 612 |
int NumberOfRvaAndSizes; // DWORD |
| 613 |
IMAGE_DATA_DIRECTORY[] DataDirectory = new IMAGE_DATA_DIRECTORY[16]; |
| 614 |
} |
| 615 |
|
| 616 |
public static class IMAGE_NT_HEADERS { |
| 617 |
int Signature; // DWORD |
| 618 |
IMAGE_FILE_HEADER FileHeader = new IMAGE_FILE_HEADER(); |
| 619 |
IMAGE_OPTIONAL_HEADER OptionalHeader = new IMAGE_OPTIONAL_HEADER(); |
| 620 |
final static int FIELD_OFFSET_OptionalHeader = 24; |
| 621 |
} |
| 622 |
|
| 623 |
public static class IMAGE_SECTION_HEADER { |
| 624 |
int[] Name = new int[8]; // BYTE[8] |
| 625 |
int Misc_VirtualSize; // DWORD (union Misc { DWORD PhysicalAddress; DWORD VirtualSize } |
| 626 |
int VirtualAddress; // DWORD |
| 627 |
int SizeOfRawData; // DWORD |
| 628 |
int PointerToRawData; // DWORD |
| 629 |
int PointerToRelocations; // DWORD |
| 630 |
int PointerToLinenumbers; // DWORD |
| 631 |
int NumberOfRelocations; // WORD |
| 632 |
int NumberOfLinenumbers; // WORD |
| 633 |
int Characteristics; // DWORD |
| 634 |
} |
| 635 |
|
| 636 |
public static class IMAGE_RESOURCE_DIRECTORY { |
| 637 |
int Characteristics; // DWORD |
| 638 |
int TimeDateStamp; // DWORD |
| 639 |
int MajorVersion; // WORD |
| 640 |
int MinorVersion; // WORD |
| 641 |
int NumberOfNamedEntries; // WORD - used |
| 642 |
int NumberOfIdEntries; // WORD - used |
| 643 |
final static int SIZEOF = 16; |
| 644 |
} |
| 645 |
|
| 646 |
public static class IMAGE_RESOURCE_DIRECTORY_ENTRY { |
| 647 |
// union |
| 648 |
int NameOffset; // DWORD 31 bits |
| 649 |
boolean NameIsString; // DWORD 1 bit |
| 650 |
int Name; // DWORD |
| 651 |
int Id; // WORD |
| 652 |
// union |
| 653 |
int OffsetToData; // DWORD |
| 654 |
int OffsetToDirectory; // DWORD 31 bits |
| 655 |
boolean DataIsDirectory; // DWORD 1 bit |
| 656 |
} |
| 657 |
|
| 658 |
public static class IMAGE_RESOURCE_DATA_ENTRY { |
| 659 |
int OffsetToData; // DWORD |
| 660 |
int Size; // DWORD |
| 661 |
int CodePage; // DWORD |
| 662 |
int Reserved; // DWORD |
| 663 |
} |
| 664 |
|
| 665 |
public static class NEWHEADER { |
| 666 |
int Reserved; // WORD |
| 667 |
int ResType; // WORD |
| 668 |
int ResCount; // WORD |
| 669 |
} |
| 670 |
|
| 671 |
public static class ICONRESDIR { |
| 672 |
int Width; // BYTE |
| 673 |
int Height; // BYTE |
| 674 |
int ColorCount; // BYTE |
| 675 |
int reserved; // BYTE |
| 676 |
} |
| 677 |
|
| 678 |
public static class CURSORDIR { |
| 679 |
int Width; // WORD |
| 680 |
int Height; // WORD |
| 681 |
} |
| 682 |
|
| 683 |
public static class RESDIR { |
| 684 |
// union |
| 685 |
ICONRESDIR Icon = new ICONRESDIR(); |
| 686 |
CURSORDIR Cursor = new CURSORDIR(); |
| 687 |
int Planes; // WORD |
| 688 |
int BitCount; // WORD |
| 689 |
int BytesInRes; // DWORD |
| 690 |
int IconCursorId; // WORD |
| 691 |
} |
| 692 |
|
| 693 |
public static class BITMAPINFOHEADER { |
| 694 |
int biSize; // DWORD |
| 695 |
int biWidth; // LONG |
| 696 |
int biHeight; // LONG |
| 697 |
int biPlanes; // WORD |
| 698 |
int biBitCount; // WORD |
| 699 |
int biCompression; // DWORD |
| 700 |
int biSizeImage; // DWORD |
| 701 |
int biXPelsPerMeter; // LONG |
| 702 |
int biYPelsPerMeter; // LONG |
| 703 |
int biClrUsed; // DWORD |
| 704 |
int biClrImportant; // DWORD |
| 705 |
} |
| 706 |
|
| 707 |
static class RGBQUAD { |
| 708 |
int rgBlue; // BYTE |
| 709 |
int rgbGreen; // BYTE |
| 710 |
int rgbRed; // BYTE |
| 711 |
int rgbReserved; // BYTE |
| 712 |
} |
| 713 |
|
| 714 |
static class BITMAPINFO { |
| 715 |
BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER(); |
| 716 |
RGBQUAD[] bmiColors = null; |
| 717 |
} |
| 718 |
|
| 719 |
static void read(RandomAccessFile raf, BITMAPINFOHEADER bih) throws IOException { |
| 720 |
bih.biSize = read4(raf); |
| 721 |
bih.biWidth = read4(raf); |
| 722 |
bih.biHeight = read4(raf); |
| 723 |
bih.biPlanes = readU2(raf); |
| 724 |
bih.biBitCount = readU2(raf); |
| 725 |
bih.biCompression = read4(raf); |
| 726 |
bih.biSizeImage = read4(raf); |
| 727 |
bih.biXPelsPerMeter = read4(raf); |
| 728 |
bih.biYPelsPerMeter = read4(raf); |
| 729 |
bih.biClrUsed = read4(raf); |
| 730 |
bih.biClrImportant = read4(raf); |
| 731 |
} |
| 732 |
|
| 733 |
static void read(RandomAccessFile raf, BITMAPINFO bi) throws IOException { |
| 734 |
read(raf, bi.bmiHeader); |
| 735 |
} |
| 736 |
|
| 737 |
/* Little Endian helpers */ |
| 738 |
static int readU2(RandomAccessFile raf) throws IOException { |
| 739 |
int b0 = raf.readByte() & 0xFF; |
| 740 |
int b1 = raf.readByte() & 0xFF; |
| 741 |
return (b1 << 8 | b0); |
| 742 |
} |
| 743 |
|
| 744 |
static int read4(RandomAccessFile raf) throws IOException { |
| 745 |
int b0 = raf.readByte() & 0xFF; |
| 746 |
int b1 = raf.readByte() & 0xFF; |
| 747 |
int b2 = raf.readByte() & 0xFF; |
| 748 |
int b3 = raf.readByte() & 0xFF; |
| 749 |
return b3 << 24 | b2 << 16 | b1 << 8 | b0; |
| 750 |
} |
| 751 |
|
| 752 |
static long read8(RandomAccessFile raf) throws IOException { |
| 753 |
long b0 = raf.readByte() & 0xFF; |
| 754 |
long b1 = raf.readByte() & 0xFF; |
| 755 |
long b2 = raf.readByte() & 0xFF; |
| 756 |
long b3 = raf.readByte() & 0xFF; |
| 757 |
long b4 = raf.readByte() & 0xFF; |
| 758 |
long b5 = raf.readByte() & 0xFF; |
| 759 |
long b6 = raf.readByte() & 0xFF; |
| 760 |
long b7 = raf.readByte() & 0xFF; |
| 761 |
return b7 << 56 | b6 << 48 | b5 << 40 | b4 << 32 | b3 << 24 | b2 << 16 | b1 << 8 | b0; |
| 762 |
} |
| 763 |
|
| 764 |
static void write4(RandomAccessFile raf, int value) throws IOException { |
| 765 |
raf.write(value & 0xFF); |
| 766 |
raf.write((value >> 8) & 0xFF); |
| 767 |
raf.write((value >> 16) & 0xFF); |
| 768 |
raf.write((value >> 24) & 0xFF); |
| 769 |
} |
| 770 |
|
| 771 |
static void writeU2(RandomAccessFile raf, int value) throws IOException { |
| 772 |
raf.write(value & 0xFF); |
| 773 |
raf.write((value >> 8) & 0xFF); |
| 774 |
} |
| 775 |
|
| 776 |
static void read(RandomAccessFile raf, IMAGE_DOS_HEADER idh) throws IOException { |
| 777 |
idh.e_magic = readU2(raf); |
| 778 |
idh.e_cblp = readU2(raf); |
| 779 |
idh.e_cp = readU2(raf); |
| 780 |
idh.e_crlc = readU2(raf); |
| 781 |
idh.e_cparhdr = readU2(raf); |
| 782 |
idh.e_minalloc = readU2(raf); |
| 783 |
idh.e_maxalloc = readU2(raf); |
| 784 |
idh.e_ss = readU2(raf); |
| 785 |
idh.e_sp = readU2(raf); |
| 786 |
idh.e_csum = readU2(raf); |
| 787 |
idh.e_ip = readU2(raf); |
| 788 |
idh.e_cs = readU2(raf); |
| 789 |
idh.e_lfarlc = readU2(raf); |
| 790 |
idh.e_ovno = readU2(raf); |
| 791 |
for (int i = 0; i < idh.e_res.length; i++) |
| 792 |
idh.e_res[i] = readU2(raf); |
| 793 |
idh.e_oemid = readU2(raf); |
| 794 |
idh.e_oeminfo = readU2(raf); |
| 795 |
for (int i = 0; i < idh.e_res2.length; i++) |
| 796 |
idh.e_res2[i] = readU2(raf); |
| 797 |
idh.e_lfanew = read4(raf); |
| 798 |
} |
| 799 |
|
| 800 |
static void read(RandomAccessFile raf, IMAGE_FILE_HEADER ifh) throws IOException { |
| 801 |
ifh.Machine = readU2(raf); |
| 802 |
ifh.NumberOfSections = readU2(raf); |
| 803 |
ifh.TimeDateStamp = read4(raf); |
| 804 |
ifh.PointerToSymbolTable = read4(raf); |
| 805 |
ifh.NumberOfSymbols = read4(raf); |
| 806 |
ifh.SizeOfOptionalHeader = readU2(raf); |
| 807 |
ifh.Characteristics = readU2(raf); |
| 808 |
} |
| 809 |
|
| 810 |
static void read(RandomAccessFile raf, IMAGE_DATA_DIRECTORY idd) throws IOException { |
| 811 |
idd.VirtualAddress = read4(raf); |
| 812 |
idd.Size = read4(raf); |
| 813 |
} |
| 814 |
|
| 815 |
static void read(RandomAccessFile raf, IMAGE_OPTIONAL_HEADER ioh) throws IOException { |
| 816 |
ioh.Magic = readU2(raf); |
| 817 |
|
| 818 |
// Assume file is 32bit executable unless x64 magic is present |
| 819 |
boolean is32 = !(ioh.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC); |
| 820 |
|
| 821 |
ioh.MajorLinkerVersion = raf.read(); |
| 822 |
ioh.MinorLinkerVersion = raf.read(); |
| 823 |
ioh.SizeOfCode = read4(raf); |
| 824 |
ioh.SizeOfInitializedData = read4(raf); |
| 825 |
ioh.SizeOfUninitializedData = read4(raf); |
| 826 |
ioh.AddressOfEntryPoint = read4(raf); |
| 827 |
ioh.BaseOfCode = read4(raf); |
| 828 |
|
| 829 |
if (is32) { |
| 830 |
ioh.BaseOfData = read4(raf); |
| 831 |
ioh.ImageBase = read4(raf); |
| 832 |
} else { |
| 833 |
// BaseOfData deleted in the 64 bit version PE32+ |
| 834 |
ioh.ImageBase = read8(raf); |
| 835 |
} |
| 836 |
|
| 837 |
ioh.SectionAlignment = read4(raf); |
| 838 |
ioh.FileAlignment = read4(raf); |
| 839 |
ioh.MajorOperatingSystemVersion = readU2(raf); |
| 840 |
ioh.MinorOperatingSystemVersion = readU2(raf); |
| 841 |
ioh.MajorImageVersion = readU2(raf); |
| 842 |
ioh.MinorImageVersion = readU2(raf); |
| 843 |
ioh.MajorSubsystemVersion = readU2(raf); |
| 844 |
ioh.MinorSubsystemVersion = readU2(raf); |
| 845 |
ioh.Win32VersionValue = read4(raf); |
| 846 |
ioh.SizeOfImage = read4(raf); |
| 847 |
ioh.SizeOfHeaders = read4(raf); |
| 848 |
ioh.CheckSum = read4(raf); |
| 849 |
ioh.Subsystem = readU2(raf); |
| 850 |
ioh.DllCharacteristics = readU2(raf); |
| 851 |
|
| 852 |
if (is32) { |
| 853 |
ioh.SizeOfStackReserve = read4(raf); |
| 854 |
ioh.SizeOfStackCommit = read4(raf); |
| 855 |
ioh.SizeOfHeapReserve = read4(raf); |
| 856 |
ioh.SizeOfHeapCommit = read4(raf); |
| 857 |
} else { |
| 858 |
ioh.SizeOfStackReserve = read8(raf); |
| 859 |
ioh.SizeOfStackCommit = read8(raf); |
| 860 |
ioh.SizeOfHeapReserve = read8(raf); |
| 861 |
ioh.SizeOfHeapCommit = read8(raf); |
| 862 |
} |
| 863 |
|
| 864 |
ioh.LoaderFlags = read4(raf); |
| 865 |
ioh.NumberOfRvaAndSizes = read4(raf); |
| 866 |
for (int i = 0; i < ioh.DataDirectory.length; i++) { |
| 867 |
ioh.DataDirectory[i] = new IMAGE_DATA_DIRECTORY(); |
| 868 |
read(raf, ioh.DataDirectory[i]); |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
static void read(RandomAccessFile raf, IMAGE_NT_HEADERS inh) throws IOException { |
| 873 |
inh.Signature = read4(raf); |
| 874 |
read(raf, inh.FileHeader); |
| 875 |
read(raf, inh.OptionalHeader); |
| 876 |
} |
| 877 |
|
| 878 |
static void read(RandomAccessFile raf, IMAGE_SECTION_HEADER ish) throws IOException { |
| 879 |
for (int i = 0; i < ish.Name.length; i++) |
| 880 |
ish.Name[i] = raf.read(); |
| 881 |
ish.Misc_VirtualSize = read4(raf); |
| 882 |
ish.VirtualAddress = read4(raf); |
| 883 |
ish.SizeOfRawData = read4(raf); |
| 884 |
ish.PointerToRawData = read4(raf); |
| 885 |
ish.PointerToRelocations = read4(raf); |
| 886 |
ish.PointerToLinenumbers = read4(raf); |
| 887 |
ish.NumberOfRelocations = readU2(raf); |
| 888 |
ish.NumberOfLinenumbers = readU2(raf); |
| 889 |
ish.Characteristics = read4(raf); |
| 890 |
} |
| 891 |
|
| 892 |
static void read(RandomAccessFile raf, IMAGE_RESOURCE_DIRECTORY ird) throws IOException { |
| 893 |
ird.Characteristics = read4(raf); |
| 894 |
ird.TimeDateStamp = read4(raf); |
| 895 |
ird.MajorVersion = readU2(raf); |
| 896 |
ird.MinorVersion = readU2(raf); |
| 897 |
ird.NumberOfNamedEntries = readU2(raf); |
| 898 |
ird.NumberOfIdEntries = readU2(raf); |
| 899 |
} |
| 900 |
|
| 901 |
static void read(RandomAccessFile raf, IMAGE_RESOURCE_DIRECTORY_ENTRY irde) throws IOException { |
| 902 |
irde.Name = read4(raf); |
| 903 |
irde.OffsetToData = read4(raf); |
| 904 |
// construct other union members |
| 905 |
irde.NameOffset = irde.Name & ~(1 << 31); |
| 906 |
irde.NameIsString = (irde.Name & (1 << 31)) != 0; |
| 907 |
irde.Id = irde.Name & 0xFFFF; |
| 908 |
irde.OffsetToDirectory = irde.OffsetToData & ~(1 << 31); |
| 909 |
irde.DataIsDirectory = (irde.OffsetToData & (1 << 31)) != 0; |
| 910 |
} |
| 911 |
|
| 912 |
static void read(RandomAccessFile raf, IMAGE_RESOURCE_DATA_ENTRY irde) throws IOException { |
| 913 |
irde.OffsetToData = read4(raf); |
| 914 |
irde.Size = read4(raf); |
| 915 |
irde.CodePage = read4(raf); |
| 916 |
irde.Reserved = read4(raf); |
| 917 |
} |
| 918 |
|
| 919 |
static void read(RandomAccessFile raf, NEWHEADER nh) throws IOException { |
| 920 |
nh.Reserved = readU2(raf); |
| 921 |
nh.ResType = readU2(raf); |
| 922 |
nh.ResCount = readU2(raf); |
| 923 |
} |
| 924 |
|
| 925 |
static void read(RandomAccessFile raf, ICONRESDIR i) throws IOException { |
| 926 |
i.Width = raf.read(); |
| 927 |
i.Height = raf.read(); |
| 928 |
i.ColorCount = raf.read(); |
| 929 |
i.reserved = raf.read(); |
| 930 |
} |
| 931 |
|
| 932 |
static void read(RandomAccessFile raf, CURSORDIR c) throws IOException { |
| 933 |
c.Width = readU2(raf); |
| 934 |
c.Height = readU2(raf); |
| 935 |
} |
| 936 |
|
| 937 |
static void read(RandomAccessFile raf, RESDIR rs) throws IOException { |
| 938 |
long start = raf.getFilePointer(); |
| 939 |
read(raf, rs.Icon); |
| 940 |
raf.seek(start); |
| 941 |
read(raf, rs.Cursor); |
| 942 |
rs.Planes = readU2(raf); |
| 943 |
rs.BitCount = readU2(raf); |
| 944 |
rs.BytesInRes = read4(raf); |
| 945 |
rs.IconCursorId = readU2(raf); |
| 946 |
} |
| 947 |
|
| 948 |
/* ImageData and Image Decoder inlining to avoid dependency on SWT |
| 949 |
* The following section can be entirely removed if SWT can be used. |
| 950 |
*/ |
| 951 |
|
| 952 |
static class RGB { |
| 953 |
|
| 954 |
/** |
| 955 |
* the red component of the RGB |
| 956 |
*/ |
| 957 |
public int red; |
| 958 |
|
| 959 |
/** |
| 960 |
* the green component of the RGB |
| 961 |
*/ |
| 962 |
public int green; |
| 963 |
|
| 964 |
/** |
| 965 |
* the blue component of the RGB |
| 966 |
*/ |
| 967 |
public int blue; |
| 968 |
|
| 969 |
static final long serialVersionUID = 3258415023461249074L; |
| 970 |
|
| 971 |
/** |
| 972 |
* Constructs an instance of this class with the given |
| 973 |
* red, green and blue values. |
| 974 |
* |
| 975 |
* @param red the red component of the new instance |
| 976 |
* @param green the green component of the new instance |
| 977 |
* @param blue the blue component of the new instance |
| 978 |
* |
| 979 |
* @exception IllegalArgumentException <ul> |
| 980 |
* <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li> |
| 981 |
* </ul> |
| 982 |
*/ |
| 983 |
public RGB(int red, int green, int blue) { |
| 984 |
if ((red > 255) || (red < 0) || (green > 255) || (green < 0) || (blue > 255) || (blue < 0)) |
| 985 |
SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 986 |
this.red = red; |
| 987 |
this.green = green; |
| 988 |
this.blue = blue; |
| 989 |
} |
| 990 |
|
| 991 |
/** |
| 992 |
* Compares the argument to the receiver, and returns true |
| 993 |
* if they represent the <em>same</em> object using a class |
| 994 |
* specific comparison. |
| 995 |
* |
| 996 |
* @param object the object to compare with this object |
| 997 |
* @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise |
| 998 |
* |
| 999 |
* @see #hashCode() |
| 1000 |
*/ |
| 1001 |
@Override |
| 1002 |
public boolean equals(Object object) { |
| 1003 |
if (object == this) |
| 1004 |
return true; |
| 1005 |
if (!(object instanceof RGB)) |
| 1006 |
return false; |
| 1007 |
RGB rgb = (RGB) object; |
| 1008 |
return (rgb.red == this.red) && (rgb.green == this.green) && (rgb.blue == this.blue); |
| 1009 |
} |
| 1010 |
|
| 1011 |
/** |
| 1012 |
* Returns an integer hash code for the receiver. Any two |
| 1013 |
* objects which return <code>true</code> when passed to |
| 1014 |
* <code>equals</code> must return the same value for this |
| 1015 |
* method. |
| 1016 |
* |
| 1017 |
* @return the receiver's hash |
| 1018 |
* |
| 1019 |
* @see #equals(Object) |
| 1020 |
*/ |
| 1021 |
@Override |
| 1022 |
public int hashCode() { |
| 1023 |
return (blue << 16) | (green << 8) | red; |
| 1024 |
} |
| 1025 |
|
| 1026 |
/** |
| 1027 |
* Returns a string containing a concise, human-readable |
| 1028 |
* description of the receiver. |
| 1029 |
* |
| 1030 |
* @return a string representation of the <code>RGB</code> |
| 1031 |
*/ |
| 1032 |
@Override |
| 1033 |
public String toString() { |
| 1034 |
return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
| 1035 |
|
| 1036 |
//$NON-NLS-4$ |
| 1037 |
} |
| 1038 |
|
| 1039 |
} |
| 1040 |
|
| 1041 |
static class PaletteData { |
| 1042 |
|
| 1043 |
/** |
| 1044 |
* true if the receiver is a direct palette, |
| 1045 |
* and false otherwise |
| 1046 |
*/ |
| 1047 |
public boolean isDirect; |
| 1048 |
|
| 1049 |
/** |
| 1050 |
* the RGB values for an indexed palette, where the |
| 1051 |
* indices of the array correspond to pixel values |
| 1052 |
*/ |
| 1053 |
public RGB[] colors; |
| 1054 |
|
| 1055 |
/** |
| 1056 |
* the red mask for a direct palette |
| 1057 |
*/ |
| 1058 |
public int redMask; |
| 1059 |
|
| 1060 |
/** |
| 1061 |
* the green mask for a direct palette |
| 1062 |
*/ |
| 1063 |
public int greenMask; |
| 1064 |
|
| 1065 |
/** |
| 1066 |
* the blue mask for a direct palette |
| 1067 |
*/ |
| 1068 |
public int blueMask; |
| 1069 |
|
| 1070 |
/** |
| 1071 |
* the red shift for a direct palette |
| 1072 |
*/ |
| 1073 |
public int redShift; |
| 1074 |
|
| 1075 |
/** |
| 1076 |
* the green shift for a direct palette |
| 1077 |
*/ |
| 1078 |
public int greenShift; |
| 1079 |
|
| 1080 |
/** |
| 1081 |
* the blue shift for a direct palette |
| 1082 |
*/ |
| 1083 |
public int blueShift; |
| 1084 |
|
| 1085 |
/** |
| 1086 |
* Constructs a new indexed palette given an array of RGB values. |
| 1087 |
* |
| 1088 |
* @param colors the array of <code>RGB</code>s for the palette |
| 1089 |
* |
| 1090 |
* @exception IllegalArgumentException <ul> |
| 1091 |
* <li>ERROR_NULL_ARGUMENT - if the argument is null</li> |
| 1092 |
* </ul> |
| 1093 |
*/ |
| 1094 |
public PaletteData(RGB[] colors) { |
| 1095 |
if (colors == null) |
| 1096 |
SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 1097 |
this.colors = colors; |
| 1098 |
this.isDirect = false; |
| 1099 |
} |
| 1100 |
|
| 1101 |
/** |
| 1102 |
* Constructs a new direct palette given the red, green and blue masks. |
| 1103 |
* |
| 1104 |
* @param redMask the red mask |
| 1105 |
* @param greenMask the green mask |
| 1106 |
* @param blueMask the blue mask |
| 1107 |
*/ |
| 1108 |
public PaletteData(int redMask, int greenMask, int blueMask) { |
| 1109 |
this.redMask = redMask; |
| 1110 |
this.greenMask = greenMask; |
| 1111 |
this.blueMask = blueMask; |
| 1112 |
this.isDirect = true; |
| 1113 |
this.redShift = shiftForMask(redMask); |
| 1114 |
this.greenShift = shiftForMask(greenMask); |
| 1115 |
this.blueShift = shiftForMask(blueMask); |
| 1116 |
} |
| 1117 |
|
| 1118 |
/** |
| 1119 |
* Returns the pixel value corresponding to the given <code>RBG</code>. |
| 1120 |
* |
| 1121 |
* @param rgb the RGB to get the pixel value for |
| 1122 |
* @return the pixel value for the given RGB |
| 1123 |
* |
| 1124 |
* @exception IllegalArgumentException <ul> |
| 1125 |
* <li>ERROR_NULL_ARGUMENT - if the argument is null</li> |
| 1126 |
* <li>ERROR_INVALID_ARGUMENT - if the RGB is not found in the palette</li> |
| 1127 |
* </ul> |
| 1128 |
*/ |
| 1129 |
public int getPixel(RGB rgb) { |
| 1130 |
if (rgb == null) |
| 1131 |
SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 1132 |
if (isDirect) { |
| 1133 |
int pixel = 0; |
| 1134 |
pixel |= (redShift < 0 ? rgb.red << -redShift : rgb.red >>> redShift) & redMask; |
| 1135 |
pixel |= (greenShift < 0 ? rgb.green << -greenShift : rgb.green >>> greenShift) & greenMask; |
| 1136 |
pixel |= (blueShift < 0 ? rgb.blue << -blueShift : rgb.blue >>> blueShift) & blueMask; |
| 1137 |
return pixel; |
| 1138 |
} |
| 1139 |
|
| 1140 |
for (int i = 0; i < colors.length; i++) { |
| 1141 |
if (colors[i].equals(rgb)) |
| 1142 |
return i; |
| 1143 |
} |
| 1144 |
/* The RGB did not exist in the palette */ |
| 1145 |
SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 1146 |
return 0; |
| 1147 |
} |
| 1148 |
|
| 1149 |
/** |
| 1150 |
* Returns an <code>RGB</code> corresponding to the given pixel value. |
| 1151 |
* |
| 1152 |
* @param pixel the pixel to get the RGB value for |
| 1153 |
* @return the RGB value for the given pixel |
| 1154 |
* |
| 1155 |
* @exception IllegalArgumentException <ul> |
| 1156 |
* <li>ERROR_NULL_ARGUMENT - if the argument is null</li> |
| 1157 |
* <li>ERROR_INVALID_ARGUMENT - if the pixel does not exist in the palette</li> |
| 1158 |
* </ul> |
| 1159 |
*/ |
| 1160 |
public RGB getRGB(int pixel) { |
| 1161 |
if (isDirect) { |
| 1162 |
int r = pixel & redMask; |
| 1163 |
r = (redShift < 0) ? r >>> -redShift : r << redShift; |
| 1164 |
int g = pixel & greenMask; |
| 1165 |
g = (greenShift < 0) ? g >>> -greenShift : g << greenShift; |
| 1166 |
int b = pixel & blueMask; |
| 1167 |
b = (blueShift < 0) ? b >>> -blueShift : b << blueShift; |
| 1168 |
return new RGB(r, g, b); |
| 1169 |
} |
| 1170 |
if (pixel < 0 || pixel >= colors.length) { |
| 1171 |
SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 1172 |
} |
| 1173 |
return colors[pixel]; |
| 1174 |
} |
| 1175 |
|
| 1176 |
/** |
| 1177 |
* Returns all the RGB values in the receiver if it is an |
| 1178 |
* indexed palette, or null if it is a direct palette. |
| 1179 |
* |
| 1180 |
* @return the <code>RGB</code>s for the receiver or null |
| 1181 |
*/ |
| 1182 |
public RGB[] getRGBs() { |
| 1183 |
return colors; |
| 1184 |
} |
| 1185 |
|
| 1186 |
/** |
| 1187 |
* Computes the shift value for a given mask. |
| 1188 |
* |
| 1189 |
* @param mask the mask to compute the shift for |
| 1190 |
* @return the shift amount |
| 1191 |
* |
| 1192 |
* @see IconExe.PaletteData |
| 1193 |
*/ |
| 1194 |
int shiftForMask(int mask) { |
| 1195 |
for (int i = 31; i >= 0; i--) { |
| 1196 |
if (((mask >> i) & 0x1) != 0) |
| 1197 |
return 7 - i; |
| 1198 |
} |
| 1199 |
return 32; |
| 1200 |
} |
| 1201 |
|
| 1202 |
} |
| 1203 |
|
| 1204 |
static class ImageLoader { |
| 1205 |
|
| 1206 |
/** |
| 1207 |
* the array of ImageData objects in this ImageLoader. |
| 1208 |
* This array is read in when the load method is called, |
| 1209 |
* and it is written out when the save method is called |
| 1210 |
*/ |
| 1211 |
public ImageData[] data; |
| 1212 |
|
| 1213 |
/** |
| 1214 |
* the width of the logical screen on which the images |
| 1215 |
* reside, in pixels (this corresponds to the GIF89a |
| 1216 |
* Logical Screen Width value) |
| 1217 |
*/ |
| 1218 |
public int logicalScreenWidth; |
| 1219 |
|
| 1220 |
/** |
| 1221 |
* the height of the logical screen on which the images |
| 1222 |
* reside, in pixels (this corresponds to the GIF89a |
| 1223 |
* Logical Screen Height value) |
| 1224 |
*/ |
| 1225 |
public int logicalScreenHeight; |
| 1226 |
|
| 1227 |
/** |
| 1228 |
* the background pixel for the logical screen (this |
| 1229 |
* corresponds to the GIF89a Background Color Index value). |
| 1230 |
* The default is -1 which means 'unspecified background' |
| 1231 |
* |
| 1232 |
*/ |
| 1233 |
public int backgroundPixel; |
| 1234 |
|
| 1235 |
/** |
| 1236 |
* the number of times to repeat the display of a sequence |
| 1237 |
* of animated images (this corresponds to the commonly-used |
| 1238 |
* GIF application extension for "NETSCAPE 2.0 01") |
| 1239 |
*/ |
| 1240 |
public int repeatCount; |
| 1241 |
|
| 1242 |
/* |
| 1243 |
* the set of ImageLoader event listeners, created on demand |
| 1244 |
*/ |
| 1245 |
@SuppressWarnings("rawtypes") |
| 1246 |
Vector imageLoaderListeners; |
| 1247 |
|
| 1248 |
/** |
| 1249 |
* Construct a new empty ImageLoader. |
| 1250 |
*/ |
| 1251 |
public ImageLoader() { |
| 1252 |
reset(); |
| 1253 |
} |
| 1254 |
|
| 1255 |
/** |
| 1256 |
* Resets the fields of the ImageLoader, except for the |
| 1257 |
* <code>imageLoaderListeners</code> field. |
| 1258 |
*/ |
| 1259 |
void reset() { |
| 1260 |
data = null; |
| 1261 |
logicalScreenWidth = 0; |
| 1262 |
logicalScreenHeight = 0; |
| 1263 |
backgroundPixel = -1; |
| 1264 |
repeatCount = 1; |
| 1265 |
} |
| 1266 |
|
| 1267 |
/** |
| 1268 |
* Loads an array of <code>ImageData</code> objects from the |
| 1269 |
* specified input stream. Throws an error if either an error |
| 1270 |
* occurs while loading the images, or if the images are not |
| 1271 |
* of a supported type. Returns the loaded image data array. |
| 1272 |
* |
| 1273 |
* @param stream the input stream to load the images from |
| 1274 |
* @return an array of <code>ImageData</code> objects loaded from the specified input stream |
| 1275 |
* |
| 1276 |
* @exception IllegalArgumentException <ul> |
| 1277 |
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li> |
| 1278 |
* </ul> |
| 1279 |
* @exception RuntimeException <ul> |
| 1280 |
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li> |
| 1281 |
* <li>ERROR_IO - if an input/output error occurs while reading data</li> |
| 1282 |
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> |
| 1283 |
* </ul> |
| 1284 |
*/ |
| 1285 |
public ImageData[] load(InputStream stream) { |
| 1286 |
if (stream == null) |
| 1287 |
throw new NullPointerException("stream can't be null"); //$NON-NLS-1$ |
| 1288 |
reset(); |
| 1289 |
data = FileFormat.load(stream, this); |
| 1290 |
return data; |
| 1291 |
} |
| 1292 |
|
| 1293 |
} |
| 1294 |
|
| 1295 |
static class ImageData { |
| 1296 |
|
| 1297 |
/** |
| 1298 |
* The width of the image, in pixels. |
| 1299 |
*/ |
| 1300 |
public int width; |
| 1301 |
|
| 1302 |
/** |
| 1303 |
* The height of the image, in pixels. |
| 1304 |
*/ |
| 1305 |
public int height; |
| 1306 |
|
| 1307 |
/** |
| 1308 |
* The color depth of the image, in bits per pixel. |
| 1309 |
* <p> |
| 1310 |
* Note that a depth of 8 or less does not necessarily |
| 1311 |
* mean that the image is palette indexed, or |
| 1312 |
* conversely that a depth greater than 8 means that |
| 1313 |
* the image is direct color. Check the associated |
| 1314 |
* PaletteData's isDirect field for such determinations. |
| 1315 |
*/ |
| 1316 |
public int depth; |
| 1317 |
|
| 1318 |
/** |
| 1319 |
* The scanline padding. |
| 1320 |
* <p> |
| 1321 |
* If one scanline of the image is not a multiple of |
| 1322 |
* this number, it will be padded with zeros until it is. |
| 1323 |
* </p> |
| 1324 |
*/ |
| 1325 |
public int scanlinePad; |
| 1326 |
|
| 1327 |
/** |
| 1328 |
* The number of bytes per scanline. |
| 1329 |
* <p> |
| 1330 |
* This is a multiple of the scanline padding. |
| 1331 |
* </p> |
| 1332 |
*/ |
| 1333 |
public int bytesPerLine; |
| 1334 |
|
| 1335 |
/** |
| 1336 |
* The pixel data of the image. |
| 1337 |
* <p> |
| 1338 |
* Note that for 16 bit depth images the pixel data is stored |
| 1339 |
* in least significant byte order; however, for 24bit and |
| 1340 |
* 32bit depth images the pixel data is stored in most |
| 1341 |
* significant byte order. |
| 1342 |
* </p> |
| 1343 |
*/ |
| 1344 |
public byte[] data; |
| 1345 |
|
| 1346 |
/** |
| 1347 |
* The color table for the image. |
| 1348 |
*/ |
| 1349 |
public PaletteData palette; |
| 1350 |
|
| 1351 |
/** |
| 1352 |
* The transparent pixel. |
| 1353 |
* <p> |
| 1354 |
* Pixels with this value are transparent. |
| 1355 |
* </p><p> |
| 1356 |
* The default is -1 which means 'no transparent pixel'. |
| 1357 |
* </p> |
| 1358 |
*/ |
| 1359 |
public int transparentPixel; |
| 1360 |
|
| 1361 |
/** |
| 1362 |
* An icon-specific field containing the data from the icon mask. |
| 1363 |
* <p> |
| 1364 |
* This is a 1 bit bitmap stored with the most significant |
| 1365 |
* bit first. The number of bytes per scanline is |
| 1366 |
* '((width + 7) / 8 + (maskPad - 1)) / maskPad * maskPad'. |
| 1367 |
* </p><p> |
| 1368 |
* The default is null which means 'no transparency mask'. |
| 1369 |
* </p> |
| 1370 |
*/ |
| 1371 |
public byte[] maskData; |
| 1372 |
|
| 1373 |
/** |
| 1374 |
* An icon-specific field containing the scanline pad of the mask. |
| 1375 |
* <p> |
| 1376 |
* If one scanline of the transparency mask is not a |
| 1377 |
* multiple of this number, it will be padded with zeros until |
| 1378 |
* it is. |
| 1379 |
* </p> |
| 1380 |
*/ |
| 1381 |
public int maskPad; |
| 1382 |
|
| 1383 |
/** |
| 1384 |
* The alpha data of the image. |
| 1385 |
* <p> |
| 1386 |
* Every pixel can have an <em>alpha blending</em> value that |
| 1387 |
* varies from 0, meaning fully transparent, to 255 meaning |
| 1388 |
* fully opaque. The number of bytes per scanline is |
| 1389 |
* 'width'. |
| 1390 |
* </p> |
| 1391 |
*/ |
| 1392 |
public byte[] alphaData; |
| 1393 |
|
| 1394 |
/** |
| 1395 |
* The global alpha value to be used for every pixel. |
| 1396 |
* <p> |
| 1397 |
* If this value is set, the <code>alphaData</code> field |
| 1398 |
* is ignored and when the image is rendered each pixel |
| 1399 |
* will be blended with the background an amount |
| 1400 |
* proportional to this value. |
| 1401 |
* </p><p> |
| 1402 |
* The default is -1 which means 'no global alpha value' |
| 1403 |
* </p> |
| 1404 |
*/ |
| 1405 |
public int alpha; |
| 1406 |
|
| 1407 |
/** |
| 1408 |
* The type of file from which the image was read. |
| 1409 |
* |
| 1410 |
* It is expressed as one of the following values: |
| 1411 |
* <dl> |
| 1412 |
* <dt><code>IMAGE_BMP</code></dt> |
| 1413 |
* <dd>Windows BMP file format, no compression</dd> |
| 1414 |
* <dt><code>IMAGE_BMP_RLE</code></dt> |
| 1415 |
* <dd>Windows BMP file format, RLE compression if appropriate</dd> |
| 1416 |
* <dt><code>IMAGE_GIF</code></dt> |
| 1417 |
* <dd>GIF file format</dd> |
| 1418 |
* <dt><code>IMAGE_ICO</code></dt> |
| 1419 |
* <dd>Windows ICO file format</dd> |
| 1420 |
* <dt><code>IMAGE_JPEG</code></dt> |
| 1421 |
* <dd>JPEG file format</dd> |
| 1422 |
* <dt><code>IMAGE_PNG</code></dt> |
| 1423 |
* <dd>PNG file format</dd> |
| 1424 |
* </dl> |
| 1425 |
*/ |
| 1426 |
public int type; |
| 1427 |
|
| 1428 |
/** |
| 1429 |
* The x coordinate of the top left corner of the image |
| 1430 |
* within the logical screen (this field corresponds to |
| 1431 |
* the GIF89a Image Left Position value). |
| 1432 |
*/ |
| 1433 |
public int x; |
| 1434 |
|
| 1435 |
/** |
| 1436 |
* The y coordinate of the top left corner of the image |
| 1437 |
* within the logical screen (this field corresponds to |
| 1438 |
* the GIF89a Image Top Position value). |
| 1439 |
*/ |
| 1440 |
public int y; |
| 1441 |
|
| 1442 |
/** |
| 1443 |
* A description of how to dispose of the current image |
| 1444 |
* before displaying the next. |
| 1445 |
* |
| 1446 |
* It is expressed as one of the following values: |
| 1447 |
* <dl> |
| 1448 |
* <dt><code>DM_UNSPECIFIED</code></dt> |
| 1449 |
* <dd>disposal method not specified</dd> |
| 1450 |
* <dt><code>DM_FILL_NONE</code></dt> |
| 1451 |
* <dd>do nothing - leave the image in place</dd> |
| 1452 |
* <dt><code>DM_FILL_BACKGROUND</code></dt> |
| 1453 |
* <dd>fill with the background color</dd> |
| 1454 |
* <dt><code>DM_FILL_PREVIOUS</code></dt> |
| 1455 |
* <dd>restore the previous picture</dd> |
| 1456 |
* </dl> |
| 1457 |
* (this field corresponds to the GIF89a Disposal Method value) |
| 1458 |
*/ |
| 1459 |
public int disposalMethod; |
| 1460 |
|
| 1461 |
/** |
| 1462 |
* The time to delay before displaying the next image |
| 1463 |
* in an animation (this field corresponds to the GIF89a |
| 1464 |
* Delay Time value). |
| 1465 |
*/ |
| 1466 |
public int delayTime; |
| 1467 |
|
| 1468 |
/** |
| 1469 |
* Arbitrary channel width data to 8-bit conversion table. |
| 1470 |
*/ |
| 1471 |
static final byte[][] ANY_TO_EIGHT = new byte[9][]; |
| 1472 |
static { |
| 1473 |
for (int b = 0; b < 9; ++b) { |
| 1474 |
byte[] data = ANY_TO_EIGHT[b] = new byte[1 << b]; |
| 1475 |
if (b == 0) |
| 1476 |
continue; |
| 1477 |
int inc = 0; |
| 1478 |
for (int bit = 0x10000; (bit >>= b) != 0;) |
| 1479 |
inc |= bit; |
| 1480 |
for (int v = 0, p = 0; v < 0x10000; v += inc) |
| 1481 |
data[p++] = (byte) (v >> 8); |
| 1482 |
} |
| 1483 |
} |
| 1484 |
static final byte[] ONE_TO_ONE_MAPPING = ANY_TO_EIGHT[8]; |
| 1485 |
|
| 1486 |
/** |
| 1487 |
* Scaled 8x8 Bayer dither matrix. |
| 1488 |
*/ |
| 1489 |
static final int[][] DITHER_MATRIX = {{0xfc0000, 0x7c0000, 0xdc0000, 0x5c0000, 0xf40000, 0x740000, 0xd40000, 0x540000}, {0x3c0000, 0xbc0000, 0x1c0000, 0x9c0000, 0x340000, 0xb40000, 0x140000, 0x940000}, {0xcc0000, 0x4c0000, 0xec0000, 0x6c0000, 0xc40000, 0x440000, 0xe40000, 0x640000}, {0x0c0000, 0x8c0000, 0x2c0000, 0xac0000, 0x040000, 0x840000, 0x240000, 0xa40000}, {0xf00000, 0x700000, 0xd00000, 0x500000, 0xf80000, 0x780000, 0xd80000, 0x580000}, {0x300000, 0xb00000, 0x100000, 0x900000, 0x380000, 0xb80000, 0x180000, 0x980000}, {0xc00000, 0x400000, 0xe00000, 0x600000, 0xc80000, 0x480000, 0xe80000, 0x680000}, {0x000000, 0x800000, 0x200000, 0xa00000, 0x080000, 0x880000, 0x280000, 0xa80000}}; |
| 1490 |
|
| 1491 |
/** |
| 1492 |
* Constructs a new, empty ImageData with the given width, height, |
| 1493 |
* depth and palette. The data will be initialized to an (all zero) |
| 1494 |
* array of the appropriate size. |
| 1495 |
* |
| 1496 |
* @param width the width of the image |
| 1497 |
* @param height the height of the image |
| 1498 |
* @param depth the depth of the image |
| 1499 |
* @param palette the palette of the image (must not be null) |
| 1500 |
* |
| 1501 |
* @exception IllegalArgumentException <ul> |
| 1502 |
* <li>ERROR_INVALID_ARGUMENT - if the width or height is negative, or if the depth is not |
| 1503 |
* one of 1, 2, 4, 8, 16, 24 or 32</li> |
| 1504 |
* <li>ERROR_NULL_ARGUMENT - if the palette is null</li> |
| 1505 |
* </ul> |
| 1506 |
*/ |
| 1507 |
public ImageData(int width, int height, int depth, PaletteData palette) { |
| 1508 |
this(width, height, depth, palette, 4, null, 0, null, null, -1, -1, SWT.IMAGE_UNDEFINED, 0, 0, 0, 0); |
| 1509 |
} |
| 1510 |
|
| 1511 |
/** |
| 1512 |
* Constructs a new, empty ImageData with the given width, height, |
| 1513 |
* depth, palette, scanlinePad and data. |
| 1514 |
* |
| 1515 |
* @param width the width of the image |
| 1516 |
* @param height the height of the image |
| 1517 |
* @param depth the depth of the image |
| 1518 |
* @param palette the palette of the image |
| 1519 |
* @param scanlinePad the padding of each line, in bytes |
| 1520 |
* @param data the data of the image |
| 1521 |
* |
| 1522 |
* @exception IllegalArgumentException <ul> |
| 1523 |
* <li>ERROR_INVALID_ARGUMENT - if the width or height is negative, or if the depth is not |
| 1524 |
* one of 1, 2, 4, 8, 16, 24 or 32</li> |
| 1525 |
* <li>ERROR_NULL_ARGUMENT - if the palette or data is null</li> |
| 1526 |
* <li>ERROR_CANNOT_BE_ZERO - if the scanlinePad is zero</li> |
| 1527 |
* </ul> |
| 1528 |
*/ |
| 1529 |
public ImageData(int width, int height, int depth, PaletteData palette, int scanlinePad, byte[] data) { |
| 1530 |
this(width, height, depth, palette, scanlinePad, checkData(data), 0, null, null, -1, -1, SWT.IMAGE_UNDEFINED, 0, 0, 0, 0); |
| 1531 |
} |
| 1532 |
|
| 1533 |
/** |
| 1534 |
* Constructs an <code>ImageData</code> loaded from a file with the |
| 1535 |
* specified name. Throws an error if an error occurs loading the |
| 1536 |
* image, or if the image has an unsupported type. |
| 1537 |
* <p> |
| 1538 |
* This constructor is provided for convenience when loading a single |
| 1539 |
* image only. If the file contains multiple images, only the first |
| 1540 |
* one will be loaded. To load multiple images, use |
| 1541 |
* <code>ImageLoader.load()</code>. |
| 1542 |
* </p> |
| 1543 |
* |
| 1544 |
* @param data the image data to use |
| 1545 |
* |
| 1546 |
* @exception IllegalArgumentException <ul> |
| 1547 |
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li> |
| 1548 |
* </ul> |
| 1549 |
* @exception RuntimeException <ul> |
| 1550 |
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li> |
| 1551 |
* <li>ERROR_IO if an IO error occurs while reading data</li> |
| 1552 |
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> |
| 1553 |
* </ul> |
| 1554 |
*/ |
| 1555 |
public ImageData(ImageData[] data) { |
| 1556 |
if (data.length < 1) |
| 1557 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 1558 |
ImageData i = data[0]; |
| 1559 |
setAllFields(i.width, i.height, i.depth, i.scanlinePad, i.bytesPerLine, i.data, i.palette, i.transparentPixel, i.maskData, i.maskPad, i.alphaData, i.alpha, i.type, i.x, i.y, i.disposalMethod, i.delayTime); |
| 1560 |
} |
| 1561 |
|
| 1562 |
/** |
| 1563 |
* Prevents uninitialized instances from being created outside the package. |
| 1564 |
*/ |
| 1565 |
ImageData() { |
| 1566 |
//empty constructor |
| 1567 |
} |
| 1568 |
|
| 1569 |
/** |
| 1570 |
* Constructs an image data by giving values for all non-computable fields. |
| 1571 |
* <p> |
| 1572 |
* This method is for internal use, and is not described further. |
| 1573 |
* </p> |
| 1574 |
*/ |
| 1575 |
ImageData(int width, int height, int depth, PaletteData palette, int scanlinePad, byte[] data, int maskPad, byte[] maskData, byte[] alphaData, int alpha, int transparentPixel, int type, int x, int y, int disposalMethod, int delayTime) { |
| 1576 |
|
| 1577 |
if (palette == null) |
| 1578 |
SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 1579 |
if (!(depth == 1 || depth == 2 || depth == 4 || depth == 8 || depth == 16 || depth == 24 || depth == 32)) { |
| 1580 |
SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 1581 |
} |
| 1582 |
if (width <= 0 || height <= 0) { |
| 1583 |
SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 1584 |
} |
| 1585 |
if (scanlinePad == 0) |
| 1586 |
SWT.error(SWT.ERROR_CANNOT_BE_ZERO); |
| 1587 |
|
| 1588 |
int bytesPerLine = (((width * depth + 7) / 8) + (scanlinePad - 1)) / scanlinePad * scanlinePad; |
| 1589 |
setAllFields(width, height, depth, scanlinePad, bytesPerLine, data != null ? data : new byte[bytesPerLine * height], palette, transparentPixel, maskData, maskPad, alphaData, alpha, type, x, y, disposalMethod, delayTime); |
| 1590 |
} |
| 1591 |
|
| 1592 |
/** |
| 1593 |
* Initializes all fields in the receiver. This method must be called |
| 1594 |
* by all public constructors to ensure that all fields are initialized |
| 1595 |
* for a new ImageData object. If a new field is added to the class, |
| 1596 |
* then it must be added to this method. |
| 1597 |
* <p> |
| 1598 |
* This method is for internal use, and is not described further. |
| 1599 |
* </p> |
| 1600 |
*/ |
| 1601 |
void setAllFields(int width, int height, int depth, int scanlinePad, int bytesPerLine, byte[] data, PaletteData palette, int transparentPixel, byte[] maskData, int maskPad, byte[] alphaData, int alpha, int type, int x, int y, int disposalMethod, int delayTime) { |
| 1602 |
|
| 1603 |
this.width = width; |
| 1604 |
this.height = height; |
| 1605 |
this.depth = depth; |
| 1606 |
this.scanlinePad = scanlinePad; |
| 1607 |
this.bytesPerLine = bytesPerLine; |
| 1608 |
this.data = data; |
| 1609 |
this.palette = palette; |
| 1610 |
this.transparentPixel = transparentPixel; |
| 1611 |
this.maskData = maskData; |
| 1612 |
this.maskPad = maskPad; |
| 1613 |
this.alphaData = alphaData; |
| 1614 |
this.alpha = alpha; |
| 1615 |
this.type = type; |
| 1616 |
this.x = x; |
| 1617 |
this.y = y; |
| 1618 |
this.disposalMethod = disposalMethod; |
| 1619 |
this.delayTime = delayTime; |
| 1620 |
} |
| 1621 |
|
| 1622 |
/** |
| 1623 |
* Invokes internal SWT functionality to create a new instance of |
| 1624 |
* this class. |
| 1625 |
* <p> |
| 1626 |
* <b>IMPORTANT:</b> This method is <em>not</em> part of the public |
| 1627 |
* API for <code>ImageData</code>. It is marked public only so that it |
| 1628 |
* can be shared within the packages provided by SWT. It is subject |
| 1629 |
* to change without notice, and should never be called from |
| 1630 |
* application code. |
| 1631 |
* </p> |
| 1632 |
* <p> |
| 1633 |
* This method is for internal use, and is not described further. |
| 1634 |
* </p> |
| 1635 |
*/ |
| 1636 |
public static ImageData internal_new(int width, int height, int depth, PaletteData palette, int scanlinePad, byte[] data, int maskPad, byte[] maskData, byte[] alphaData, int alpha, int transparentPixel, int type, int x, int y, int disposalMethod, int delayTime) { |
| 1637 |
return new ImageData(width, height, depth, palette, scanlinePad, data, maskPad, maskData, alphaData, alpha, transparentPixel, type, x, y, disposalMethod, delayTime); |
| 1638 |
} |
| 1639 |
|
| 1640 |
ImageData colorMaskImage(int pixel) { |
| 1641 |
ImageData mask = new ImageData(width, height, 1, bwPalette(), 2, null, 0, null, null, -1, -1, SWT.IMAGE_UNDEFINED, 0, 0, 0, 0); |
| 1642 |
int[] row = new int[width]; |
| 1643 |
for (int y = 0; y < height; y++) { |
| 1644 |
getPixels(0, y, width, row, 0); |
| 1645 |
for (int i = 0; i < width; i++) { |
| 1646 |
if (pixel != -1 && row[i] == pixel) { |
| 1647 |
row[i] = 0; |
| 1648 |
} else { |
| 1649 |
row[i] = 1; |
| 1650 |
} |
| 1651 |
} |
| 1652 |
mask.setPixels(0, y, width, row, 0); |
| 1653 |
} |
| 1654 |
return mask; |
| 1655 |
} |
| 1656 |
|
| 1657 |
static byte[] checkData(byte[] data) { |
| 1658 |
if (data == null) |
| 1659 |
SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 1660 |
return data; |
| 1661 |
} |
| 1662 |
|
| 1663 |
/** |
| 1664 |
* Returns <code>getWidth</code> pixel values starting at offset |
| 1665 |
* <code>x</code> in scanline <code>y</code> in the receiver's |
| 1666 |
* data starting at <code>startIndex</code>. |
| 1667 |
* |
| 1668 |
* @param x the x position of the first pixel to get |
| 1669 |
* @param y the y position of the first pixel to get |
| 1670 |
* @param getWidth the width of the data to get |
| 1671 |
* @param pixels the buffer in which to put the pixels |
| 1672 |
* @param startIndex the offset into the byte array to begin storing pixels |
| 1673 |
* |
| 1674 |
* @exception IndexOutOfBoundsException if getWidth is too large |
| 1675 |
* @exception IllegalArgumentException <ul> |
| 1676 |
* <li>ERROR_NULL_ARGUMENT - if pixels is null</li> |
| 1677 |
* <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> |
| 1678 |
* <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li> |
| 1679 |
* </ul> |
| 1680 |
* @exception RuntimeException <ul> |
| 1681 |
* <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4 or 8 |
| 1682 |
* (For higher depths, use the int[] version of this method.)</li> |
| 1683 |
* </ul> |
| 1684 |
*/ |
| 1685 |
public void getPixels(int x, int y, int getWidth, byte[] pixels, int startIndex) { |
| 1686 |
if (pixels == null) |
| 1687 |
SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 1688 |
if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) |
| 1689 |
SWT.error |
| 1690 |
|
| 1691 |
(SWT.ERROR_INVALID_ARGUMENT); |
| 1692 |
if (getWidth == 0) |
| 1693 |
return; |
| 1694 |
int index; |
| 1695 |
int theByte; |
| 1696 |
int mask = 0; |
| 1697 |
int n = getWidth; |
| 1698 |
int i = startIndex; |
| 1699 |
int srcX = x, srcY = y; |
| 1700 |
if (depth == 1) { |
| 1701 |
index = (y * bytesPerLine) + (x >> 3); |
| 1702 |
theByte = data[index] & 0xFF; |
| 1703 |
while (n > 0) { |
| 1704 |
mask = 1 << (7 - (srcX & 0x7)); |
| 1705 |
if ((theByte & mask) == 0) { |
| 1706 |
pixels[i] = 0; |
| 1707 |
} else { |
| 1708 |
pixels[i] = 1; |
| 1709 |
} |
| 1710 |
i++; |
| 1711 |
n--; |
| 1712 |
srcX++; |
| 1713 |
if (srcX >= width) { |
| 1714 |
srcY++; |
| 1715 |
index = srcY * bytesPerLine; |
| 1716 |
if (n > 0) |
| 1717 |
theByte = data[index] & 0xFF; |
| 1718 |
srcX = 0; |
| 1719 |
} else { |
| 1720 |
if (mask == 1) { |
| 1721 |
index++; |
| 1722 |
if (n > 0) |
| 1723 |
theByte = data[index] & 0xFF; |
| 1724 |
} |
| 1725 |
} |
| 1726 |
} |
| 1727 |
return; |
| 1728 |
} |
| 1729 |
if (depth == 2) { |
| 1730 |
index = (y * bytesPerLine) + (x >> 2); |
| 1731 |
theByte = data[index] & 0xFF; |
| 1732 |
int offset; |
| 1733 |
while (n > 0) { |
| 1734 |
offset = 3 - (srcX % 4); |
| 1735 |
mask = 3 << (offset * 2); |
| 1736 |
pixels[i] = (byte) ((theByte & mask) >> (offset * 2)); |
| 1737 |
i++; |
| 1738 |
n--; |
| 1739 |
srcX++; |
| 1740 |
if (srcX >= width) { |
| 1741 |
srcY++; |
| 1742 |
index = srcY * bytesPerLine; |
| 1743 |
if (n > 0) |
| 1744 |
theByte = data[index] & 0xFF; |
| 1745 |
srcX = 0; |
| 1746 |
} else { |
| 1747 |
if (offset == 0) { |
| 1748 |
index++; |
| 1749 |
theByte = data[index] & 0xFF; |
| 1750 |
} |
| 1751 |
} |
| 1752 |
} |
| 1753 |
return; |
| 1754 |
} |
| 1755 |
if (depth == 4) { |
| 1756 |
index = (y * bytesPerLine) + (x >> 1); |
| 1757 |
if ((x & 0x1) == 1) { |
| 1758 |
theByte = data[index] & 0xFF; |
| 1759 |
pixels[i] = (byte) (theByte & 0x0F); |
| 1760 |
i++; |
| 1761 |
n--; |
| 1762 |
srcX++; |
| 1763 |
if (srcX >= width) { |
| 1764 |
srcY++; |
| 1765 |
index = srcY * bytesPerLine; |
| 1766 |
srcX = 0; |
| 1767 |
} else { |
| 1768 |
index++; |
| 1769 |
} |
| 1770 |
} |
| 1771 |
while (n > 1) { |
| 1772 |
theByte = data[index] & 0xFF; |
| 1773 |
pixels[i] = (byte) (theByte >> 4); |
| 1774 |
i++; |
| 1775 |
n--; |
| 1776 |
srcX++; |
| 1777 |
if (srcX >= width) { |
| 1778 |
srcY++; |
| 1779 |
index = srcY * bytesPerLine; |
| 1780 |
srcX = 0; |
| 1781 |
} else { |
| 1782 |
pixels[i] = (byte) (theByte & 0x0F); |
| 1783 |
i++; |
| 1784 |
n--; |
| 1785 |
srcX++; |
| 1786 |
if (srcX >= width) { |
| 1787 |
srcY++; |
| 1788 |
index = srcY * bytesPerLine; |
| 1789 |
srcX = 0; |
| 1790 |
} else { |
| 1791 |
index++; |
| 1792 |
} |
| 1793 |
} |
| 1794 |
} |
| 1795 |
if (n > 0) { |
| 1796 |
theByte = data[index] & 0xFF; |
| 1797 |
pixels[i] = (byte) (theByte >> 4); |
| 1798 |
} |
| 1799 |
return; |
| 1800 |
} |
| 1801 |
if (depth == 8) { |
| 1802 |
index = (y * bytesPerLine) + x; |
| 1803 |
for (int j = 0; j < getWidth; j++) { |
| 1804 |
pixels[i] = data[index]; |
| 1805 |
i++; |
| 1806 |
srcX++; |
| 1807 |
if (srcX >= width) { |
| 1808 |
srcY++; |
| 1809 |
index = srcY * bytesPerLine; |
| 1810 |
srcX = 0; |
| 1811 |
} else { |
| 1812 |
index++; |
| 1813 |
} |
| 1814 |
} |
| 1815 |
return; |
| 1816 |
} |
| 1817 |
SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); |
| 1818 |
} |
| 1819 |
|
| 1820 |
/** |
| 1821 |
* Returns <code>getWidth</code> pixel values starting at offset |
| 1822 |
* <code>x</code> in scanline <code>y</code> in the receiver's |
| 1823 |
* data starting at <code>startIndex</code>. |
| 1824 |
* |
| 1825 |
* @param x the x position of the first pixel to get |
| 1826 |
* @param y the y position of the first pixel to get |
| 1827 |
* @param getWidth the width of the data to get |
| 1828 |
* @param pixels the buffer in which to put the pixels |
| 1829 |
* @param startIndex the offset into the buffer to begin storing pixels |
| 1830 |
* |
| 1831 |
* @exception IndexOutOfBoundsException if getWidth is too large |
| 1832 |
* @exception IllegalArgumentException <ul> |
| 1833 |
* <li>ERROR_NULL_ARGUMENT - if pixels is null</li> |
| 1834 |
* <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> |
| 1835 |
* <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li> |
| 1836 |
* </ul> |
| 1837 |
* @exception RuntimeException <ul> |
| 1838 |
* <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li> |
| 1839 |
* </ul> |
| 1840 |
*/ |
| 1841 |
public void getPixels(int x, int y, int getWidth, int[] pixels, int startIndex) { |
| 1842 |
if (pixels == null) |
| 1843 |
SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 1844 |
if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) |
| 1845 |
SWT.error |
| 1846 |
|
| 1847 |
(SWT.ERROR_INVALID_ARGUMENT); |
| 1848 |
if (getWidth == 0) |
| 1849 |
return; |
| 1850 |
int index; |
| 1851 |
int theByte; |
| 1852 |
int mask; |
| 1853 |
int n = getWidth; |
| 1854 |
int i = startIndex; |
| 1855 |
int srcX = x, srcY = y; |
| 1856 |
if (depth == 1) { |
| 1857 |
index = (y * bytesPerLine) + (x >> 3); |
| 1858 |
theByte = data[index] & 0xFF; |
| 1859 |
while (n > 0) { |
| 1860 |
mask = 1 << (7 - (srcX & 0x7)); |
| 1861 |
if ((theByte & mask) == 0) { |
| 1862 |
pixels[i] = 0; |
| 1863 |
} else { |
| 1864 |
pixels[i] = 1; |
| 1865 |
} |
| 1866 |
i++; |
| 1867 |
n--; |
| 1868 |
srcX++; |
| 1869 |
if (srcX >= width) { |
| 1870 |
srcY++; |
| 1871 |
index = srcY * bytesPerLine; |
| 1872 |
if (n > 0) |
| 1873 |
theByte = data[index] & 0xFF; |
| 1874 |
srcX = 0; |
| 1875 |
} else { |
| 1876 |
if (mask == 1) { |
| 1877 |
index++; |
| 1878 |
if (n > 0) |
| 1879 |
theByte = data[index] & 0xFF; |
| 1880 |
} |
| 1881 |
} |
| 1882 |
} |
| 1883 |
return; |
| 1884 |
} |
| 1885 |
if (depth == 2) { |
| 1886 |
index = (y * bytesPerLine) + (x >> 2); |
| 1887 |
theByte = data[index] & 0xFF; |
| 1888 |
int offset; |
| 1889 |
while (n > 0) { |
| 1890 |
offset = 3 - (srcX % 4); |
| 1891 |
mask = 3 << (offset * 2); |
| 1892 |
pixels[i] = (byte) ((theByte & mask) >> (offset * 2)); |
| 1893 |
i++; |
| 1894 |
n--; |
| 1895 |
srcX++; |
| 1896 |
if (srcX >= width) { |
| 1897 |
srcY++; |
| 1898 |
index = srcY * bytesPerLine; |
| 1899 |
if (n > 0) |
| 1900 |
theByte = data[index] & 0xFF; |
| 1901 |
srcX = 0; |
| 1902 |
} else { |
| 1903 |
if (offset == 0) { |
| 1904 |
index++; |
| 1905 |
theByte = data[index] & 0xFF; |
| 1906 |
} |
| 1907 |
} |
| 1908 |
} |
| 1909 |
return; |
| 1910 |
} |
| 1911 |
if (depth == 4) { |
| 1912 |
index = (y * bytesPerLine) + (x >> 1); |
| 1913 |
if ((x & 0x1) == 1) { |
| 1914 |
theByte = data[index] & 0xFF; |
| 1915 |
pixels[i] = theByte & 0x0F; |
| 1916 |
i++; |
| 1917 |
n--; |
| 1918 |
srcX++; |
| 1919 |
if (srcX >= width) { |
| 1920 |
srcY++; |
| 1921 |
index = srcY * bytesPerLine; |
| 1922 |
srcX = 0; |
| 1923 |
} else { |
| 1924 |
index++; |
| 1925 |
} |
| 1926 |
} |
| 1927 |
while (n > 1) { |
| 1928 |
theByte = data[index] & 0xFF; |
| 1929 |
pixels[i] = theByte >> 4; |
| 1930 |
i++; |
| 1931 |
n--; |
| 1932 |
srcX++; |
| 1933 |
if (srcX >= width) { |
| 1934 |
srcY++; |
| 1935 |
index = srcY * bytesPerLine; |
| 1936 |
srcX = 0; |
| 1937 |
} else { |
| 1938 |
pixels[i] = theByte & 0x0F; |
| 1939 |
i++; |
| 1940 |
n--; |
| 1941 |
srcX++; |
| 1942 |
if (srcX >= width) { |
| 1943 |
srcY++; |
| 1944 |
index = srcY * bytesPerLine; |
| 1945 |
srcX = 0; |
| 1946 |
} else { |
| 1947 |
index++; |
| 1948 |
} |
| 1949 |
} |
| 1950 |
} |
| 1951 |
if (n > 0) { |
| 1952 |
theByte = data[index] & 0xFF; |
| 1953 |
pixels[i] = theByte >> 4; |
| 1954 |
} |
| 1955 |
return; |
| 1956 |
} |
| 1957 |
if (depth == 8) { |
| 1958 |
index = (y * bytesPerLine) + x; |
| 1959 |
for (int j = 0; j < getWidth; j++) { |
| 1960 |
pixels[i] = data[index] & 0xFF; |
| 1961 |
i++; |
| 1962 |
srcX++; |
| 1963 |
if (srcX >= width) { |
| 1964 |
srcY++; |
| 1965 |
index = srcY * bytesPerLine; |
| 1966 |
srcX = 0; |
| 1967 |
} else { |
| 1968 |
index++; |
| 1969 |
} |
| 1970 |
} |
| 1971 |
return; |
| 1972 |
} |
| 1973 |
if (depth == 16) { |
| 1974 |
index = (y * bytesPerLine) + (x * 2); |
| 1975 |
for (int j = 0; j < getWidth; j++) { |
| 1976 |
pixels[i] = ((data[index + 1] & 0xFF) << 8) + (data[index] & 0xFF); |
| 1977 |
i++; |
| 1978 |
srcX++; |
| 1979 |
if (srcX >= width) { |
| 1980 |
srcY++; |
| 1981 |
index = srcY * bytesPerLine; |
| 1982 |
srcX = 0; |
| 1983 |
} else { |
| 1984 |
index += 2; |
| 1985 |
} |
| 1986 |
} |
| 1987 |
return; |
| 1988 |
} |
| 1989 |
if (depth == 24) { |
| 1990 |
index = (y * bytesPerLine) + (x * 3); |
| 1991 |
for (int j = 0; j < getWidth; j++) { |
| 1992 |
pixels[i] = ((data[index] & 0xFF) << 16) | ((data[index + 1] & 0xFF) << 8) | (data[index + 2] & 0xFF); |
| 1993 |
i++; |
| 1994 |
srcX++; |
| 1995 |
if (srcX >= width) { |
| 1996 |
srcY++; |
| 1997 |
index = srcY * bytesPerLine; |
| 1998 |
srcX = 0; |
| 1999 |
} else { |
| 2000 |
index += 3; |
| 2001 |
} |
| 2002 |
} |
| 2003 |
return; |
| 2004 |
} |
| 2005 |
if (depth == 32) { |
| 2006 |
index = (y * bytesPerLine) + (x * 4); |
| 2007 |
i = startIndex; |
| 2008 |
for (int j = 0; j < getWidth; j++) { |
| 2009 |
pixels[i] = ((data[index] & 0xFF) << 24) | ((data[index + 1] & 0xFF) << 16) | ((data[index + 2] & 0xFF) << 8) | (data[index + 3] & 0xFF); |
| 2010 |
i++; |
| 2011 |
srcX++; |
| 2012 |
if (srcX >= width) { |
| 2013 |
srcY++; |
| 2014 |
index = srcY * bytesPerLine; |
| 2015 |
srcX = 0; |
| 2016 |
} else { |
| 2017 |
index += 4; |
| 2018 |
} |
| 2019 |
} |
| 2020 |
return; |
| 2021 |
} |
| 2022 |
SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); |
| 2023 |
} |
| 2024 |
|
| 2025 |
/** |
| 2026 |
* Returns an array of <code>RGB</code>s which comprise the |
| 2027 |
* indexed color table of the receiver, or null if the receiver |
| 2028 |
* has a direct color model. |
| 2029 |
* |
| 2030 |
* @return the RGB values for the image or null if direct color |
| 2031 |
* |
| 2032 |
* @see IconExe.PaletteData#getRGBs() |
| 2033 |
*/ |
| 2034 |
public RGB[] getRGBs() { |
| 2035 |
return palette.getRGBs(); |
| 2036 |
} |
| 2037 |
|
| 2038 |
/** |
| 2039 |
* Returns an <code>ImageData</code> which specifies the |
| 2040 |
* transparency mask information for the receiver, or null if the |
| 2041 |
* receiver has no transparency and is not an icon. |
| 2042 |
* |
| 2043 |
* @return the transparency mask or null if none exists |
| 2044 |
*/ |
| 2045 |
public ImageData getTransparencyMask() { |
| 2046 |
if (getTransparencyType() == SWT.TRANSPARENCY_MASK) { |
| 2047 |
return new ImageData(width, height, 1, bwPalette(), maskPad, maskData); |
| 2048 |
} |
| 2049 |
return colorMaskImage(transparentPixel); |
| 2050 |
} |
| 2051 |
|
| 2052 |
/** |
| 2053 |
* Returns the image transparency type. |
| 2054 |
* |
| 2055 |
* @return the receiver's transparency type |
| 2056 |
*/ |
| 2057 |
public int getTransparencyType() { |
| 2058 |
if (maskData != null) |
| 2059 |
return SWT.TRANSPARENCY_MASK; |
| 2060 |
if (transparentPixel != -1) |
| 2061 |
return SWT.TRANSPARENCY_PIXEL; |
| 2062 |
if (alphaData != null) |
| 2063 |
return SWT.TRANSPARENCY_ALPHA; |
| 2064 |
return SWT.TRANSPARENCY_NONE; |
| 2065 |
} |
| 2066 |
|
| 2067 |
/** |
| 2068 |
* Returns the byte order of the receiver. |
| 2069 |
* |
| 2070 |
* @return MSB_FIRST or LSB_FIRST |
| 2071 |
*/ |
| 2072 |
int getByteOrder() { |
| 2073 |
return depth != 16 ? MSB_FIRST : LSB_FIRST; |
| 2074 |
} |
| 2075 |
|
| 2076 |
/** |
| 2077 |
* Sets the pixel values starting at offset <code>x</code> in |
| 2078 |
* scanline <code>y</code> in the receiver's data to the |
| 2079 |
* values from the array <code>pixels</code> starting at |
| 2080 |
* <code>startIndex</code>. |
| 2081 |
* |
| 2082 |
* @param x the x position of the pixel to set |
| 2083 |
* @param y the y position of the pixel to set |
| 2084 |
* @param putWidth the width of the pixels to set |
| 2085 |
* @param pixels the pixels to set |
| 2086 |
* @param startIndex the index at which to begin setting |
| 2087 |
* |
| 2088 |
* @exception IndexOutOfBoundsException if putWidth is too large |
| 2089 |
* @exception IllegalArgumentException <ul> |
| 2090 |
* <li>ERROR_NULL_ARGUMENT - if pixels is null</li> |
| 2091 |
* <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> |
| 2092 |
* <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li> |
| 2093 |
* </ul> |
| 2094 |
* @exception RuntimeException <ul> |
| 2095 |
* <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8 |
| 2096 |
* (For higher depths, use the int[] version of this method.)</li> |
| 2097 |
* </ul> |
| 2098 |
*/ |
| 2099 |
public void setPixels(int x, int y, int putWidth, byte[] pixels, int startIndex) { |
| 2100 |
if (pixels == null) |
| 2101 |
SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 2102 |
if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) |
| 2103 |
SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 2104 |
if (putWidth == 0) |
| 2105 |
return; |
| 2106 |
int index; |
| 2107 |
int theByte; |
| 2108 |
int mask; |
| 2109 |
int n = putWidth; |
| 2110 |
int i = startIndex; |
| 2111 |
int srcX = x, srcY = y; |
| 2112 |
if (depth == 1) { |
| 2113 |
index = (y * bytesPerLine) + (x >> 3); |
| 2114 |
while (n > 0) { |
| 2115 |
mask = 1 << (7 - (srcX & 0x7)); |
| 2116 |
if ((pixels[i] & 0x1) == 1) { |
| 2117 |
data[index] = (byte) ((data[index] & 0xFF) | mask); |
| 2118 |
} else { |
| 2119 |
data[index] = (byte) ((data[index] & 0xFF) & (mask ^ -1)); |
| 2120 |
} |
| 2121 |
i++; |
| 2122 |
n--; |
| 2123 |
srcX++; |
| 2124 |
if (srcX >= width) { |
| 2125 |
srcY++; |
| 2126 |
index = srcY * bytesPerLine; |
| 2127 |
srcX = 0; |
| 2128 |
} else { |
| 2129 |
if (mask == 1) { |
| 2130 |
index++; |
| 2131 |
} |
| 2132 |
} |
| 2133 |
} |
| 2134 |
return; |
| 2135 |
} |
| 2136 |
if (depth == 2) { |
| 2137 |
byte[] masks = {(byte) 0xFC, (byte) 0xF3, (byte) 0xCF, (byte) 0x3F}; |
| 2138 |
index = (y * bytesPerLine) + (x >> 2); |
| 2139 |
int offset = 3 - (x % 4); |
| 2140 |
while (n > 0) { |
| 2141 |
theByte = pixels[i] & 0x3; |
| 2142 |
data[index] = (byte) ((data[index] & masks[offset]) | (theByte << (offset * 2))); |
| 2143 |
i++; |
| 2144 |
n--; |
| 2145 |
srcX++; |
| 2146 |
if (srcX >= width) { |
| 2147 |
srcY++; |
| 2148 |
index = srcY * bytesPerLine; |
| 2149 |
offset = 0; |
| 2150 |
srcX = 0; |
| 2151 |
} else { |
| 2152 |
if (offset == 0) { |
| 2153 |
index++; |
| 2154 |
offset = 3; |
| 2155 |
} else { |
| 2156 |
offset--; |
| 2157 |
} |
| 2158 |
} |
| 2159 |
} |
| 2160 |
return; |
| 2161 |
} |
| 2162 |
if (depth == 4) { |
| 2163 |
index = (y * bytesPerLine) + (x >> 1); |
| 2164 |
boolean high = (x & 0x1) == 0; |
| 2165 |
while (n > 0) { |
| 2166 |
theByte = pixels[i] & 0x0F; |
| 2167 |
if (high) { |
| 2168 |
data[index] = (byte) ((data[index] & 0x0F) | (theByte << 4)); |
| 2169 |
} else { |
| 2170 |
data[index] = (byte) ((data[index] & 0xF0) | theByte); |
| 2171 |
} |
| 2172 |
i++; |
| 2173 |
n--; |
| 2174 |
srcX++; |
| 2175 |
if (srcX >= width) { |
| 2176 |
srcY++; |
| 2177 |
index = srcY * bytesPerLine; |
| 2178 |
high = true; |
| 2179 |
srcX = 0; |
| 2180 |
} else { |
| 2181 |
if (!high) |
| 2182 |
index++; |
| 2183 |
high = !high; |
| 2184 |
} |
| 2185 |
} |
| 2186 |
return; |
| 2187 |
} |
| 2188 |
if (depth == 8) { |
| 2189 |
index = (y * bytesPerLine) + x; |
| 2190 |
for (int j = 0; j < putWidth; j++) { |
| 2191 |
data[index] = (byte) (pixels[i] & 0xFF); |
| 2192 |
i++; |
| 2193 |
srcX++; |
| 2194 |
if (srcX >= width) { |
| 2195 |
srcY++; |
| 2196 |
index = srcY * bytesPerLine; |
| 2197 |
srcX = 0; |
| 2198 |
} else { |
| 2199 |
index++; |
| 2200 |
} |
| 2201 |
} |
| 2202 |
return; |
| 2203 |
} |
| 2204 |
SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); |
| 2205 |
} |
| 2206 |
|
| 2207 |
/** |
| 2208 |
* Sets the pixel values starting at offset <code>x</code> in |
| 2209 |
* scanline <code>y</code> in the receiver's data to the |
| 2210 |
* values from the array <code>pixels</code> starting at |
| 2211 |
* <code>startIndex</code>. |
| 2212 |
* |
| 2213 |
* @param x the x position of the pixel to set |
| 2214 |
* @param y the y position of the pixel to set |
| 2215 |
* @param putWidth the width of the pixels to set |
| 2216 |
* @param pixels the pixels to set |
| 2217 |
* @param startIndex the index at which to begin setting |
| 2218 |
* |
| 2219 |
* @exception IndexOutOfBoundsException if putWidth is too large |
| 2220 |
* @exception IllegalArgumentException <ul> |
| 2221 |
* <li>ERROR_NULL_ARGUMENT - if pixels is null</li> |
| 2222 |
* <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> |
| 2223 |
* <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li> |
| 2224 |
* </ul> |
| 2225 |
* @exception RuntimeException <ul> |
| 2226 |
* <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li> |
| 2227 |
* </ul> |
| 2228 |
*/ |
| 2229 |
public void setPixels(int x, int y, int putWidth, int[] pixels, int startIndex) { |
| 2230 |
if (pixels == null) |
| 2231 |
SWT.error(SWT.ERROR_NULL_ARGUMENT); |
| 2232 |
if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) |
| 2233 |
SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
| 2234 |
if (putWidth == 0) |
| 2235 |
return; |
| 2236 |
int index; |
| 2237 |
int theByte; |
| 2238 |
int mask; |
| 2239 |
int n = putWidth; |
| 2240 |
int i = startIndex; |
| 2241 |
int pixel; |
| 2242 |
int srcX = x, srcY = y; |
| 2243 |
if (depth == 1) { |
| 2244 |
index = (y * bytesPerLine) + (x >> 3); |
| 2245 |
while (n > 0) { |
| 2246 |
mask = 1 << (7 - (srcX & 0x7)); |
| 2247 |
if ((pixels[i] & 0x1) == 1) { |
| 2248 |
data[index] = (byte) ((data[index] & 0xFF) | mask); |
| 2249 |
} else { |
| 2250 |
data[index] = (byte) ((data[index] & 0xFF) & (mask ^ -1)); |
| 2251 |
} |
| 2252 |
i++; |
| 2253 |
n--; |
| 2254 |
srcX++; |
| 2255 |
if (srcX >= width) { |
| 2256 |
srcY++; |
| 2257 |
index = srcY * bytesPerLine; |
| 2258 |
srcX = 0; |
| 2259 |
} else { |
| 2260 |
if (mask == 1) { |
| 2261 |
index++; |
| 2262 |
} |
| 2263 |
} |
| 2264 |
} |
| 2265 |
return; |
| 2266 |
} |
| 2267 |
if (depth == 2) { |
| 2268 |
byte[] masks = {(byte) 0xFC, (byte) 0xF3, (byte) 0xCF, (byte) 0x3F}; |
| 2269 |
index = (y * bytesPerLine) + (x >> 2); |
| 2270 |
int offset = 3 - (x % 4); |
| 2271 |
while (n > 0) { |
| 2272 |
theByte = pixels[i] & 0x3; |
| 2273 |
data[index] = (byte) ((data[index] & masks[offset]) | (theByte << (offset * 2))); |
| 2274 |
i++; |
| 2275 |
n--; |
| 2276 |
srcX++; |
| 2277 |
if (srcX >= width) { |
| 2278 |
srcY++; |
| 2279 |
index = srcY * bytesPerLine; |
| 2280 |
offset = 3; |
| 2281 |
srcX = 0; |
| 2282 |
} else { |
| 2283 |
if (offset == 0) { |
| 2284 |
index++; |
| 2285 |
offset = 3; |
| 2286 |
} else { |
| 2287 |
offset--; |
| 2288 |
} |
| 2289 |
} |
| 2290 |
} |
| 2291 |
return; |
| 2292 |
} |
| 2293 |
if (depth == 4) { |
| 2294 |
index = (y * bytesPerLine) + (x >> 1); |
| 2295 |
boolean high = (x & 0x1) == 0; |
| 2296 |
while (n > 0) { |
| 2297 |
theByte = pixels[i] & 0x0F; |
| 2298 |
if (high) { |
| 2299 |
data[index] = (byte) ((data[index] & 0x0F) | (theByte << 4)); |
| 2300 |
} else { |
| 2301 |
data[index] = (byte) ((data[index] & 0xF0) | theByte); |
| 2302 |
} |
| 2303 |
i++; |
| 2304 |
n--; |
| 2305 |
srcX++; |
| 2306 |
if (srcX >= width) { |
| 2307 |
srcY++; |
| 2308 |
index = srcY * bytesPerLine; |
| 2309 |
high = true; |
| 2310 |
srcX = 0; |
| 2311 |
} else { |
| 2312 |
if (!high) |
| 2313 |
index++; |
| 2314 |
high = !high; |
| 2315 |
} |
| 2316 |
} |
| 2317 |
return; |
| 2318 |
} |
| 2319 |
if (depth == 8) { |
| 2320 |
index = (y * bytesPerLine) + x; |
| 2321 |
for (int j = 0; j < putWidth; j++) { |
| 2322 |
data[index] = (byte) (pixels[i] & 0xFF); |
| 2323 |
i++; |
| 2324 |
srcX++; |
| 2325 |
if (srcX >= width) { |
| 2326 |
srcY++; |
| 2327 |
index = srcY * bytesPerLine; |
| 2328 |
srcX = 0; |
| 2329 |
} else { |
| 2330 |
index++; |
| 2331 |
} |
| 2332 |
} |
| 2333 |
return; |
| 2334 |
|
| 2335 |
} |
| 2336 |
if (depth == 16) { |
| 2337 |
index = (y * bytesPerLine) + (x * 2); |
| 2338 |
for (int j = 0; j < putWidth; j++) { |
| 2339 |
pixel = pixels[i]; |
| 2340 |
data[index] = (byte) (pixel & 0xFF); |
| 2341 |
data[index + 1] = (byte) ((pixel >> 8) & 0xFF); |
| 2342 |
i++; |
| 2343 |
srcX++; |
| 2344 |
if (srcX >= width) { |
| 2345 |
srcY++; |
| 2346 |
index = srcY * bytesPerLine; |
| 2347 |
srcX = 0; |
| 2348 |
} else { |
| 2349 |
index += 2; |
| 2350 |
} |
| 2351 |
} |
| 2352 |
return; |
| 2353 |
} |
| 2354 |
if (depth == 24) { |
| 2355 |
index = (y * bytesPerLine) + (x * 3); |
| 2356 |
for (int j = 0; j < putWidth; j++) { |
| 2357 |
pixel = pixels[i]; |
| 2358 |
data[index] = (byte) ((pixel >> 16) & 0xFF); |
| 2359 |
data[index + 1] = (byte) ((pixel >> 8) & 0xFF); |
| 2360 |
data[index + 2] = (byte) (pixel & 0xFF); |
| 2361 |
i++; |
| 2362 |
srcX++; |
| 2363 |
if (srcX >= width) { |
| 2364 |
srcY++; |
| 2365 |
index = srcY * bytesPerLine; |
| 2366 |
srcX = 0; |
| 2367 |
} else { |
| 2368 |
index += 3; |
| 2369 |
} |
| 2370 |
} |
| 2371 |
return; |
| 2372 |
} |
| 2373 |
if (depth == 32) { |
| 2374 |
index = (y * bytesPerLine) + (x * 4); |
| 2375 |
for (int j = 0; j < putWidth; j++) { |
| 2376 |
pixel = pixels[i]; |
| 2377 |
data[index] = (byte) ((pixel >> 24) & 0xFF); |
| 2378 |
data[index + 1] = (byte) ((pixel >> 16) & 0xFF); |
| 2379 |
data[index + 2] = (byte) ((pixel >> 8) & 0xFF); |
| 2380 |
data[index + 3] = (byte) (pixel & 0xFF); |
| 2381 |
i++; |
| 2382 |
srcX++; |
| 2383 |
if (srcX >= width) { |
| 2384 |
srcY++; |
| 2385 |
index = srcY * bytesPerLine; |
| 2386 |
srcX = 0; |
| 2387 |
} else { |
| 2388 |
index += 4; |
| 2389 |
} |
| 2390 |
} |
| 2391 |
return; |
| 2392 |
} |
| 2393 |
SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); |
| 2394 |
} |
| 2395 |
|
| 2396 |
/** |
| 2397 |
* Returns a palette with 2 colors: black & white. |
| 2398 |
*/ |
| 2399 |
static PaletteData bwPalette() { |
| 2400 |
return new PaletteData(new RGB[] {new RGB(0, 0, 0), new RGB(255, 255, 255)}); |
| 2401 |
} |
| 2402 |
|
| 2403 |
/** |
| 2404 |
* Gets the offset of the most significant bit for |
| 2405 |
* the given mask. |
| 2406 |
*/ |
| 2407 |
static int getMSBOffset(int mask) { |
| 2408 |
for (int i = 31; i >= 0; i--) { |
| 2409 |
if (((mask >> i) & 0x1) != 0) |
| 2410 |
return i + 1; |
| 2411 |
} |
| 2412 |
return 0; |
| 2413 |
} |
| 2414 |
|
| 2415 |
/** |
| 2416 |
* Finds the closest match. |
| 2417 |
*/ |
| 2418 |
static int closestMatch(int depth, byte red, byte green, byte blue, int redMask, int greenMask, int blueMask, byte[] reds, byte[] greens, byte[] blues) { |
| 2419 |
if (depth > 8) { |
| 2420 |
int rshift = 32 - getMSBOffset(redMask); |
| 2421 |
int gshift = 32 - getMSBOffset(greenMask); |
| 2422 |
int bshift = 32 - getMSBOffset(blueMask); |
| 2423 |
return (((red << 24) >>> rshift) & redMask) | (((green << 24) >>> gshift) & greenMask) | (((blue << 24) >>> bshift) & blueMask); |
| 2424 |
} |
| 2425 |
int r, g, b; |
| 2426 |
int minDistance = 0x7fffffff; |
| 2427 |
int nearestPixel = 0; |
| 2428 |
int n = reds.length; |
| 2429 |
for (int j = 0; j < n; j++) { |
| 2430 |
r = (reds[j] & 0xFF) - (red & 0xFF); |
| 2431 |
g = (greens[j] & 0xFF) - (green & 0xFF); |
| 2432 |
b = (blues[j] & 0xFF) - (blue & 0xFF); |
| 2433 |
int distance = r * r + g * g + b * b; |
| 2434 |
if (distance < minDistance) { |
| 2435 |
nearestPixel = j; |
| 2436 |
if (distance == 0) |
| 2437 |
break; |
| 2438 |
minDistance = distance; |
| 2439 |
} |
| 2440 |
} |
| 2441 |
return nearestPixel; |
| 2442 |
} |
| 2443 |
|
| 2444 |
static final ImageData convertMask(ImageData mask) { |
| 2445 |
if (mask.depth == 1) |
| 2446 |
return mask; |
| 2447 |
PaletteData palette = new PaletteData(new RGB[] {new RGB(0, 0, 0), new RGB(255, 255, 255)}); |
| 2448 |
ImageData newMask = new ImageData(mask.width, mask.height, 1, palette); |
| 2449 |
/* Find index of black in mask palette */ |
| 2450 |
int blackIndex = 0; |
| 2451 |
RGB[] rgbs = mask.getRGBs(); |
| 2452 |
if (rgbs != null) { |
| 2453 |
while (blackIndex < rgbs.length) { |
| 2454 |
if (rgbs[blackIndex].equals(palette.colors[0])) |
| 2455 |
break; |
| 2456 |
blackIndex++; |
| 2457 |
} |
| 2458 |
} |
| 2459 |
int[] pixels = new int[mask.width]; |
| 2460 |
for (int y = 0; y < mask.height; y++) { |
| 2461 |
mask.getPixels(0, y, mask.width, pixels, 0); |
| 2462 |
for (int i = 0; i < pixels.length; i++) { |
| 2463 |
if (pixels[i] == blackIndex) { |
| 2464 |
pixels[i] = 0; |
| 2465 |
} else { |
| 2466 |
pixels[i] = 1; |
| 2467 |
} |
| 2468 |
} |
| 2469 |
newMask.setPixels(0, y, mask.width, pixels, 0); |
| 2470 |
} |
| 2471 |
return newMask; |
| 2472 |
} |
| 2473 |
|
| 2474 |
static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) { |
| 2475 |
if (pad == newPad) |
| 2476 |
return data; |
| 2477 |
int stride = (width * depth + 7) / 8; |
| 2478 |
int bpl = (stride + (pad - 1)) / pad * pad; |
| 2479 |
int newBpl = (stride + (newPad - 1)) / newPad * newPad; |
| 2480 |
byte[] newData = new byte[height * newBpl]; |
| 2481 |
int srcIndex = 0, destIndex = 0; |
| 2482 |
for (int y = 0; y < height; y++) { |
| 2483 |
System.arraycopy(data, srcIndex, newData, destIndex, stride); |
| 2484 |
srcIndex += bpl; |
| 2485 |
destIndex += newBpl; |
| 2486 |
} |
| 2487 |
return newData; |
| 2488 |
} |
| 2489 |
|
| 2490 |
/** |
| 2491 |
* Blit operation bits to be OR'ed together to specify the desired operation. |
| 2492 |
*/ |
| 2493 |
static final int BLIT_SRC = 1, // copy source directly, else applies logic operations |
| 2494 |
BLIT_ALPHA = 2, // enable alpha blending |
| 2495 |
BLIT_DITHER = 4; // enable dithering in low color modes |
| 2496 |
|
| 2497 |
/** |
| 2498 |
* Alpha mode, values 0 - 255 specify global alpha level |
| 2499 |
*/ |
| 2500 |
static final int ALPHA_OPAQUE = 255, // Fully opaque (ignores any alpha data) |
| 2501 |
ALPHA_TRANSPARENT = 0, // Fully transparent (ignores any alpha data) |
| 2502 |
ALPHA_CHANNEL_SEPARATE = -1, // Use alpha channel from separate alphaData |
| 2503 |
ALPHA_CHANNEL_SOURCE = -2, // Use alpha channel embedded in sourceData |
| 2504 |
ALPHA_MASK_UNPACKED = -3, // Use transparency mask formed by bytes in alphaData (non-zero is opaque) |
| 2505 |
ALPHA_MASK_PACKED = -4, // Use transparency mask formed by packed bits in alphaData |
| 2506 |
ALPHA_MASK_INDEX = -5, // Consider source palette indices transparent if in alphaData array |
| 2507 |
ALPHA_MASK_RGB = -6; // Consider source RGBs transparent if in RGB888 format alphaData array |
| 2508 |
|
| 2509 |
/** |
| 2510 |
* Byte and bit order constants. |
| 2511 |
*/ |
| 2512 |
static final int LSB_FIRST = 0; |
| 2513 |
static final int MSB_FIRST = 1; |
| 2514 |
|
| 2515 |
/** |
| 2516 |
* Data types (internal) |
| 2517 |
*/ |
| 2518 |
/* |
| 2519 |
private static final int |
| 2520 |
// direct / true color formats with arbitrary masks & shifts |
| 2521 |
TYPE_GENERIC_8 = 0, |
| 2522 |
TYPE_GENERIC_16_MSB = 1, |
| 2523 |
TYPE_GENERIC_16_LSB = 2, |
| 2524 |
TYPE_GENERIC_24 = 3, |
| 2525 |
TYPE_GENERIC_32_MSB = 4, |
| 2526 |
TYPE_GENERIC_32_LSB = 5, |
| 2527 |
// palette indexed color formats |
| 2528 |
TYPE_INDEX_8 = 6, |
| 2529 |
TYPE_INDEX_4 = 7, |
| 2530 |
TYPE_INDEX_2 = 8, |
| 2531 |
TYPE_INDEX_1_MSB = 9, |
| 2532 |
TYPE_INDEX_1_LSB = 10; |
| 2533 |
*/ |
| 2534 |
/** |
| 2535 |
* Computes the required channel shift from a mask. |
| 2536 |
*/ |
| 2537 |
static int getChannelShift(int mask) { |
| 2538 |
if (mask == 0) |
| 2539 |
return 0; |
| 2540 |
int i; |
| 2541 |
for (i = 0; ((mask & 1) == 0) && (i < 32); ++i) { |
| 2542 |
mask >>>= 1; |
| 2543 |
} |
| 2544 |
return i; |
| 2545 |
} |
| 2546 |
|
| 2547 |
/** |
| 2548 |
* Computes the required channel width (depth) from a mask. |
| 2549 |
*/ |
| 2550 |
static int getChannelWidth(int mask, int shift) { |
| 2551 |
if (mask == 0) |
| 2552 |
return 0; |
| 2553 |
int i; |
| 2554 |
mask >>>= shift; |
| 2555 |
for (i = shift; ((mask & 1) != 0) && (i < 32); ++i) { |
| 2556 |
mask >>>= 1; |
| 2557 |
} |
| 2558 |
return i - shift; |
| 2559 |
} |
| 2560 |
|
| 2561 |
/** |
| 2562 |
* Extracts a field from packed RGB data given a mask for that field. |
| 2563 |
*/ |
| 2564 |
static byte getChannelField(int data, int mask) { |
| 2565 |
final int shift = getChannelShift(mask); |
| 2566 |
return ANY_TO_EIGHT[getChannelWidth(mask, shift)][(data & mask) >>> shift]; |
| 2567 |
} |
| 2568 |
|
| 2569 |
/* |
| 2570 |
* Fill in dithered gradated values for a color channel |
| 2571 |
*/ |
| 2572 |
static final void buildDitheredGradientChannel(int from, int to, int steps, int bandWidth, int bandHeight, boolean vertical, byte[] bitmapData, int dp, int bytesPerLine, int bits) { |
| 2573 |
final int mask = 0xff00 >>> bits; |
| 2574 |
int val = from << 16; |
| 2575 |
final int inc = ((to << 16) - val) / steps + 1; |
| 2576 |
if (vertical) { |
| 2577 |
for (int dy = 0; dy < bandHeight; ++dy, dp += bytesPerLine) { |
| 2578 |
for (int dx = 0, dptr = dp; dx < bandWidth; ++dx, dptr += 4) { |
| 2579 |
final int thresh = DITHER_MATRIX[dy & 7][dx] >>> bits; |
| 2580 |
int temp = val + thresh; |
| 2581 |
if (temp > 0xffffff) |
| 2582 |
bitmapData[dptr] = -1; |
| 2583 |
else |
| 2584 |
bitmapData[dptr] = (byte) ((temp >>> 16) & mask); |
| 2585 |
} |
| 2586 |
val += inc; |
| 2587 |
} |
| 2588 |
} else { |
| 2589 |
for (int dx = 0; dx < bandWidth; ++dx, dp += 4) { |
| 2590 |
for (int dy = 0, dptr = dp; dy < bandHeight; ++dy, dptr += bytesPerLine) { |
| 2591 |
final int thresh = DITHER_MATRIX[dy][dx & 7] >>> bits; |
| 2592 |
int temp = val + thresh; |
| 2593 |
if (temp > 0xffffff) |
| 2594 |
bitmapData[dptr] = -1; |
| 2595 |
else |
| 2596 |
bitmapData[dptr] = (byte) ((temp >>> 16) & mask); |
| 2597 |
} |
| 2598 |
val += inc; |
| 2599 |
} |
| 2600 |
} |
| 2601 |
} |
| 2602 |
} |
| 2603 |
|
| 2604 |
static class LEDataInputStream extends InputStream { |
| 2605 |
int position; |
| 2606 |
InputStream in; |
| 2607 |
|
| 2608 |
/** |
| 2609 |
* The byte array containing the bytes to read. |
| 2610 |
*/ |
| 2611 |
protected byte[] buf; |
| 2612 |
|
| 2613 |
/** |
| 2614 |
* The current position within the byte array <code>buf</code>. A value |
| 2615 |
* equal to buf.length indicates no bytes available. A value of |
| 2616 |
* 0 indicates the buffer is full. |
| 2617 |
*/ |
| 2618 |
protected int pos; |
| 2619 |
|
| 2620 |
public LEDataInputStream(InputStream input) { |
| 2621 |
this(input, 512); |
| 2622 |
} |
| 2623 |
|
| 2624 |
public LEDataInputStream(InputStream input, int bufferSize) { |
| 2625 |
this.in = input; |
| 2626 |
if (bufferSize > 0) { |
| 2627 |
buf = new byte[bufferSize]; |
| 2628 |
pos = bufferSize; |
| 2629 |
} else |
| 2630 |
throw new IllegalArgumentException(); |
| 2631 |
} |
| 2632 |
|
| 2633 |
@Override |
| 2634 |
public void close() throws IOException { |
| 2635 |
buf = null; |
| 2636 |
if (in != null) { |
| 2637 |
in.close(); |
| 2638 |
in = null; |
| 2639 |
} |
| 2640 |
} |
| 2641 |
|
| 2642 |
/** |
| 2643 |
* Answer how many bytes were read. |
| 2644 |
*/ |
| 2645 |
public int getPosition() { |
| 2646 |
return position; |
| 2647 |
} |
| 2648 |
|
| 2649 |
/** |
| 2650 |
* Answers how many bytes are available for reading without blocking |
| 2651 |
*/ |
| 2652 |
@Override |
| 2653 |
public int available() throws IOException { |
| 2654 |
if (buf == null) |
| 2655 |
throw new IOException(); |
| 2656 |
return (buf.length - pos) + in.available(); |
| 2657 |
} |
| 2658 |
|
| 2659 |
/** |
| 2660 |
* Answer the next byte of the input stream. |
| 2661 |
*/ |
| 2662 |
@Override |
| 2663 |
public int read() throws IOException { |
| 2664 |
if (buf == null) |
| 2665 |
throw new IOException(); |
| 2666 |
position++; |
| 2667 |
if (pos < buf.length) |
| 2668 |
return (buf[pos++] & 0xFF); |
| 2669 |
return in.read(); |
| 2670 |
} |
| 2671 |
|
| 2672 |
/** |
| 2673 |
* Don't imitate the JDK behaviour of reading a random number |
| 2674 |
* of bytes when you can actually read them all. |
| 2675 |
*/ |
| 2676 |
@Override |
| 2677 |
public int read(byte b[], int off, int len) throws IOException { |
| 2678 |
int result; |
| 2679 |
int left = len; |
| 2680 |
result = readData(b, off, len); |
| 2681 |
while (true) { |
| 2682 |
if (result == -1) |
| 2683 |
return -1; |
| 2684 |
position += result; |
| 2685 |
if (result == left) |
| 2686 |
return len; |
| 2687 |
left -= result; |
| 2688 |
off += result; |
| 2689 |
result = readData(b, off, left); |
| 2690 |
} |
| 2691 |
} |
| 2692 |
|
| 2693 |
/** |
| 2694 |
* Reads at most <code>length</code> bytes from this LEDataInputStream and |
| 2695 |
* stores them in byte array <code>buffer</code> starting at <code>offset</code>. |
| 2696 |
* <p> |
| 2697 |
* Answer the number of bytes actually read or -1 if no bytes were read and |
| 2698 |
* end of stream was encountered. This implementation reads bytes from |
| 2699 |
* the pushback buffer first, then the target stream if more bytes are required |
| 2700 |
* to satisfy <code>count</code>. |
| 2701 |
* </p> |
| 2702 |
* @param buffer the byte array in which to store the read bytes. |
| 2703 |
* @param offset the offset in <code>buffer</code> to store the read bytes. |
| 2704 |
* @param length the maximum number of bytes to store in <code>buffer</code>. |
| 2705 |
* |
| 2706 |
* @return int the number of bytes actually read or -1 if end of stream. |
| 2707 |
* |
| 2708 |
* @exception java.io.IOException if an IOException occurs. |
| 2709 |
*/ |
| 2710 |
private int readData(byte[] buffer, int offset, int length) throws IOException { |
| 2711 |
if (buf == null) |
| 2712 |
throw new IOException(); |
| 2713 |
if (offset < 0 || offset > buffer.length || length < 0 || (length > buffer.length - offset)) { |
| 2714 |
throw new ArrayIndexOutOfBoundsException(); |
| 2715 |
} |
| 2716 |
|
| 2717 |
int cacheCopied = 0; |
| 2718 |
int newOffset = offset; |
| 2719 |
|
| 2720 |
// Are there pushback bytes available? |
| 2721 |
int available = buf.length - pos; |
| 2722 |
if (available > 0) { |
| 2723 |
cacheCopied = (available >= length) ? length : available; |
| 2724 |
System.arraycopy(buf, pos, buffer, newOffset, cacheCopied); |
| 2725 |
newOffset += cacheCopied; |
| 2726 |
pos += cacheCopied; |
| 2727 |
} |
| 2728 |
|
| 2729 |
// Have we copied enough? |
| 2730 |
if (cacheCopied == length) |
| 2731 |
return length; |
| 2732 |
|
| 2733 |
int inCopied = in.read(buffer, newOffset, length - cacheCopied); |
| 2734 |
|
| 2735 |
if (inCopied > 0) |
| 2736 |
return inCopied + cacheCopied; |
| 2737 |
if (cacheCopied == 0) |
| 2738 |
return inCopied; |
| 2739 |
return cacheCopied; |
| 2740 |
} |
| 2741 |
|
| 2742 |
/** |
| 2743 |
* Answer an integer comprised of the next |
| 2744 |
* four bytes of the input stream. |
| 2745 |
*/ |
| 2746 |
public int readInt() throws IOException { |
| 2747 |
byte[] buf = new byte[4]; |
| 2748 |
read(buf); |
| 2749 |
return ((((((buf[3] & 0xFF) << 8) | (buf[2] & 0xFF)) << 8) | (buf[1] & 0xFF)) << 8) | (buf[0] & 0xFF); |
| 2750 |
} |
| 2751 |
|
| 2752 |
/** |
| 2753 |
* Answer a short comprised of the next |
| 2754 |
* two bytes of the input stream. |
| 2755 |
*/ |
| 2756 |
public short readShort() throws IOException { |
| 2757 |
byte[] buf = new byte[2]; |
| 2758 |
read(buf); |
| 2759 |
return (short) (((buf[1] & 0xFF) << 8) | (buf[0] & 0xFF)); |
| 2760 |
} |
| 2761 |
|
| 2762 |
/** |
| 2763 |
* Push back the entire content of the given buffer <code>b</code>. |
| 2764 |
* <p> |
| 2765 |
* The bytes are pushed so that they would be read back b[0], b[1], etc. |
| 2766 |
* If the push back buffer cannot handle the bytes copied from <code>b</code>, |
| 2767 |
* an IOException will be thrown and no byte will be pushed back. |
| 2768 |
* </p> |
| 2769 |
* |
| 2770 |
* @param b the byte array containing bytes to push back into the stream |
| 2771 |
* |
| 2772 |
* @exception java.io.IOException if the pushback buffer is too small |
| 2773 |
*/ |
| 2774 |
public void unread(byte[] b) throws IOException { |
| 2775 |
int length = b.length; |
| 2776 |
if (length > pos) |
| 2777 |
throw new IOException(); |
| 2778 |
position -= length; |
| 2779 |
pos -= length; |
| 2780 |
System.arraycopy(b, 0, buf, pos, length); |
| 2781 |
} |
| 2782 |
} |
| 2783 |
|
| 2784 |
public static abstract class FileFormat { |
| 2785 |
LEDataInputStream inputStream; |
| 2786 |
ImageLoader loader; |
| 2787 |
int compression; |
| 2788 |
|
| 2789 |
byte[] bitInvertData(byte[] data, int startIndex, int endIndex) { |
| 2790 |
// Destructively bit invert data in the given byte array. |
| 2791 |
for (int i = startIndex; i < endIndex; i++) { |
| 2792 |
data[i] = (byte) (255 - data[i - startIndex]); |
| 2793 |
} |
| 2794 |
return data; |
| 2795 |
} |
| 2796 |
|
| 2797 |
/** |
| 2798 |
* Return whether or not the specified input stream |
| 2799 |
* represents a supported file format. |
| 2800 |
*/ |
| 2801 |
abstract boolean isFileFormat(LEDataInputStream stream); |
| 2802 |
|
| 2803 |
abstract ImageData[] loadFromByteStream(); |
| 2804 |
|
| 2805 |
public ImageData[] loadFromStream(LEDataInputStream stream) { |
| 2806 |
inputStream = stream; |
| 2807 |
return loadFromByteStream(); |
| 2808 |
} |
| 2809 |
|
| 2810 |
public static ImageData[] load(InputStream is, ImageLoader loader) { |
| 2811 |
LEDataInputStream stream = new LEDataInputStream(is); |
| 2812 |
boolean isSupported = false; |
| 2813 |
FileFormat fileFormat = new WinICOFileFormat(); |
| 2814 |
if (fileFormat.isFileFormat(stream)) |
| 2815 |
isSupported = true; |
| 2816 |
else { |
| 2817 |
fileFormat = new WinBMPFileFormat(); |
| 2818 |
if (fileFormat.isFileFormat(stream)) |
| 2819 |
isSupported = true; |
| 2820 |
} |
| 2821 |
if (!isSupported) |
| 2822 |
SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); |
| 2823 |
fileFormat.loader = loader; |
| 2824 |
return fileFormat.loadFromStream(stream); |
| 2825 |
} |
| 2826 |
} |
| 2827 |
|
| 2828 |
static class WinBMPFileFormat extends FileFormat { |
| 2829 |
static final int BMPFileHeaderSize = 14; |
| 2830 |
static final int BMPHeaderFixedSize = 40; |
| 2831 |
int importantColors; |
| 2832 |
|
| 2833 |
void decompressData(byte[] src, byte[] dest, int stride, int cmp) { |
| 2834 |
if (cmp == 1) { // BMP_RLE8_COMPRESSION |
| 2835 |
if (decompressRLE8Data(src, src.length, stride, dest, dest.length) <= 0) |
| 2836 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 2837 |
return; |
| 2838 |
} |
| 2839 |
if (cmp == 2) { // BMP_RLE4_COMPRESSION |
| 2840 |
if (decompressRLE4Data(src, src.length, stride, dest, dest.length) <= 0) |
| 2841 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 2842 |
return; |
| 2843 |
} |
| 2844 |
throw new IllegalArgumentException(MessageFormat.format( |
| 2845 |
"only BMP_RLE8_COMPRESSION(cmp=1) or BMP_RLE4_COMPRESSION(cmp=2) compression is currently supported (given cmp={0})", //$NON-NLS-1$ |
| 2846 |
cmp)); |
| 2847 |
} |
| 2848 |
|
| 2849 |
int decompressRLE4Data(byte[] src, int numBytes, int stride, byte[] dest, int destSize) { |
| 2850 |
int sp = 0; |
| 2851 |
int se = numBytes; |
| 2852 |
int dp = 0; |
| 2853 |
int de = destSize; |
| 2854 |
int x = 0, y = 0; |
| 2855 |
while (sp < se) { |
| 2856 |
int len = src[sp] & 0xFF; |
| 2857 |
sp++; |
| 2858 |
if (len == 0) { |
| 2859 |
len = src[sp] & 0xFF; |
| 2860 |
sp++; |
| 2861 |
switch (len) { |
| 2862 |
case 0 : /* end of line */ |
| 2863 |
y++; |
| 2864 |
x = 0; |
| 2865 |
dp = y * stride; |
| 2866 |
if (dp >= de) |
| 2867 |
return -1; |
| 2868 |
break; |
| 2869 |
case 1 : /* end of bitmap */ |
| 2870 |
return 1; |
| 2871 |
case 2 : /* delta */ |
| 2872 |
x += src[sp] & 0xFF; |
| 2873 |
sp++; |
| 2874 |
y += src[sp] & 0xFF; |
| 2875 |
sp++; |
| 2876 |
dp = y * stride + x / 2; |
| 2877 |
if (dp >= de) |
| 2878 |
return -1; |
| 2879 |
break; |
| 2880 |
default : /* absolute mode run */ |
| 2881 |
if ((len & 1) != 0) /* odd run lengths not currently supported */ |
| 2882 |
return -1; |
| 2883 |
x += len; |
| 2884 |
len = len / 2; |
| 2885 |
if (len > (se - sp)) |
| 2886 |
return -1; |
| 2887 |
if (len > (de - dp)) |
| 2888 |
return -1; |
| 2889 |
for (int i = 0; i < len; i++) { |
| 2890 |
dest[dp] = src[sp]; |
| 2891 |
dp++; |
| 2892 |
sp++; |
| 2893 |
} |
| 2894 |
if ((sp & 1) != 0) |
| 2895 |
sp++; /* word align sp? */ |
| 2896 |
break; |
| 2897 |
} |
| 2898 |
} else { |
| 2899 |
if ((len & 1) != 0) |
| 2900 |
return -1; |
| 2901 |
x += len; |
| 2902 |
len = len / 2; |
| 2903 |
byte theByte = src[sp]; |
| 2904 |
sp++; |
| 2905 |
if (len > (de - dp)) |
| 2906 |
return -1; |
| 2907 |
for (int i = 0; i < len; i++) { |
| 2908 |
dest[dp] = theByte; |
| 2909 |
dp++; |
| 2910 |
} |
| 2911 |
} |
| 2912 |
} |
| 2913 |
return 1; |
| 2914 |
} |
| 2915 |
|
| 2916 |
int decompressRLE8Data(byte[] src, int numBytes, int stride, byte[] dest, int destSize) { |
| 2917 |
int sp = 0; |
| 2918 |
int se = numBytes; |
| 2919 |
int dp = 0; |
| 2920 |
int de = destSize; |
| 2921 |
int x = 0, y = 0; |
| 2922 |
while (sp < se) { |
| 2923 |
int len = src[sp] & 0xFF; |
| 2924 |
sp++; |
| 2925 |
if (len == 0) { |
| 2926 |
len = src[sp] & 0xFF; |
| 2927 |
sp++; |
| 2928 |
switch (len) { |
| 2929 |
case 0 : /* end of line */ |
| 2930 |
y++; |
| 2931 |
x = 0; |
| 2932 |
dp = y * stride; |
| 2933 |
if (dp >= de) |
| 2934 |
return -1; |
| 2935 |
break; |
| 2936 |
case 1 : /* end of bitmap */ |
| 2937 |
return 1; |
| 2938 |
case 2 : /* delta */ |
| 2939 |
x += src[sp] & 0xFF; |
| 2940 |
sp++; |
| 2941 |
y += src[sp] & 0xFF; |
| 2942 |
sp++; |
| 2943 |
dp = y * stride + x; |
| 2944 |
if (dp >= de) |
| 2945 |
return -1; |
| 2946 |
break; |
| 2947 |
default : /* absolute mode run */ |
| 2948 |
if (len > (se - sp)) |
| 2949 |
return -1; |
| 2950 |
if (len > (de - dp)) |
| 2951 |
return -1; |
| 2952 |
for (int i = 0; i < len; i++) { |
| 2953 |
dest[dp] = src[sp]; |
| 2954 |
dp++; |
| 2955 |
sp++; |
| 2956 |
} |
| 2957 |
if ((sp & 1) != 0) |
| 2958 |
sp++; /* word align sp? */ |
| 2959 |
x += len; |
| 2960 |
break; |
| 2961 |
} |
| 2962 |
} else { |
| 2963 |
byte theByte = src[sp]; |
| 2964 |
sp++; |
| 2965 |
if (len > (de - dp)) |
| 2966 |
return -1; |
| 2967 |
for (int i = 0; i < len; i++) { |
| 2968 |
dest[dp] = theByte; |
| 2969 |
dp++; |
| 2970 |
} |
| 2971 |
x += len; |
| 2972 |
} |
| 2973 |
} |
| 2974 |
return 1; |
| 2975 |
} |
| 2976 |
|
| 2977 |
@Override |
| 2978 |
boolean isFileFormat(LEDataInputStream stream) { |
| 2979 |
try { |
| 2980 |
byte[] header = new byte[18]; |
| 2981 |
stream.read(header); |
| 2982 |
stream.unread(header); |
| 2983 |
int infoHeaderSize = (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) | ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24); |
| 2984 |
return header[0] == 0x42 && header[1] == 0x4D && infoHeaderSize >= BMPHeaderFixedSize; |
| 2985 |
} catch (Exception e) { |
| 2986 |
return false; |
| 2987 |
} |
| 2988 |
} |
| 2989 |
|
| 2990 |
byte[] loadData(byte[] infoHeader) { |
| 2991 |
int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); |
| 2992 |
int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); |
| 2993 |
int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); |
| 2994 |
int stride = (width * bitCount + 7) / 8; |
| 2995 |
stride = (stride + 3) / 4 * 4; // Round up to 4 byte multiple |
| 2996 |
byte[] data = loadData(infoHeader, stride); |
| 2997 |
flipScanLines(data, stride, height); |
| 2998 |
return data; |
| 2999 |
} |
| 3000 |
|
| 3001 |
byte[] loadData(byte[] infoHeader, int stride) { |
| 3002 |
int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); |
| 3003 |
int dataSize = height * stride; |
| 3004 |
byte[] data = new byte[dataSize]; |
| 3005 |
int cmp = (infoHeader[16] & 0xFF) | ((infoHeader[17] & 0xFF) << 8) | ((infoHeader[18] & 0xFF) << 16) | ((infoHeader[19] & 0xFF) << 24); |
| 3006 |
if (cmp == 0) { // BMP_NO_COMPRESSION |
| 3007 |
try { |
| 3008 |
if (inputStream.read(data) != dataSize) |
| 3009 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3010 |
} catch (IOException e) { |
| 3011 |
SWT.error(SWT.ERROR_IO, e); |
| 3012 |
} |
| 3013 |
} else { |
| 3014 |
int compressedSize = (infoHeader[20] & 0xFF) | ((infoHeader[21] & 0xFF) << 8) | ((infoHeader[22] & 0xFF) << 16) | ((infoHeader[23] & 0xFF) << 24); |
| 3015 |
byte[] compressed = new byte[compressedSize]; |
| 3016 |
try { |
| 3017 |
if (inputStream.read(compressed) != compressedSize) |
| 3018 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3019 |
} catch (IOException e) { |
| 3020 |
SWT.error(SWT.ERROR_IO, e); |
| 3021 |
} |
| 3022 |
decompressData(compressed, data, stride, cmp); |
| 3023 |
} |
| 3024 |
return data; |
| 3025 |
} |
| 3026 |
|
| 3027 |
int[] loadFileHeader() { |
| 3028 |
int[] header = new int[5]; |
| 3029 |
try { |
| 3030 |
header[0] = inputStream.readShort(); |
| 3031 |
header[1] = inputStream.readInt(); |
| 3032 |
header[2] = inputStream.readShort(); |
| 3033 |
header[3] = inputStream.readShort(); |
| 3034 |
header[4] = inputStream.readInt(); |
| 3035 |
} catch (IOException e) { |
| 3036 |
SWT.error(SWT.ERROR_IO, e); |
| 3037 |
} |
| 3038 |
if (header[0] != 0x4D42) |
| 3039 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3040 |
return header; |
| 3041 |
} |
| 3042 |
|
| 3043 |
@Override |
| 3044 |
ImageData[] loadFromByteStream() { |
| 3045 |
int[] fileHeader = loadFileHeader(); |
| 3046 |
byte[] infoHeader = new byte[BMPHeaderFixedSize]; |
| 3047 |
try { |
| 3048 |
inputStream.read(infoHeader); |
| 3049 |
} catch (Exception e) { |
| 3050 |
SWT.error(SWT.ERROR_IO, e); |
| 3051 |
} |
| 3052 |
int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); |
| 3053 |
int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); |
| 3054 |
int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); |
| 3055 |
PaletteData palette = loadPalette(infoHeader); |
| 3056 |
if (inputStream.getPosition() < fileHeader[4]) { |
| 3057 |
// Seek to the specified offset |
| 3058 |
try { |
| 3059 |
inputStream.skip(fileHeader[4] - inputStream.getPosition()); |
| 3060 |
} catch (IOException e) { |
| 3061 |
SWT.error(SWT.ERROR_IO, e); |
| 3062 |
} |
| 3063 |
} |
| 3064 |
byte[] data = loadData(infoHeader); |
| 3065 |
this.compression = (infoHeader[16] & 0xFF) | ((infoHeader[17] & 0xFF) << 8) | ((infoHeader[18] & 0xFF) << 16) | ((infoHeader[19] & 0xFF) << 24); |
| 3066 |
this.importantColors = (infoHeader[36] & 0xFF) | ((infoHeader[37] & 0xFF) << 8) | ((infoHeader[38] & 0xFF) << 16) | ((infoHeader[39] & 0xFF) << 24); |
| 3067 |
// int xPelsPerMeter = (infoHeader[24] & 0xFF) | ((infoHeader[25] & 0xFF) << 8) | ((infoHeader[26] & 0xFF) << 16) | ((infoHeader[27] & 0xFF) << 24); |
| 3068 |
// int yPelsPerMeter = (infoHeader[28] & 0xFF) | ((infoHeader[29] & 0xFF) << 8) | ((infoHeader[30] & 0xFF) << 16) | ((infoHeader[31] & 0xFF) << 24); |
| 3069 |
int type = (this.compression == 1 /*BMP_RLE8_COMPRESSION*/) || (this.compression == 2 /*BMP_RLE4_COMPRESSION*/) ? SWT.IMAGE_BMP_RLE : SWT.IMAGE_BMP; |
| 3070 |
return new ImageData[] {ImageData.internal_new(width, height, bitCount, palette, 4, data, 0, null, null, -1, -1, type, 0, 0, 0, 0)}; |
| 3071 |
} |
| 3072 |
|
| 3073 |
PaletteData loadPalette(byte[] infoHeader) { |
| 3074 |
int depth = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); |
| 3075 |
if (depth <= 8) { |
| 3076 |
int numColors = (infoHeader[32] & 0xFF) | ((infoHeader[33] & 0xFF) << 8) | ((infoHeader[34] & 0xFF) << 16) | ((infoHeader[35] & 0xFF) << 24); |
| 3077 |
if (numColors == 0) { |
| 3078 |
numColors = 1 << depth; |
| 3079 |
} else { |
| 3080 |
if (numColors > 256) |
| 3081 |
numColors = 256; |
| 3082 |
} |
| 3083 |
byte[] buf = new byte[numColors * 4]; |
| 3084 |
try { |
| 3085 |
if (inputStream.read(buf) != buf.length) |
| 3086 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3087 |
} catch (IOException e) { |
| 3088 |
SWT.error(SWT.ERROR_IO, e); |
| 3089 |
} |
| 3090 |
return paletteFromBytes(buf, numColors); |
| 3091 |
} |
| 3092 |
if (depth == 16) |
| 3093 |
return new PaletteData(0x7C00, 0x3E0, 0x1F); |
| 3094 |
if (depth == 24) |
| 3095 |
return new PaletteData(0xFF, 0xFF00, 0xFF0000); |
| 3096 |
return new PaletteData(0xFF00, 0xFF0000, 0xFF000000); |
| 3097 |
} |
| 3098 |
|
| 3099 |
PaletteData paletteFromBytes(byte[] bytes, int numColors) { |
| 3100 |
int bytesOffset = 0; |
| 3101 |
RGB[] colors = new RGB[numColors]; |
| 3102 |
for (int i = 0; i < numColors; i++) { |
| 3103 |
colors[i] = new RGB(bytes[bytesOffset + 2] & 0xFF, bytes[bytesOffset + 1] & 0xFF, bytes[bytesOffset] & 0xFF); |
| 3104 |
bytesOffset += 4; |
| 3105 |
} |
| 3106 |
return new PaletteData(colors); |
| 3107 |
} |
| 3108 |
|
| 3109 |
/** |
| 3110 |
* Answer a byte array containing the BMP representation of |
| 3111 |
* the given device independent palette. |
| 3112 |
*/ |
| 3113 |
static byte[] paletteToBytes(PaletteData pal) { |
| 3114 |
int n = pal.colors == null ? 0 : (pal.colors.length < 256 ? pal.colors.length : 256); |
| 3115 |
byte[] bytes = new byte[n * 4]; |
| 3116 |
int offset = 0; |
| 3117 |
for (int i = 0; i < n; i++) { |
| 3118 |
RGB col = pal.colors[i]; |
| 3119 |
bytes[offset] = (byte) col.blue; |
| 3120 |
bytes[offset + 1] = (byte) col.green; |
| 3121 |
bytes[offset + 2] = (byte) col.red; |
| 3122 |
offset += 4; |
| 3123 |
} |
| 3124 |
return bytes; |
| 3125 |
} |
| 3126 |
|
| 3127 |
void flipScanLines(byte[] data, int stride, int height) { |
| 3128 |
int i1 = 0; |
| 3129 |
int i2 = (height - 1) * stride; |
| 3130 |
for (int i = 0; i < height / 2; i++) { |
| 3131 |
for (int index = 0; index < stride; index++) { |
| 3132 |
byte b = data[index + i1]; |
| 3133 |
data[index + i1] = data[index + i2]; |
| 3134 |
data[index + i2] = b; |
| 3135 |
} |
| 3136 |
i1 += stride; |
| 3137 |
i2 -= stride; |
| 3138 |
} |
| 3139 |
} |
| 3140 |
|
| 3141 |
} |
| 3142 |
|
| 3143 |
static class WinICOFileFormat extends FileFormat { |
| 3144 |
|
| 3145 |
static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) { |
| 3146 |
if (pad == newPad) |
| 3147 |
return data; |
| 3148 |
int stride = (width * depth + 7) / 8; |
| 3149 |
int bpl = (stride + (pad - 1)) / pad * pad; |
| 3150 |
int newBpl = (stride + (newPad - 1)) / newPad * newPad; |
| 3151 |
byte[] newData = new byte[height * newBpl]; |
| 3152 |
int srcIndex = 0, destIndex = 0; |
| 3153 |
for (int y = 0; y < height; y++) { |
| 3154 |
System.arraycopy(data, srcIndex, newData, destIndex, newBpl); |
| 3155 |
srcIndex += bpl; |
| 3156 |
destIndex += newBpl; |
| 3157 |
} |
| 3158 |
return newData; |
| 3159 |
} |
| 3160 |
|
| 3161 |
/** |
| 3162 |
* Answer the size in bytes of the file representation of the given |
| 3163 |
* icon |
| 3164 |
*/ |
| 3165 |
int iconSize(ImageData i) { |
| 3166 |
int shapeDataStride = (i.width * i.depth + 31) / 32 * 4; |
| 3167 |
int maskDataStride = (i.width + 31) / 32 * 4; |
| 3168 |
int dataSize = (shapeDataStride + maskDataStride) * i.height; |
| 3169 |
int paletteSize = i.palette.colors != null ? i.palette.colors.length * 4 : 0; |
| 3170 |
return WinBMPFileFormat.BMPHeaderFixedSize + paletteSize + dataSize; |
| 3171 |
} |
| 3172 |
|
| 3173 |
@Override |
| 3174 |
boolean isFileFormat(LEDataInputStream stream) { |
| 3175 |
try { |
| 3176 |
byte[] header = new byte[4]; |
| 3177 |
stream.read(header); |
| 3178 |
stream.unread(header); |
| 3179 |
return header[0] == 0 && header[1] == 0 && header[2] == 1 && header[3] == 0; |
| 3180 |
} catch (Exception e) { |
| 3181 |
return false; |
| 3182 |
} |
| 3183 |
} |
| 3184 |
|
| 3185 |
boolean isValidIcon(ImageData i) { |
| 3186 |
switch (i.depth) { |
| 3187 |
case 1 : |
| 3188 |
case 4 : |
| 3189 |
case 8 : |
| 3190 |
if (i.palette.isDirect) |
| 3191 |
return false; |
| 3192 |
int size = i.palette.colors.length; |
| 3193 |
return size == 2 || size == 16 || size == 32 || size == 256; |
| 3194 |
case 24 : |
| 3195 |
case 32 : |
| 3196 |
return i.palette.isDirect; |
| 3197 |
} |
| 3198 |
return false; |
| 3199 |
} |
| 3200 |
|
| 3201 |
int loadFileHeader(LEDataInputStream byteStream) { |
| 3202 |
int[] fileHeader = new int[3]; |
| 3203 |
try { |
| 3204 |
fileHeader[0] = byteStream.readShort(); |
| 3205 |
fileHeader[1] = byteStream.readShort(); |
| 3206 |
fileHeader[2] = byteStream.readShort(); |
| 3207 |
} catch (IOException e) { |
| 3208 |
SWT.error(SWT.ERROR_IO, e); |
| 3209 |
} |
| 3210 |
if ((fileHeader[0] != 0) || (fileHeader[1] != 1)) |
| 3211 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3212 |
int numIcons = fileHeader[2]; |
| 3213 |
if (numIcons <= 0) |
| 3214 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3215 |
return numIcons; |
| 3216 |
} |
| 3217 |
|
| 3218 |
int loadFileHeader(LEDataInputStream byteStream, boolean hasHeader) { |
| 3219 |
int[] fileHeader = new int[3]; |
| 3220 |
try { |
| 3221 |
if (hasHeader) { |
| 3222 |
fileHeader[0] = byteStream.readShort(); |
| 3223 |
fileHeader[1] = byteStream.readShort(); |
| 3224 |
} else { |
| 3225 |
fileHeader[0] = 0; |
| 3226 |
fileHeader[1] = 1; |
| 3227 |
} |
| 3228 |
fileHeader[2] = byteStream.readShort(); |
| 3229 |
} catch (IOException e) { |
| 3230 |
SWT.error(SWT.ERROR_IO, e); |
| 3231 |
} |
| 3232 |
if ((fileHeader[0] != 0) || (fileHeader[1] != 1)) |
| 3233 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3234 |
int numIcons = fileHeader[2]; |
| 3235 |
if (numIcons <= 0) |
| 3236 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3237 |
return numIcons; |
| 3238 |
} |
| 3239 |
|
| 3240 |
@Override |
| 3241 |
ImageData[] loadFromByteStream() { |
| 3242 |
int numIcons = loadFileHeader(inputStream); |
| 3243 |
int[][] headers = loadIconHeaders(numIcons); |
| 3244 |
ImageData[] icons = new ImageData[headers.length]; |
| 3245 |
for (int i = 0; i < icons.length; i++) { |
| 3246 |
icons[i] = loadIcon(headers[i]); |
| 3247 |
} |
| 3248 |
return icons; |
| 3249 |
} |
| 3250 |
|
| 3251 |
/** |
| 3252 |
* Load one icon from the byte stream. |
| 3253 |
*/ |
| 3254 |
ImageData loadIcon(int[] iconHeader) { |
| 3255 |
byte[] infoHeader = loadInfoHeader(iconHeader); |
| 3256 |
WinBMPFileFormat bmpFormat = new WinBMPFileFormat(); |
| 3257 |
bmpFormat.inputStream = inputStream; |
| 3258 |
PaletteData palette = bmpFormat.loadPalette(infoHeader); |
| 3259 |
byte[] shapeData = bmpFormat.loadData(infoHeader); |
| 3260 |
int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); |
| 3261 |
int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); |
| 3262 |
int depth = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); |
| 3263 |
infoHeader[14] = 1; |
| 3264 |
infoHeader[15] = 0; |
| 3265 |
byte[] maskData = bmpFormat.loadData(infoHeader); |
| 3266 |
maskData = convertPad(maskData, width, height, 1, 4, 2); |
| 3267 |
bitInvertData(maskData, 0, maskData.length); |
| 3268 |
return ImageData.internal_new(width, height, depth, palette, 4, shapeData, 2, maskData, null, -1, -1, SWT.IMAGE_ICO, 0, 0, 0, 0); |
| 3269 |
} |
| 3270 |
|
| 3271 |
int[][] loadIconHeaders(int numIcons) { |
| 3272 |
int[][] headers = new int[numIcons][7]; |
| 3273 |
try { |
| 3274 |
for (int i = 0; i < numIcons; i++) { |
| 3275 |
headers[i][0] = inputStream.read(); |
| 3276 |
headers[i][1] = inputStream.read(); |
| 3277 |
headers[i][2] = inputStream.readShort(); |
| 3278 |
headers[i][3] = inputStream.readShort(); |
| 3279 |
headers[i][4] = inputStream.readShort(); |
| 3280 |
headers[i][5] = inputStream.readInt(); |
| 3281 |
headers[i][6] = inputStream.readInt(); |
| 3282 |
} |
| 3283 |
} catch (IOException e) { |
| 3284 |
SWT.error(SWT.ERROR_IO, e); |
| 3285 |
} |
| 3286 |
return headers; |
| 3287 |
} |
| 3288 |
|
| 3289 |
byte[] loadInfoHeader(int[] iconHeader) { |
| 3290 |
int width = iconHeader[0]; |
| 3291 |
int height = iconHeader[1]; |
| 3292 |
int numColors = iconHeader[2]; // the number of colors is in the low byte, but the high byte must be 0 |
| 3293 |
if (numColors == 0) |
| 3294 |
numColors = 256; // this is specified: '00' represents '256' (0x100) colors |
| 3295 |
if ((numColors != 2) && (numColors != 8) && (numColors != 16) && (numColors != 32) && (numColors != 256)) |
| 3296 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3297 |
if (inputStream.getPosition() < iconHeader[6]) { |
| 3298 |
// Seek to the specified offset |
| 3299 |
try { |
| 3300 |
inputStream.skip(iconHeader[6] - inputStream.getPosition()); |
| 3301 |
} catch (IOException e) { |
| 3302 |
SWT.error(SWT.ERROR_IO, e); |
| 3303 |
return null; |
| 3304 |
} |
| 3305 |
} |
| 3306 |
byte[] infoHeader = new byte[WinBMPFileFormat.BMPHeaderFixedSize]; |
| 3307 |
try { |
| 3308 |
inputStream.read(infoHeader); |
| 3309 |
} catch (IOException e) { |
| 3310 |
SWT.error(SWT.ERROR_IO, e); |
| 3311 |
} |
| 3312 |
if (((infoHeader[12] & 0xFF) | ((infoHeader[13] & 0xFF) << 8)) != 1) |
| 3313 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3314 |
int infoWidth = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); |
| 3315 |
int infoHeight = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); |
| 3316 |
int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); |
| 3317 |
/* |
| 3318 |
* Feature in the ico spec. The spec says that a width/height of 0 represents 256, however, newer images can be created with even larger sizes. |
| 3319 |
* Images with a width/height >= 256 will have their width/height set to 0 in the icon header; the fix for this case is to read the width/height |
| 3320 |
* directly from the image header. |
| 3321 |
*/ |
| 3322 |
if (width == 0) |
| 3323 |
width = infoWidth; |
| 3324 |
if (height == 0) |
| 3325 |
height = infoHeight / 2; |
| 3326 |
if (height == infoHeight && bitCount == 1) |
| 3327 |
height /= 2; |
| 3328 |
if (!((width == infoWidth) && (height * 2 == infoHeight) && (bitCount == 1 || bitCount == 4 || bitCount == 8 || bitCount == 24 || bitCount == 32))) |
| 3329 |
SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 3330 |
infoHeader[8] = (byte) (height & 0xFF); |
| 3331 |
infoHeader[9] = (byte) ((height >> 8) & 0xFF); |
| 3332 |
infoHeader[10] = (byte) ((height >> 16) & 0xFF); |
| 3333 |
infoHeader[11] = (byte) ((height >> 24) & 0xFF); |
| 3334 |
return infoHeader; |
| 3335 |
} |
| 3336 |
} |
| 3337 |
|
| 3338 |
static class SWT { |
| 3339 |
public static final int IMAGE_ICO = 3; |
| 3340 |
public static final int ERROR_IO = 39; |
| 3341 |
public static final int ERROR_INVALID_IMAGE = 40; |
| 3342 |
public static final int ERROR_NULL_ARGUMENT = 4; |
| 3343 |
public static final int ERROR_INVALID_ARGUMENT = 5; |
| 3344 |
public static final int ERROR_CANNOT_BE_ZERO = 7; |
| 3345 |
public static final int IMAGE_UNDEFINED = -1; |
| 3346 |
public static final int ERROR_UNSUPPORTED_DEPTH = 38; |
| 3347 |
public static final int TRANSPARENCY_MASK = 1 << 1; |
| 3348 |
public static final int ERROR_UNSUPPORTED_FORMAT = 42; |
| 3349 |
public static final int TRANSPARENCY_ALPHA = 1 << 0; |
| 3350 |
public static final int TRANSPARENCY_NONE = 0x0; |
| 3351 |
public static final int TRANSPARENCY_PIXEL = 1 << 2; |
| 3352 |
public static final int IMAGE_BMP = 0; |
| 3353 |
public static final int IMAGE_BMP_RLE = 1; |
| 3354 |
|
| 3355 |
public static void error(int code) { |
| 3356 |
throw new RuntimeException("Error " + code); //$NON-NLS-1$ |
| 3357 |
} |
| 3358 |
|
| 3359 |
public static void error(int code, Throwable t) { |
| 3360 |
throw new RuntimeException(t); |
| 3361 |
} |
| 3362 |
} |
| 3363 |
} |