PageRenderTime 39ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java

https://github.com/skovatch/org.eclipse.swt
Java | 3673 lines | 2670 code | 121 blank | 882 comment | 785 complexity | b656a716b49b1fd3c15da18f8f757e1a MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2009 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.swt.graphics;
  12. import java.io.*;
  13. import org.eclipse.swt.*;
  14. import org.eclipse.swt.internal.CloneableCompatibility;
  15. /**
  16. * Instances of this class are device-independent descriptions
  17. * of images. They are typically used as an intermediate format
  18. * between loading from or writing to streams and creating an
  19. * <code>Image</code>.
  20. * <p>
  21. * Note that the public fields <code>x</code>, <code>y</code>,
  22. * <code>disposalMethod</code> and <code>delayTime</code> are
  23. * typically only used when the image is in a set of images used
  24. * for animation.
  25. * </p>
  26. *
  27. * @see Image
  28. * @see ImageLoader
  29. * @see <a href="http://www.eclipse.org/swt/snippets/#image">ImageData snippets</a>
  30. * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ImageAnalyzer</a>
  31. * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
  32. */
  33. public final class ImageData implements CloneableCompatibility {
  34. /**
  35. * The width of the image, in pixels.
  36. */
  37. public int width;
  38. /**
  39. * The height of the image, in pixels.
  40. */
  41. public int height;
  42. /**
  43. * The color depth of the image, in bits per pixel.
  44. * <p>
  45. * Note that a depth of 8 or less does not necessarily
  46. * mean that the image is palette indexed, or
  47. * conversely that a depth greater than 8 means that
  48. * the image is direct color. Check the associated
  49. * PaletteData's isDirect field for such determinations.
  50. */
  51. public int depth;
  52. /**
  53. * The scanline padding.
  54. * <p>
  55. * If one scanline of the image is not a multiple of
  56. * this number, it will be padded with zeros until it is.
  57. * </p>
  58. */
  59. public int scanlinePad;
  60. /**
  61. * The number of bytes per scanline.
  62. * <p>
  63. * This is a multiple of the scanline padding.
  64. * </p>
  65. */
  66. public int bytesPerLine;
  67. /**
  68. * The pixel data of the image.
  69. * <p>
  70. * Note that for 16 bit depth images the pixel data is stored
  71. * in least significant byte order; however, for 24bit and
  72. * 32bit depth images the pixel data is stored in most
  73. * significant byte order.
  74. * </p>
  75. */
  76. public byte[] data;
  77. /**
  78. * The color table for the image.
  79. */
  80. public PaletteData palette;
  81. /**
  82. * The transparent pixel.
  83. * <p>
  84. * Pixels with this value are transparent.
  85. * </p><p>
  86. * The default is -1 which means 'no transparent pixel'.
  87. * </p>
  88. */
  89. public int transparentPixel;
  90. /**
  91. * An icon-specific field containing the data from the icon mask.
  92. * <p>
  93. * This is a 1 bit bitmap stored with the most significant
  94. * bit first. The number of bytes per scanline is
  95. * '((width + 7) / 8 + (maskPad - 1)) / maskPad * maskPad'.
  96. * </p><p>
  97. * The default is null which means 'no transparency mask'.
  98. * </p>
  99. */
  100. public byte[] maskData;
  101. /**
  102. * An icon-specific field containing the scanline pad of the mask.
  103. * <p>
  104. * If one scanline of the transparency mask is not a
  105. * multiple of this number, it will be padded with zeros until
  106. * it is.
  107. * </p>
  108. */
  109. public int maskPad;
  110. /**
  111. * The alpha data of the image.
  112. * <p>
  113. * Every pixel can have an <em>alpha blending</em> value that
  114. * varies from 0, meaning fully transparent, to 255 meaning
  115. * fully opaque. The number of bytes per scanline is
  116. * 'width'.
  117. * </p>
  118. */
  119. public byte[] alphaData;
  120. /**
  121. * The global alpha value to be used for every pixel.
  122. * <p>
  123. * If this value is set, the <code>alphaData</code> field
  124. * is ignored and when the image is rendered each pixel
  125. * will be blended with the background an amount
  126. * proportional to this value.
  127. * </p><p>
  128. * The default is -1 which means 'no global alpha value'
  129. * </p>
  130. */
  131. public int alpha;
  132. /**
  133. * The type of file from which the image was read.
  134. *
  135. * It is expressed as one of the following values:
  136. * <dl>
  137. * <dt><code>IMAGE_BMP</code></dt>
  138. * <dd>Windows BMP file format, no compression</dd>
  139. * <dt><code>IMAGE_BMP_RLE</code></dt>
  140. * <dd>Windows BMP file format, RLE compression if appropriate</dd>
  141. * <dt><code>IMAGE_GIF</code></dt>
  142. * <dd>GIF file format</dd>
  143. * <dt><code>IMAGE_ICO</code></dt>
  144. * <dd>Windows ICO file format</dd>
  145. * <dt><code>IMAGE_JPEG</code></dt>
  146. * <dd>JPEG file format</dd>
  147. * <dt><code>IMAGE_PNG</code></dt>
  148. * <dd>PNG file format</dd>
  149. * </dl>
  150. */
  151. public int type;
  152. /**
  153. * The x coordinate of the top left corner of the image
  154. * within the logical screen (this field corresponds to
  155. * the GIF89a Image Left Position value).
  156. */
  157. public int x;
  158. /**
  159. * The y coordinate of the top left corner of the image
  160. * within the logical screen (this field corresponds to
  161. * the GIF89a Image Top Position value).
  162. */
  163. public int y;
  164. /**
  165. * A description of how to dispose of the current image
  166. * before displaying the next.
  167. *
  168. * It is expressed as one of the following values:
  169. * <dl>
  170. * <dt><code>DM_UNSPECIFIED</code></dt>
  171. * <dd>disposal method not specified</dd>
  172. * <dt><code>DM_FILL_NONE</code></dt>
  173. * <dd>do nothing - leave the image in place</dd>
  174. * <dt><code>DM_FILL_BACKGROUND</code></dt>
  175. * <dd>fill with the background color</dd>
  176. * <dt><code>DM_FILL_PREVIOUS</code></dt>
  177. * <dd>restore the previous picture</dd>
  178. * </dl>
  179. * (this field corresponds to the GIF89a Disposal Method value)
  180. */
  181. public int disposalMethod;
  182. /**
  183. * The time to delay before displaying the next image
  184. * in an animation (this field corresponds to the GIF89a
  185. * Delay Time value).
  186. */
  187. public int delayTime;
  188. /**
  189. * Arbitrary channel width data to 8-bit conversion table.
  190. */
  191. static final byte[][] ANY_TO_EIGHT = new byte[9][];
  192. static {
  193. for (int b = 0; b < 9; ++b) {
  194. byte[] data = ANY_TO_EIGHT[b] = new byte[1 << b];
  195. if (b == 0) continue;
  196. int inc = 0;
  197. for (int bit = 0x10000; (bit >>= b) != 0;) inc |= bit;
  198. for (int v = 0, p = 0; v < 0x10000; v+= inc) data[p++] = (byte)(v >> 8);
  199. }
  200. }
  201. static final byte[] ONE_TO_ONE_MAPPING = ANY_TO_EIGHT[8];
  202. /**
  203. * Scaled 8x8 Bayer dither matrix.
  204. */
  205. static final int[][] DITHER_MATRIX = {
  206. { 0xfc0000, 0x7c0000, 0xdc0000, 0x5c0000, 0xf40000, 0x740000, 0xd40000, 0x540000 },
  207. { 0x3c0000, 0xbc0000, 0x1c0000, 0x9c0000, 0x340000, 0xb40000, 0x140000, 0x940000 },
  208. { 0xcc0000, 0x4c0000, 0xec0000, 0x6c0000, 0xc40000, 0x440000, 0xe40000, 0x640000 },
  209. { 0x0c0000, 0x8c0000, 0x2c0000, 0xac0000, 0x040000, 0x840000, 0x240000, 0xa40000 },
  210. { 0xf00000, 0x700000, 0xd00000, 0x500000, 0xf80000, 0x780000, 0xd80000, 0x580000 },
  211. { 0x300000, 0xb00000, 0x100000, 0x900000, 0x380000, 0xb80000, 0x180000, 0x980000 },
  212. { 0xc00000, 0x400000, 0xe00000, 0x600000, 0xc80000, 0x480000, 0xe80000, 0x680000 },
  213. { 0x000000, 0x800000, 0x200000, 0xa00000, 0x080000, 0x880000, 0x280000, 0xa80000 }
  214. };
  215. /**
  216. * Constructs a new, empty ImageData with the given width, height,
  217. * depth and palette. The data will be initialized to an (all zero)
  218. * array of the appropriate size.
  219. *
  220. * @param width the width of the image
  221. * @param height the height of the image
  222. * @param depth the depth of the image
  223. * @param palette the palette of the image (must not be null)
  224. *
  225. * @exception IllegalArgumentException <ul>
  226. * <li>ERROR_INVALID_ARGUMENT - if the width or height is zero or negative, or if the depth is not
  227. * one of 1, 2, 4, 8, 16, 24 or 32</li>
  228. * <li>ERROR_NULL_ARGUMENT - if the palette is null</li>
  229. * </ul>
  230. */
  231. public ImageData(int width, int height, int depth, PaletteData palette) {
  232. this(width, height, depth, palette,
  233. 4, null, 0, null,
  234. null, -1, -1, SWT.IMAGE_UNDEFINED,
  235. 0, 0, 0, 0);
  236. }
  237. /**
  238. * Constructs a new, empty ImageData with the given width, height,
  239. * depth, palette, scanlinePad and data.
  240. *
  241. * @param width the width of the image
  242. * @param height the height of the image
  243. * @param depth the depth of the image
  244. * @param palette the palette of the image
  245. * @param scanlinePad the padding of each line, in bytes
  246. * @param data the data of the image
  247. *
  248. * @exception IllegalArgumentException <ul>
  249. * <li>ERROR_INVALID_ARGUMENT - if the width or height is zero or negative, or if the depth is not
  250. * one of 1, 2, 4, 8, 16, 24 or 32, or the data array is too small to contain the image data</li>
  251. * <li>ERROR_NULL_ARGUMENT - if the palette or data is null</li>
  252. * <li>ERROR_CANNOT_BE_ZERO - if the scanlinePad is zero</li>
  253. * </ul>
  254. */
  255. public ImageData(int width, int height, int depth, PaletteData palette, int scanlinePad, byte[] data) {
  256. this(width, height, depth, palette,
  257. scanlinePad, checkData(data), 0, null,
  258. null, -1, -1, SWT.IMAGE_UNDEFINED,
  259. 0, 0, 0, 0);
  260. }
  261. /**
  262. * Constructs an <code>ImageData</code> loaded from the specified
  263. * input stream. Throws an error if an error occurs while loading
  264. * the image, or if the image has an unsupported type. Application
  265. * code is still responsible for closing the input stream.
  266. * <p>
  267. * This constructor is provided for convenience when loading a single
  268. * image only. If the stream contains multiple images, only the first
  269. * one will be loaded. To load multiple images, use
  270. * <code>ImageLoader.load()</code>.
  271. * </p><p>
  272. * This constructor may be used to load a resource as follows:
  273. * </p>
  274. * <pre>
  275. * static ImageData loadImageData (Class clazz, String string) {
  276. * InputStream stream = clazz.getResourceAsStream (string);
  277. * if (stream == null) return null;
  278. * ImageData imageData = null;
  279. * try {
  280. * imageData = new ImageData (stream);
  281. * } catch (SWTException ex) {
  282. * } finally {
  283. * try {
  284. * stream.close ();
  285. * } catch (IOException ex) {}
  286. * }
  287. * return imageData;
  288. * }
  289. * </pre>
  290. *
  291. * @param stream the input stream to load the image from (must not be null)
  292. *
  293. * @exception IllegalArgumentException <ul>
  294. * <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
  295. * </ul>
  296. * @exception SWTException <ul>
  297. * <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
  298. * <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
  299. * <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
  300. * </ul>
  301. *
  302. * @see ImageLoader#load(InputStream)
  303. */
  304. public ImageData(InputStream stream) {
  305. ImageData[] data = ImageDataLoader.load(stream);
  306. if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
  307. ImageData i = data[0];
  308. setAllFields(
  309. i.width,
  310. i.height,
  311. i.depth,
  312. i.scanlinePad,
  313. i.bytesPerLine,
  314. i.data,
  315. i.palette,
  316. i.transparentPixel,
  317. i.maskData,
  318. i.maskPad,
  319. i.alphaData,
  320. i.alpha,
  321. i.type,
  322. i.x,
  323. i.y,
  324. i.disposalMethod,
  325. i.delayTime);
  326. }
  327. /**
  328. * Constructs an <code>ImageData</code> loaded from a file with the
  329. * specified name. Throws an error if an error occurs loading the
  330. * image, or if the image has an unsupported type.
  331. * <p>
  332. * This constructor is provided for convenience when loading a single
  333. * image only. If the file contains multiple images, only the first
  334. * one will be loaded. To load multiple images, use
  335. * <code>ImageLoader.load()</code>.
  336. * </p>
  337. *
  338. * @param filename the name of the file to load the image from (must not be null)
  339. *
  340. * @exception IllegalArgumentException <ul>
  341. * <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
  342. * </ul>
  343. * @exception SWTException <ul>
  344. * <li>ERROR_IO - if an IO error occurs while reading from the file</li>
  345. * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
  346. * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
  347. * </ul>
  348. */
  349. public ImageData(String filename) {
  350. ImageData[] data = ImageDataLoader.load(filename);
  351. if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
  352. ImageData i = data[0];
  353. setAllFields(
  354. i.width,
  355. i.height,
  356. i.depth,
  357. i.scanlinePad,
  358. i.bytesPerLine,
  359. i.data,
  360. i.palette,
  361. i.transparentPixel,
  362. i.maskData,
  363. i.maskPad,
  364. i.alphaData,
  365. i.alpha,
  366. i.type,
  367. i.x,
  368. i.y,
  369. i.disposalMethod,
  370. i.delayTime);
  371. }
  372. /**
  373. * Prevents uninitialized instances from being created outside the package.
  374. */
  375. ImageData() {
  376. }
  377. /**
  378. * Constructs an image data by giving values for all non-computable fields.
  379. * <p>
  380. * This method is for internal use, and is not described further.
  381. * </p>
  382. */
  383. ImageData(
  384. int width, int height, int depth, PaletteData palette,
  385. int scanlinePad, byte[] data, int maskPad, byte[] maskData,
  386. byte[] alphaData, int alpha, int transparentPixel, int type,
  387. int x, int y, int disposalMethod, int delayTime)
  388. {
  389. if (palette == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
  390. if (!(depth == 1 || depth == 2 || depth == 4 || depth == 8
  391. || depth == 16 || depth == 24 || depth == 32)) {
  392. SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  393. }
  394. if (width <= 0 || height <= 0) {
  395. SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  396. }
  397. if (scanlinePad == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO);
  398. int bytesPerLine = (((width * depth + 7) / 8) + (scanlinePad - 1))
  399. / scanlinePad * scanlinePad;
  400. /*
  401. * When the image is being loaded from a PNG, we need to use the theoretical minimum
  402. * number of bytes per line to check whether there is enough data, because the actual
  403. * number of bytes per line is calculated based on the given depth, which may be larger
  404. * than the actual depth of the PNG.
  405. */
  406. int minBytesPerLine = type == SWT.IMAGE_PNG ? ((((width + 7) / 8) + 3) / 4) * 4 : bytesPerLine;
  407. if (data != null && data.length < minBytesPerLine * height) {
  408. SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  409. }
  410. setAllFields(
  411. width,
  412. height,
  413. depth,
  414. scanlinePad,
  415. bytesPerLine,
  416. data != null ? data : new byte[bytesPerLine * height],
  417. palette,
  418. transparentPixel,
  419. maskData,
  420. maskPad,
  421. alphaData,
  422. alpha,
  423. type,
  424. x,
  425. y,
  426. disposalMethod,
  427. delayTime);
  428. }
  429. /**
  430. * Initializes all fields in the receiver. This method must be called
  431. * by all public constructors to ensure that all fields are initialized
  432. * for a new ImageData object. If a new field is added to the class,
  433. * then it must be added to this method.
  434. * <p>
  435. * This method is for internal use, and is not described further.
  436. * </p>
  437. */
  438. void setAllFields(int width, int height, int depth, int scanlinePad,
  439. int bytesPerLine, byte[] data, PaletteData palette, int transparentPixel,
  440. byte[] maskData, int maskPad, byte[] alphaData, int alpha,
  441. int type, int x, int y, int disposalMethod, int delayTime) {
  442. this.width = width;
  443. this.height = height;
  444. this.depth = depth;
  445. this.scanlinePad = scanlinePad;
  446. this.bytesPerLine = bytesPerLine;
  447. this.data = data;
  448. this.palette = palette;
  449. this.transparentPixel = transparentPixel;
  450. this.maskData = maskData;
  451. this.maskPad = maskPad;
  452. this.alphaData = alphaData;
  453. this.alpha = alpha;
  454. this.type = type;
  455. this.x = x;
  456. this.y = y;
  457. this.disposalMethod = disposalMethod;
  458. this.delayTime = delayTime;
  459. }
  460. /**
  461. * Invokes internal SWT functionality to create a new instance of
  462. * this class.
  463. * <p>
  464. * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
  465. * API for <code>ImageData</code>. It is marked public only so that it
  466. * can be shared within the packages provided by SWT. It is subject
  467. * to change without notice, and should never be called from
  468. * application code.
  469. * </p>
  470. * <p>
  471. * This method is for internal use, and is not described further.
  472. * </p>
  473. */
  474. public static ImageData internal_new(
  475. int width, int height, int depth, PaletteData palette,
  476. int scanlinePad, byte[] data, int maskPad, byte[] maskData,
  477. byte[] alphaData, int alpha, int transparentPixel, int type,
  478. int x, int y, int disposalMethod, int delayTime)
  479. {
  480. return new ImageData(
  481. width, height, depth, palette, scanlinePad, data, maskPad, maskData,
  482. alphaData, alpha, transparentPixel, type, x, y, disposalMethod, delayTime);
  483. }
  484. ImageData colorMaskImage(int pixel) {
  485. ImageData mask = new ImageData(width, height, 1, bwPalette(),
  486. 2, null, 0, null, null, -1, -1, SWT.IMAGE_UNDEFINED,
  487. 0, 0, 0, 0);
  488. int[] row = new int[width];
  489. for (int y = 0; y < height; y++) {
  490. getPixels(0, y, width, row, 0);
  491. for (int i = 0; i < width; i++) {
  492. if (pixel != -1 && row[i] == pixel) {
  493. row[i] = 0;
  494. } else {
  495. row[i] = 1;
  496. }
  497. }
  498. mask.setPixels(0, y, width, row, 0);
  499. }
  500. return mask;
  501. }
  502. static byte[] checkData(byte [] data) {
  503. if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
  504. return data;
  505. }
  506. /**
  507. * Returns a new instance of the same class as the receiver,
  508. * whose slots have been filled in with <em>copies</em> of
  509. * the values in the slots of the receiver. That is, the
  510. * returned object is a <em>deep copy</em> of the receiver.
  511. *
  512. * @return a copy of the receiver.
  513. */
  514. public Object clone() {
  515. byte[] cloneData = new byte[data.length];
  516. System.arraycopy(data, 0, cloneData, 0, data.length);
  517. byte[] cloneMaskData = null;
  518. if (maskData != null) {
  519. cloneMaskData = new byte[maskData.length];
  520. System.arraycopy(maskData, 0, cloneMaskData, 0, maskData.length);
  521. }
  522. byte[] cloneAlphaData = null;
  523. if (alphaData != null) {
  524. cloneAlphaData = new byte[alphaData.length];
  525. System.arraycopy(alphaData, 0, cloneAlphaData, 0, alphaData.length);
  526. }
  527. return new ImageData(
  528. width,
  529. height,
  530. depth,
  531. palette,
  532. scanlinePad,
  533. cloneData,
  534. maskPad,
  535. cloneMaskData,
  536. cloneAlphaData,
  537. alpha,
  538. transparentPixel,
  539. type,
  540. x,
  541. y,
  542. disposalMethod,
  543. delayTime);
  544. }
  545. /**
  546. * Returns the alpha value at offset <code>x</code> in
  547. * scanline <code>y</code> in the receiver's alpha data.
  548. * The alpha value is between 0 (transparent) and
  549. * 255 (opaque).
  550. *
  551. * @param x the x coordinate of the pixel to get the alpha value of
  552. * @param y the y coordinate of the pixel to get the alpha value of
  553. * @return the alpha value at the given coordinates
  554. *
  555. * @exception IllegalArgumentException <ul>
  556. * <li>ERROR_INVALID_ARGUMENT - if either argument is out of range</li>
  557. * </ul>
  558. */
  559. public int getAlpha(int x, int y) {
  560. if (x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  561. if (alphaData == null) return 255;
  562. return alphaData[y * width + x] & 0xFF;
  563. }
  564. /**
  565. * Returns <code>getWidth</code> alpha values starting at offset
  566. * <code>x</code> in scanline <code>y</code> in the receiver's alpha
  567. * data starting at <code>startIndex</code>. The alpha values
  568. * are unsigned, between <code>(byte)0</code> (transparent) and
  569. * <code>(byte)255</code> (opaque).
  570. *
  571. * @param x the x position of the pixel to begin getting alpha values
  572. * @param y the y position of the pixel to begin getting alpha values
  573. * @param getWidth the width of the data to get
  574. * @param alphas the buffer in which to put the alpha values
  575. * @param startIndex the offset into the image to begin getting alpha values
  576. *
  577. * @exception IndexOutOfBoundsException if getWidth is too large
  578. * @exception IllegalArgumentException <ul>
  579. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  580. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  581. * <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li>
  582. * </ul>
  583. */
  584. public void getAlphas(int x, int y, int getWidth, byte[] alphas, int startIndex) {
  585. if (alphas == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
  586. if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  587. if (getWidth == 0) return;
  588. if (alphaData == null) {
  589. int endIndex = startIndex + getWidth;
  590. for (int i = startIndex; i < endIndex; i++) {
  591. alphas[i] = (byte)255;
  592. }
  593. return;
  594. }
  595. // may throw an IndexOutOfBoundsException
  596. System.arraycopy(alphaData, y * width + x, alphas, startIndex, getWidth);
  597. }
  598. /**
  599. * Returns the pixel value at offset <code>x</code> in
  600. * scanline <code>y</code> in the receiver's data.
  601. *
  602. * @param x the x position of the pixel to get
  603. * @param y the y position of the pixel to get
  604. * @return the pixel at the given coordinates
  605. *
  606. * @exception IllegalArgumentException <ul>
  607. * <li>ERROR_INVALID_ARGUMENT - if either argument is out of bounds</li>
  608. * </ul>
  609. * @exception SWTException <ul>
  610. * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li>
  611. * </ul>
  612. */
  613. public int getPixel(int x, int y) {
  614. if (x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  615. int index;
  616. int theByte;
  617. int mask;
  618. switch (depth) {
  619. case 32:
  620. index = (y * bytesPerLine) + (x * 4);
  621. return ((data[index] & 0xFF) << 24) + ((data[index+1] & 0xFF) << 16) +
  622. ((data[index+2] & 0xFF) << 8) + (data[index+3] & 0xFF);
  623. case 24:
  624. index = (y * bytesPerLine) + (x * 3);
  625. return ((data[index] & 0xFF) << 16) + ((data[index+1] & 0xFF) << 8) +
  626. (data[index+2] & 0xFF);
  627. case 16:
  628. index = (y * bytesPerLine) + (x * 2);
  629. return ((data[index+1] & 0xFF) << 8) + (data[index] & 0xFF);
  630. case 8:
  631. index = (y * bytesPerLine) + x ;
  632. return data[index] & 0xFF;
  633. case 4:
  634. index = (y * bytesPerLine) + (x >> 1);
  635. theByte = data[index] & 0xFF;
  636. if ((x & 0x1) == 0) {
  637. return theByte >> 4;
  638. } else {
  639. return theByte & 0x0F;
  640. }
  641. case 2:
  642. index = (y * bytesPerLine) + (x >> 2);
  643. theByte = data[index] & 0xFF;
  644. int offset = 3 - (x % 4);
  645. mask = 3 << (offset * 2);
  646. return (theByte & mask) >> (offset * 2);
  647. case 1:
  648. index = (y * bytesPerLine) + (x >> 3);
  649. theByte = data[index] & 0xFF;
  650. mask = 1 << (7 - (x & 0x7));
  651. if ((theByte & mask) == 0) {
  652. return 0;
  653. } else {
  654. return 1;
  655. }
  656. }
  657. SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);
  658. return 0;
  659. }
  660. /**
  661. * Returns <code>getWidth</code> pixel values starting at offset
  662. * <code>x</code> in scanline <code>y</code> in the receiver's
  663. * data starting at <code>startIndex</code>.
  664. *
  665. * @param x the x position of the first pixel to get
  666. * @param y the y position of the first pixel to get
  667. * @param getWidth the width of the data to get
  668. * @param pixels the buffer in which to put the pixels
  669. * @param startIndex the offset into the byte array to begin storing pixels
  670. *
  671. * @exception IndexOutOfBoundsException if getWidth is too large
  672. * @exception IllegalArgumentException <ul>
  673. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  674. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  675. * <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li>
  676. * </ul>
  677. * @exception SWTException <ul>
  678. * <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4 or 8
  679. * (For higher depths, use the int[] version of this method.)</li>
  680. * </ul>
  681. */
  682. public void getPixels(int x, int y, int getWidth, byte[] pixels, int startIndex) {
  683. if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
  684. if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  685. if (getWidth == 0) return;
  686. int index;
  687. int theByte;
  688. int mask = 0;
  689. int n = getWidth;
  690. int i = startIndex;
  691. int srcX = x, srcY = y;
  692. switch (depth) {
  693. case 8:
  694. index = (y * bytesPerLine) + x;
  695. for (int j = 0; j < getWidth; j++) {
  696. pixels[i] = data[index];
  697. i++;
  698. srcX++;
  699. if (srcX >= width) {
  700. srcY++;
  701. index = srcY * bytesPerLine;
  702. srcX = 0;
  703. } else {
  704. index++;
  705. }
  706. }
  707. return;
  708. case 4:
  709. index = (y * bytesPerLine) + (x >> 1);
  710. if ((x & 0x1) == 1) {
  711. theByte = data[index] & 0xFF;
  712. pixels[i] = (byte)(theByte & 0x0F);
  713. i++;
  714. n--;
  715. srcX++;
  716. if (srcX >= width) {
  717. srcY++;
  718. index = srcY * bytesPerLine;
  719. srcX = 0;
  720. } else {
  721. index++;
  722. }
  723. }
  724. while (n > 1) {
  725. theByte = data[index] & 0xFF;
  726. pixels[i] = (byte)(theByte >> 4);
  727. i++;
  728. n--;
  729. srcX++;
  730. if (srcX >= width) {
  731. srcY++;
  732. index = srcY * bytesPerLine;
  733. srcX = 0;
  734. } else {
  735. pixels[i] = (byte)(theByte & 0x0F);
  736. i++;
  737. n--;
  738. srcX++;
  739. if (srcX >= width) {
  740. srcY++;
  741. index = srcY * bytesPerLine;
  742. srcX = 0;
  743. } else {
  744. index++;
  745. }
  746. }
  747. }
  748. if (n > 0) {
  749. theByte = data[index] & 0xFF;
  750. pixels[i] = (byte)(theByte >> 4);
  751. }
  752. return;
  753. case 2:
  754. index = (y * bytesPerLine) + (x >> 2);
  755. theByte = data[index] & 0xFF;
  756. int offset;
  757. while (n > 0) {
  758. offset = 3 - (srcX % 4);
  759. mask = 3 << (offset * 2);
  760. pixels[i] = (byte)((theByte & mask) >> (offset * 2));
  761. i++;
  762. n--;
  763. srcX++;
  764. if (srcX >= width) {
  765. srcY++;
  766. index = srcY * bytesPerLine;
  767. if (n > 0) theByte = data[index] & 0xFF;
  768. srcX = 0;
  769. } else {
  770. if (offset == 0) {
  771. index++;
  772. theByte = data[index] & 0xFF;
  773. }
  774. }
  775. }
  776. return;
  777. case 1:
  778. index = (y * bytesPerLine) + (x >> 3);
  779. theByte = data[index] & 0xFF;
  780. while (n > 0) {
  781. mask = 1 << (7 - (srcX & 0x7));
  782. if ((theByte & mask) == 0) {
  783. pixels[i] = 0;
  784. } else {
  785. pixels[i] = 1;
  786. }
  787. i++;
  788. n--;
  789. srcX++;
  790. if (srcX >= width) {
  791. srcY++;
  792. index = srcY * bytesPerLine;
  793. if (n > 0) theByte = data[index] & 0xFF;
  794. srcX = 0;
  795. } else {
  796. if (mask == 1) {
  797. index++;
  798. if (n > 0) theByte = data[index] & 0xFF;
  799. }
  800. }
  801. }
  802. return;
  803. }
  804. SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);
  805. }
  806. /**
  807. * Returns <code>getWidth</code> pixel values starting at offset
  808. * <code>x</code> in scanline <code>y</code> in the receiver's
  809. * data starting at <code>startIndex</code>.
  810. *
  811. * @param x the x position of the first pixel to get
  812. * @param y the y position of the first pixel to get
  813. * @param getWidth the width of the data to get
  814. * @param pixels the buffer in which to put the pixels
  815. * @param startIndex the offset into the buffer to begin storing pixels
  816. *
  817. * @exception IndexOutOfBoundsException if getWidth is too large
  818. * @exception IllegalArgumentException <ul>
  819. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  820. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  821. * <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li>
  822. * </ul>
  823. * @exception SWTException <ul>
  824. * <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li>
  825. * </ul>
  826. */
  827. public void getPixels(int x, int y, int getWidth, int[] pixels, int startIndex) {
  828. if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
  829. if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  830. if (getWidth == 0) return;
  831. int index;
  832. int theByte;
  833. int mask;
  834. int n = getWidth;
  835. int i = startIndex;
  836. int srcX = x, srcY = y;
  837. switch (depth) {
  838. case 32:
  839. index = (y * bytesPerLine) + (x * 4);
  840. i = startIndex;
  841. for (int j = 0; j < getWidth; j++) {
  842. pixels[i] = ((data[index] & 0xFF) << 24) | ((data[index+1] & 0xFF) << 16)
  843. | ((data[index+2] & 0xFF) << 8) | (data[index+3] & 0xFF);
  844. i++;
  845. srcX++;
  846. if (srcX >= width) {
  847. srcY++;
  848. index = srcY * bytesPerLine;
  849. srcX = 0;
  850. } else {
  851. index += 4;
  852. }
  853. }
  854. return;
  855. case 24:
  856. index = (y * bytesPerLine) + (x * 3);
  857. for (int j = 0; j < getWidth; j++) {
  858. pixels[i] = ((data[index] & 0xFF) << 16) | ((data[index+1] & 0xFF) << 8)
  859. | (data[index+2] & 0xFF);
  860. i++;
  861. srcX++;
  862. if (srcX >= width) {
  863. srcY++;
  864. index = srcY * bytesPerLine;
  865. srcX = 0;
  866. } else {
  867. index += 3;
  868. }
  869. }
  870. return;
  871. case 16:
  872. index = (y * bytesPerLine) + (x * 2);
  873. for (int j = 0; j < getWidth; j++) {
  874. pixels[i] = ((data[index+1] & 0xFF) << 8) + (data[index] & 0xFF);
  875. i++;
  876. srcX++;
  877. if (srcX >= width) {
  878. srcY++;
  879. index = srcY * bytesPerLine;
  880. srcX = 0;
  881. } else {
  882. index += 2;
  883. }
  884. }
  885. return;
  886. case 8:
  887. index = (y * bytesPerLine) + x;
  888. for (int j = 0; j < getWidth; j++) {
  889. pixels[i] = data[index] & 0xFF;
  890. i++;
  891. srcX++;
  892. if (srcX >= width) {
  893. srcY++;
  894. index = srcY * bytesPerLine;
  895. srcX = 0;
  896. } else {
  897. index++;
  898. }
  899. }
  900. return;
  901. case 4:
  902. index = (y * bytesPerLine) + (x >> 1);
  903. if ((x & 0x1) == 1) {
  904. theByte = data[index] & 0xFF;
  905. pixels[i] = theByte & 0x0F;
  906. i++;
  907. n--;
  908. srcX++;
  909. if (srcX >= width) {
  910. srcY++;
  911. index = srcY * bytesPerLine;
  912. srcX = 0;
  913. } else {
  914. index++;
  915. }
  916. }
  917. while (n > 1) {
  918. theByte = data[index] & 0xFF;
  919. pixels[i] = theByte >> 4;
  920. i++;
  921. n--;
  922. srcX++;
  923. if (srcX >= width) {
  924. srcY++;
  925. index = srcY * bytesPerLine;
  926. srcX = 0;
  927. } else {
  928. pixels[i] = theByte & 0x0F;
  929. i++;
  930. n--;
  931. srcX++;
  932. if (srcX >= width) {
  933. srcY++;
  934. index = srcY * bytesPerLine;
  935. srcX = 0;
  936. } else {
  937. index++;
  938. }
  939. }
  940. }
  941. if (n > 0) {
  942. theByte = data[index] & 0xFF;
  943. pixels[i] = theByte >> 4;
  944. }
  945. return;
  946. case 2:
  947. index = (y * bytesPerLine) + (x >> 2);
  948. theByte = data[index] & 0xFF;
  949. int offset;
  950. while (n > 0) {
  951. offset = 3 - (srcX % 4);
  952. mask = 3 << (offset * 2);
  953. pixels[i] = (byte)((theByte & mask) >> (offset * 2));
  954. i++;
  955. n--;
  956. srcX++;
  957. if (srcX >= width) {
  958. srcY++;
  959. index = srcY * bytesPerLine;
  960. if (n > 0) theByte = data[index] & 0xFF;
  961. srcX = 0;
  962. } else {
  963. if (offset == 0) {
  964. index++;
  965. theByte = data[index] & 0xFF;
  966. }
  967. }
  968. }
  969. return;
  970. case 1:
  971. index = (y * bytesPerLine) + (x >> 3);
  972. theByte = data[index] & 0xFF;
  973. while (n > 0) {
  974. mask = 1 << (7 - (srcX & 0x7));
  975. if ((theByte & mask) == 0) {
  976. pixels[i] = 0;
  977. } else {
  978. pixels[i] = 1;
  979. }
  980. i++;
  981. n--;
  982. srcX++;
  983. if (srcX >= width) {
  984. srcY++;
  985. index = srcY * bytesPerLine;
  986. if (n > 0) theByte = data[index] & 0xFF;
  987. srcX = 0;
  988. } else {
  989. if (mask == 1) {
  990. index++;
  991. if (n > 0) theByte = data[index] & 0xFF;
  992. }
  993. }
  994. }
  995. return;
  996. }
  997. SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);
  998. }
  999. /**
  1000. * Returns an array of <code>RGB</code>s which comprise the
  1001. * indexed color table of the receiver, or null if the receiver
  1002. * has a direct color model.
  1003. *
  1004. * @return the RGB values for the image or null if direct color
  1005. *
  1006. * @see PaletteData#getRGBs()
  1007. */
  1008. public RGB[] getRGBs() {
  1009. return palette.getRGBs();
  1010. }
  1011. /**
  1012. * Returns an <code>ImageData</code> which specifies the
  1013. * transparency mask information for the receiver. If the
  1014. * receiver has no transparency or is not an icon, returns
  1015. * an opaque mask.
  1016. *
  1017. * @return the transparency mask
  1018. */
  1019. public ImageData getTransparencyMask() {
  1020. if (getTransparencyType() == SWT.TRANSPARENCY_MASK) {
  1021. return new ImageData(width, height, 1, bwPalette(), maskPad, maskData);
  1022. } else {
  1023. return colorMaskImage(transparentPixel);
  1024. }
  1025. }
  1026. /**
  1027. * Returns the image transparency type, which will be one of
  1028. * <code>SWT.TRANSPARENCY_NONE</code>, <code>SWT.TRANSPARENCY_MASK</code>,
  1029. * <code>SWT.TRANSPARENCY_PIXEL</code> or <code>SWT.TRANSPARENCY_ALPHA</code>.
  1030. *
  1031. * @return the receiver's transparency type
  1032. */
  1033. public int getTransparencyType() {
  1034. if (maskData != null) return SWT.TRANSPARENCY_MASK;
  1035. if (transparentPixel != -1) return SWT.TRANSPARENCY_PIXEL;
  1036. if (alphaData != null) return SWT.TRANSPARENCY_ALPHA;
  1037. return SWT.TRANSPARENCY_NONE;
  1038. }
  1039. /**
  1040. * Returns the byte order of the receiver.
  1041. *
  1042. * @return MSB_FIRST or LSB_FIRST
  1043. */
  1044. int getByteOrder() {
  1045. return depth != 16 ? MSB_FIRST : LSB_FIRST;
  1046. }
  1047. /**
  1048. * Returns a copy of the receiver which has been stretched or
  1049. * shrunk to the specified size. If either the width or height
  1050. * is negative, the resulting image will be inverted in the
  1051. * associated axis.
  1052. *
  1053. * @param width the width of the new ImageData
  1054. * @param height the height of the new ImageData
  1055. * @return a scaled copy of the image
  1056. */
  1057. public ImageData scaledTo(int width, int height) {
  1058. /* Create a destination image with no data */
  1059. final boolean flipX = (width < 0);
  1060. if (flipX) width = - width;
  1061. final boolean flipY = (height < 0);
  1062. if (flipY) height = - height;
  1063. ImageData dest = new ImageData(
  1064. width, height, depth, palette,
  1065. scanlinePad, null, 0, null,
  1066. null, -1, transparentPixel, type,
  1067. x, y, disposalMethod, delayTime);
  1068. /* Scale the image contents */
  1069. if (palette.isDirect) blit(BLIT_SRC,
  1070. this.data, this.depth, this.bytesPerLine, this.getByteOrder(), 0, 0, this.width, this.height, 0, 0, 0,
  1071. ALPHA_OPAQUE, null, 0, 0, 0,
  1072. dest.data, dest.depth, dest.bytesPerLine, dest.getByteOrder(), 0, 0, dest.width, dest.height, 0, 0, 0,
  1073. flipX, flipY);
  1074. else blit(BLIT_SRC,
  1075. this.data, this.depth, this.bytesPerLine, this.getByteOrder(), 0, 0, this.width, this.height, null, null, null,
  1076. ALPHA_OPAQUE, null, 0, 0, 0,
  1077. dest.data, dest.depth, dest.bytesPerLine, dest.getByteOrder(), 0, 0, dest.width, dest.height, null, null, null,
  1078. flipX, flipY);
  1079. /* Scale the image mask or alpha */
  1080. if (maskData != null) {
  1081. dest.maskPad = this.maskPad;
  1082. int destBpl = (dest.width + 7) / 8;
  1083. destBpl = (destBpl + (dest.maskPad - 1)) / dest.maskPad * dest.maskPad;
  1084. dest.maskData = new byte[destBpl * dest.height];
  1085. int srcBpl = (this.width + 7) / 8;
  1086. srcBpl = (srcBpl + (this.maskPad - 1)) / this.maskPad * this.maskPad;
  1087. blit(BLIT_SRC,
  1088. this.maskData, 1, srcBpl, MSB_FIRST, 0, 0, this.width, this.height, null, null, null,
  1089. ALPHA_OPAQUE, null, 0, 0, 0,
  1090. dest.maskData, 1, destBpl, MSB_FIRST, 0, 0, dest.width, dest.height, null, null, null,
  1091. flipX, flipY);
  1092. } else if (alpha != -1) {
  1093. dest.alpha = this.alpha;
  1094. } else if (alphaData != null) {
  1095. dest.alphaData = new byte[dest.width * dest.height];
  1096. blit(BLIT_SRC,
  1097. this.alphaData, 8, this.width, MSB_FIRST, 0, 0, this.width, this.height, null, null, null,
  1098. ALPHA_OPAQUE, null, 0, 0, 0,
  1099. dest.alphaData, 8, dest.width, MSB_FIRST, 0, 0, dest.width, dest.height, null, null, null,
  1100. flipX, flipY);
  1101. }
  1102. return dest;
  1103. }
  1104. /**
  1105. * Sets the alpha value at offset <code>x</code> in
  1106. * scanline <code>y</code> in the receiver's alpha data.
  1107. * The alpha value must be between 0 (transparent)
  1108. * and 255 (opaque).
  1109. *
  1110. * @param x the x coordinate of the alpha value to set
  1111. * @param y the y coordinate of the alpha value to set
  1112. * @param alpha the value to set the alpha to
  1113. *
  1114. * @exception IllegalArgumentException <ul>
  1115. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  1116. * </ul>
  1117. */
  1118. public void setAlpha(int x, int y, int alpha) {
  1119. if (x >= width || y >= height || x < 0 || y < 0 || alpha < 0 || alpha > 255)
  1120. SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  1121. if (alphaData == null) alphaData = new byte[width * height];
  1122. alphaData[y * width + x] = (byte)alpha;
  1123. }
  1124. /**
  1125. * Sets the alpha values starting at offset <code>x</code> in
  1126. * scanline <code>y</code> in the receiver's alpha data to the
  1127. * values from the array <code>alphas</code> starting at
  1128. * <code>startIndex</code>. The alpha values must be between
  1129. * <code>(byte)0</code> (transparent) and <code>(byte)255</code> (opaque)
  1130. *
  1131. * @param x the x coordinate of the pixel to being setting the alpha values
  1132. * @param y the y coordinate of the pixel to being setting the alpha values
  1133. * @param putWidth the width of the alpha values to set
  1134. * @param alphas the alpha values to set
  1135. * @param startIndex the index at which to begin setting
  1136. *
  1137. * @exception IndexOutOfBoundsException if putWidth is too large
  1138. * @exception IllegalArgumentException <ul>
  1139. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  1140. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  1141. * <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li>
  1142. * </ul>
  1143. */
  1144. public void setAlphas(int x, int y, int putWidth, byte[] alphas, int startIndex) {
  1145. if (alphas == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
  1146. if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  1147. if (putWidth == 0) return;
  1148. if (alphaData == null) alphaData = new byte[width * height];
  1149. // may throw an IndexOutOfBoundsException
  1150. System.arraycopy(alphas, startIndex, alphaData, y * width + x, putWidth);
  1151. }
  1152. /**
  1153. * Sets the pixel value at offset <code>x</code> in
  1154. * scanline <code>y</code> in the receiver's data.
  1155. *
  1156. * @param x the x coordinate of the pixel to set
  1157. * @param y the y coordinate of the pixel to set
  1158. * @param pixelValue the value to set the pixel to
  1159. *
  1160. * @exception IllegalArgumentException <ul>
  1161. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  1162. * </ul>
  1163. * @exception SWTException <ul>
  1164. * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li>
  1165. * </ul>
  1166. */
  1167. public void setPixel(int x, int y, int pixelValue) {
  1168. if (x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  1169. int index;
  1170. byte theByte;
  1171. int mask;
  1172. switch (depth) {
  1173. case 32:
  1174. index = (y * bytesPerLine) + (x * 4);
  1175. data[index] = (byte)((pixelValue >> 24) & 0xFF);
  1176. data[index + 1] = (byte)((pixelValue >> 16) & 0xFF);
  1177. data[index + 2] = (byte)((pixelValue >> 8) & 0xFF);
  1178. data[index + 3] = (byte)(pixelValue & 0xFF);
  1179. return;
  1180. case 24:
  1181. index = (y * bytesPerLine) + (x * 3);
  1182. data[index] = (byte)((pixelValue >> 16) & 0xFF);
  1183. data[index + 1] = (byte)((pixelValue >> 8) & 0xFF);
  1184. data[index + 2] = (byte)(pixelValue & 0xFF);
  1185. return;
  1186. case 16:
  1187. index = (y * bytesPerLine) + (x * 2);
  1188. data[index + 1] = (byte)((pixelValue >> 8) & 0xFF);
  1189. data[index] = (byte)(pixelValue & 0xFF);
  1190. return;
  1191. case 8:
  1192. index = (y * bytesPerLine) + x ;
  1193. data[index] = (byte)(pixelValue & 0xFF);
  1194. return;
  1195. case 4:
  1196. index = (y * bytesPerLine) + (x >> 1);
  1197. if ((x & 0x1) == 0) {
  1198. data[index] = (byte)((data[index] & 0x0F) | ((pixelValue & 0x0F) << 4));
  1199. } else {
  1200. data[index] = (byte)((data[index] & 0xF0) | (pixelValue & 0x0F));
  1201. }
  1202. return;
  1203. case 2:
  1204. index = (y * bytesPerLine) + (x >> 2);
  1205. theByte = data[index];
  1206. int offset = 3 - (x % 4);
  1207. mask = 0xFF ^ (3 << (offset * 2));
  1208. data[index] = (byte)((data[index] & mask) | (pixelValue << (offset * 2)));
  1209. return;
  1210. case 1:
  1211. index = (y * bytesPerLine) + (x >> 3);
  1212. theByte = data[index];
  1213. mask = 1 << (7 - (x & 0x7));
  1214. if ((pixelValue & 0x1) == 1) {
  1215. data[index] = (byte)(theByte | mask);
  1216. } else {
  1217. data[index] = (byte)(theByte & (mask ^ -1));
  1218. }
  1219. return;
  1220. }
  1221. SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);
  1222. }
  1223. /**
  1224. * Sets the pixel values starting at offset <code>x</code> in
  1225. * scanline <code>y</code> in the receiver's data to the
  1226. * values from the array <code>pixels</code> starting at
  1227. * <code>startIndex</code>.
  1228. *
  1229. * @param x the x position of the pixel to set
  1230. * @param y the y position of the pixel to set
  1231. * @param putWidth the width of the pixels to set
  1232. * @param pixels the pixels to set
  1233. * @param startIndex the index at which to begin setting
  1234. *
  1235. * @exception IndexOutOfBoundsException if putWidth is too large
  1236. * @exception IllegalArgumentException <ul>
  1237. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  1238. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  1239. * <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li>
  1240. * </ul>
  1241. * @exception SWTException <ul>
  1242. * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8
  1243. * (For higher depths, use the int[] version of this method.)</li>
  1244. * </ul>
  1245. */
  1246. public void setPixels(int x, int y, int putWidth, byte[] pixels, int startIndex) {
  1247. if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
  1248. if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  1249. if (putWidth == 0) return;
  1250. int index;
  1251. int theByte;
  1252. int mask;
  1253. int n = putWidth;
  1254. int i = startIndex;
  1255. int srcX = x, srcY = y;
  1256. switch (depth) {
  1257. case 8:
  1258. index = (y * bytesPerLine) + x;
  1259. for (int j = 0; j < putWidth; j++) {
  1260. data[index] = (byte)(pixels[i] & 0xFF);
  1261. i++;
  1262. srcX++;
  1263. if (srcX >= width) {
  1264. srcY++;
  1265. index = srcY * bytesPerLine;
  1266. srcX = 0;
  1267. } else {
  1268. index++;
  1269. }
  1270. }
  1271. return;
  1272. case 4:
  1273. index = (y * bytesPerLine) + (x >> 1);
  1274. boolean high = (x & 0x1) == 0;
  1275. while (n > 0) {
  1276. theByte = pixels[i] & 0x0F;
  1277. if (high) {
  1278. data[index] = (byte)((data[index] & 0x0F) | (theByte << 4));
  1279. } else {
  1280. data[index] = (byte)((data[index] & 0xF0) | theByte);
  1281. }
  1282. i++;
  1283. n--;
  1284. srcX++;
  1285. if (srcX >= width) {
  1286. srcY++;
  1287. index = srcY * bytesPerLine;
  1288. high = true;
  1289. srcX = 0;
  1290. } else {
  1291. if (!high) index++;
  1292. high = !high;
  1293. }
  1294. }
  1295. return;
  1296. case 2:
  1297. byte [] masks = { (byte)0xFC, (byte)0xF3, (byte)0xCF, (byte)0x3F };
  1298. index = (y * bytesPerLine) + (x >> 2);
  1299. int offset = 3 - (x % 4);
  1300. while (n > 0) {
  1301. theByte = pixels[i] & 0x3;
  1302. data[index] = (byte)((data[index] & masks[offset]) | (theByte << (offset * 2)));
  1303. i++;
  1304. n--;
  1305. srcX++;
  1306. if (srcX >= width) {
  1307. srcY++;
  1308. index = srcY * bytesPerLine;
  1309. offset = 0;
  1310. srcX = 0;
  1311. } else {
  1312. if (offset == 0) {
  1313. index++;
  1314. offset = 3;
  1315. } else {
  1316. offset--;
  1317. }
  1318. }
  1319. }
  1320. return;
  1321. case 1:
  1322. index = (y * bytesPerLine) + (x >> 3);
  1323. while (n > 0) {
  1324. mask = 1 << (7 - (srcX & 0x7));
  1325. if ((pixels[i] & 0x1) == 1) {
  1326. data[index] = (byte)((data[index] & 0xFF) | mask);
  1327. } else {
  1328. data[index] = (byte)((data[index] & 0xFF) & (mask ^ -1));
  1329. }
  1330. i++;
  1331. n--;
  1332. srcX++;
  1333. if (srcX >= width) {
  1334. srcY++;
  1335. index = srcY * bytesPerLine;
  1336. srcX = 0;
  1337. } else {
  1338. if (mask == 1) {
  1339. index++;
  1340. }
  1341. }
  1342. }
  1343. return;
  1344. }
  1345. SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);
  1346. }
  1347. /**
  1348. * Sets the pixel values starting at offset <code>x</code> in
  1349. * scanline <code>y</code> in the receiver's data to the
  1350. * values from the array <code>pixels</code> starting at
  1351. * <code>startIndex</code>.
  1352. *
  1353. * @param x the x position of the pixel to set
  1354. * @param y the y position of the pixel to set
  1355. * @param putWidth the width of the pixels to set
  1356. * @param pixels the pixels to set
  1357. * @param startIndex the index at which to begin setting
  1358. *
  1359. * @exception IndexOutOfBoundsException if putWidth is too large
  1360. * @exception IllegalArgumentException <ul>
  1361. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  1362. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  1363. * <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li>
  1364. * </ul>
  1365. * @exception SWTException <ul>
  1366. * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li>
  1367. * </ul>
  1368. */
  1369. public void setPixels(int x, int y, int putWidth, int[] pixels, int startIndex) {
  1370. if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
  1371. if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
  1372. if (putWidth == 0) return;
  1373. int index;
  1374. int theByte;
  1375. int mask;
  1376. int n = putWidth;
  1377. int i = startIndex;
  1378. int pixel;
  1379. int srcX = x, srcY = y;
  1380. switch (depth) {
  1381. case 32:
  1382. index = (y * bytesPerLine) + (x * 4);
  1383. for (int j = 0; j < putWidth; j++) {
  1384. pixel = pixels[i];
  1385. data[index] = (byte)((pixel >> 24) & 0xFF);
  1386. data[index + 1] = (byte)((pixel >> 16) & 0xFF);
  1387. data[index + 2] = (byte)((pixel >> 8) & 0xFF);
  1388. data[index + 3] = (byte)(pixel & 0xFF);
  1389. i++;
  1390. srcX++;
  1391. if (srcX >= width) {
  1392. srcY++;
  1393. index = srcY * bytesPerLine;
  1394. srcX = 0;
  1395. } else {
  1396. index += 4;
  1397. }
  1398. }
  1399. return;
  1400. case 24:
  1401. index = (y * bytesPerLine) + (x * 3);
  1402. for (int j = 0; j < putWidth; j++) {
  1403. pixel = pixels[i];
  1404. data[index] = (byte)((pixel >> 16) & 0xFF);
  1405. data[index + 1] = (byte)((pixel >> 8) & 0xFF);
  1406. data[index + 2] = (byte)(pixel & 0xFF);
  1407. i++;
  1408. srcX++;
  1409. if (srcX >= width) {
  1410. srcY++;
  1411. index = srcY * bytesPerLine;
  1412. srcX = 0;
  1413. } else {
  1414. index += 3;
  1415. }
  1416. }
  1417. return;
  1418. case 16:
  1419. index = (y * bytesPerLine) + (x * 2);
  1420. for (int j = 0; j < putWidth; j++) {
  1421. pixel = pixels[i];
  1422. data[index] = (byte)(pixel & 0xFF);
  1423. data[index + 1] = (byte)((pixel >> 8) & 0xFF);
  1424. i++;
  1425. srcX++;
  1426. if (srcX >= width) {
  1427. srcY++;
  1428. index = srcY * bytesPerLine;
  1429. srcX = 0;
  1430. } else {
  1431. index += 2;
  1432. }
  1433. }
  1434. return;
  1435. case 8:
  1436. index = (y * bytesPerLine) + x;
  1437. for (int j = 0; j < putWidth; j++) {
  1438. data[index] = (byte)(pixels[i] & 0xFF);
  1439. i++;
  1440. srcX++;
  1441. if (srcX >= width) {
  1442. srcY++;
  1443. index = srcY * bytesPerLine;
  1444. srcX = 0;
  1445. } else {
  1446. index++;
  1447. }
  1448. }
  1449. return;
  1450. case 4:
  1451. index = (y * bytesPerLine) + (x >> 1);
  1452. boolean high = (x & 0x1) == 0;
  1453. while (n > 0) {
  1454. theByte = pixels[i] & 0x0F;
  1455. if (high) {
  1456. data[index] = (byte)((data[index] & 0x0F) | (theByte << 4));
  1457. } else {
  1458. data[index] = (byte)((data[index] & 0xF0) | theByte);
  1459. }
  1460. i++;
  1461. n--;
  1462. srcX++;
  1463. if (srcX >= width) {
  1464. srcY++;
  1465. index = srcY * bytesPerLine;
  1466. high = true;
  1467. srcX = 0;
  1468. } else {
  1469. if (!high) index++;
  1470. high = !high;
  1471. }
  1472. }
  1473. return;
  1474. case 2:
  1475. byte [] masks = { (byte)0xFC, (byte)0xF3, (byte)0xCF, (byte)0x3F };
  1476. index = (y * bytesPerLine) + (x >> 2);
  1477. int offset = 3 - (x % 4);
  1478. while (n > 0) {
  1479. theByte = pixels[i] & 0x3;
  1480. data[index] = (byte)((data[index] & masks[offset]) | (theByte << (offset * 2)));
  1481. i++;
  1482. n--;
  1483. srcX++;
  1484. if (srcX >= width) {
  1485. srcY++;
  1486. index = srcY * bytesPerLine;
  1487. offset = 3;
  1488. srcX = 0;
  1489. } else {
  1490. if (offset == 0) {
  1491. index++;
  1492. offset = 3;
  1493. } else {
  1494. offset--;
  1495. }
  1496. }
  1497. }
  1498. return;
  1499. case 1:
  1500. index = (y * bytesPerLine) + (x >> 3);
  1501. while (n > 0) {
  1502. mask = 1 << (7 - (srcX & 0x7));
  1503. if ((pixels[i] & 0x1) == 1) {
  1504. data[index] = (byte)((data[index] & 0xFF) | mask);
  1505. } else {
  1506. data[index] = (byte)((data[index] & 0xFF) & (mask ^ -1));
  1507. }
  1508. i++;
  1509. n--;
  1510. srcX++;
  1511. if (srcX >= width) {
  1512. srcY++;
  1513. index = srcY * bytesPerLine;
  1514. srcX = 0;
  1515. } else {
  1516. if (mask == 1) {
  1517. index++;
  1518. }
  1519. }
  1520. }
  1521. return;
  1522. }
  1523. SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);
  1524. }
  1525. /**
  1526. * Returns a palette with 2 colors: black & white.
  1527. */
  1528. static PaletteData bwPalette() {
  1529. return new PaletteData(new RGB[] {new RGB(0, 0, 0), new RGB(255, 255, 255)});
  1530. }
  1531. /**
  1532. * Gets the offset of the most significant bit for
  1533. * the given mask.
  1534. */
  1535. static int getMSBOffset(int mask) {
  1536. for (int i = 31; i >= 0; i--) {
  1537. if (((mask >> i) & 0x1) != 0) return i + 1;
  1538. }
  1539. return 0;
  1540. }
  1541. /**
  1542. * Finds the closest match.
  1543. */
  1544. static int closestMatch(int depth, byte red, byte green, byte blue, int redMask, int greenMask, int blueMask, byte[] reds, byte[] greens, byte[] blues) {
  1545. if (depth > 8) {
  1546. int rshift = 32 - getMSBOffset(redMask);
  1547. int gshift = 32 - getMSBOffset(greenMask);
  1548. int bshift = 32 - getMSBOffset(blueMask);
  1549. return (((red << 24) >>> rshift) & redMask) |
  1550. (((green << 24) >>> gshift) & greenMask) |
  1551. (((blue << 24) >>> bshift) & blueMask);
  1552. }
  1553. int r, g, b;
  1554. int minDistance = 0x7fffffff;
  1555. int nearestPixel = 0;
  1556. int n = reds.length;
  1557. for (int j = 0; j < n; j++) {
  1558. r = (reds[j] & 0xFF) - (red & 0xFF);
  1559. g = (greens[j] & 0xFF) - (green & 0xFF);
  1560. b = (blues[j] & 0xFF) - (blue & 0xFF);
  1561. int distance = r*r + g*g + b*b;
  1562. if (distance < minDistance) {
  1563. nearestPixel = j;
  1564. if (distance == 0) break;
  1565. minDistance = distance;
  1566. }
  1567. }
  1568. return nearestPixel;
  1569. }
  1570. static final ImageData convertMask(ImageData mask) {
  1571. if (mask.depth == 1) return mask;
  1572. PaletteData palette = new PaletteData(new RGB[] {new RGB(0, 0, 0), new RGB(255,255,255)});
  1573. ImageData newMask = new ImageData(mask.width, mask.height, 1, palette);
  1574. /* Find index of black in mask palette */
  1575. int blackIndex = 0;
  1576. RGB[] rgbs = mask.getRGBs();
  1577. if (rgbs != null) {
  1578. while (blackIndex < rgbs.length) {
  1579. if (rgbs[blackIndex].equals(palette.colors[0])) break;
  1580. blackIndex++;
  1581. }
  1582. }
  1583. int[] pixels = new int[mask.width];
  1584. for (int y = 0; y < mask.height; y++) {
  1585. mask.getPixels(0, y, mask.width, pixels, 0);
  1586. for (int i = 0; i < pixels.length; i++) {
  1587. if (pixels[i] == blackIndex) {
  1588. pixels[i] = 0;
  1589. } else {
  1590. pixels[i] = 1;
  1591. }
  1592. }
  1593. newMask.setPixels(0, y, mask.width, pixels, 0);
  1594. }
  1595. return newMask;
  1596. }
  1597. static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) {
  1598. if (pad == newPad) return data;
  1599. int stride = (width * depth + 7) / 8;
  1600. int bpl = (stride + (pad - 1)) / pad * pad;
  1601. int newBpl = (stride + (newPad - 1)) / newPad * newPad;
  1602. byte[] newData = new byte[height * newBpl];
  1603. int srcIndex = 0, destIndex = 0;
  1604. for (int y = 0; y < height; y++) {
  1605. System.arraycopy(data, srcIndex, newData, destIndex, stride);
  1606. srcIndex += bpl;
  1607. destIndex += newBpl;
  1608. }
  1609. return newData;
  1610. }
  1611. /**
  1612. * Blit operation bits to be OR'ed together to specify the desired operation.
  1613. */
  1614. static final int
  1615. BLIT_SRC = 1, // copy source directly, else applies logic operations
  1616. BLIT_ALPHA = 2, // enable alpha blending
  1617. BLIT_DITHER = 4; // enable dithering in low color modes
  1618. /**
  1619. * Alpha mode, values 0 - 255 specify global alpha level
  1620. */
  1621. static final int
  1622. ALPHA_OPAQUE = 255, // Fully opaque (ignores any alpha data)
  1623. ALPHA_TRANSPARENT = 0, // Fully transparent (ignores any alpha data)
  1624. ALPHA_CHANNEL_SEPARATE = -1, // Use alpha channel from separate alphaData
  1625. ALPHA_CHANNEL_SOURCE = -2, // Use alpha channel embedded in sourceData
  1626. ALPHA_MASK_UNPACKED = -3, // Use transparency mask formed by bytes in alphaData (non-zero is opaque)
  1627. ALPHA_MASK_PACKED = -4, // Use transparency mask formed by packed bits in alphaData
  1628. ALPHA_MASK_INDEX = -5, // Consider source palette indices transparent if in alphaData array
  1629. ALPHA_MASK_RGB = -6; // Consider source RGBs transparent if in RGB888 format alphaData array
  1630. /**
  1631. * Byte and bit order constants.
  1632. */
  1633. static final int LSB_FIRST = 0;
  1634. static final int MSB_FIRST = 1;
  1635. /**
  1636. * Data types (internal)
  1637. */
  1638. private static final int
  1639. // direct / true color formats with arbitrary masks & shifts
  1640. TYPE_GENERIC_8 = 0,
  1641. TYPE_GENERIC_16_MSB = 1,
  1642. TYPE_GENERIC_16_LSB = 2,
  1643. TYPE_GENERIC_24 = 3,
  1644. TYPE_GENERIC_32_MSB = 4,
  1645. TYPE_GENERIC_32_LSB = 5,
  1646. // palette indexed color formats
  1647. TYPE_INDEX_8 = 6,
  1648. TYPE_INDEX_4 = 7,
  1649. TYPE_INDEX_2 = 8,
  1650. TYPE_INDEX_1_MSB = 9,
  1651. TYPE_INDEX_1_LSB = 10;
  1652. /**
  1653. * Blits a direct palette image into a direct palette image.
  1654. * <p>
  1655. * Note: When the source and destination depth, order and masks
  1656. * are pairwise equal and the blitter operation is BLIT_SRC,
  1657. * the masks are ignored. Hence when not changing the image
  1658. * data format, 0 may be specified for the masks.
  1659. * </p>
  1660. *
  1661. * @param op the blitter operation: a combination of BLIT_xxx flags
  1662. * (see BLIT_xxx constants)
  1663. * @param srcData the source byte array containing image data
  1664. * @param srcDepth the source depth: one of 8, 16, 24, 32
  1665. * @param srcStride the source number of bytes per line
  1666. * @param srcOrder the source byte ordering: one of MSB_FIRST or LSB_FIRST;
  1667. * ignored if srcDepth is not 16 or 32
  1668. * @param srcX the top-left x-coord of the source blit region
  1669. * @param srcY the top-left y-coord of the source blit region
  1670. * @param srcWidth the width of the source blit region
  1671. * @param srcHeight the height of the source blit region
  1672. * @param srcRedMask the source red channel mask
  1673. * @param srcGreenMask the source green channel mask
  1674. * @param srcBlueMask the source blue channel mask
  1675. * @param alphaMode the alpha blending or mask mode, may be
  1676. * an integer 0-255 for global alpha; ignored if BLIT_ALPHA
  1677. * not specified in the blitter operations
  1678. * (see ALPHA_MODE_xxx constants)
  1679. * @param alphaData the alpha blending or mask data, varies depending
  1680. * on the value of alphaMode and sometimes ignored
  1681. * @param alphaStride the alpha data number of bytes per line
  1682. * @param alphaX the top-left x-coord of the alpha blit region
  1683. * @param alphaY the top-left y-coord of the alpha blit region
  1684. * @param destData the destination byte array containing image data
  1685. * @param destDepth the destination depth: one of 8, 16, 24, 32
  1686. * @param destStride the destination number of bytes per line
  1687. * @param destOrder the destination byte ordering: one of MSB_FIRST or LSB_FIRST;
  1688. * ignored if destDepth is not 16 or 32
  1689. * @param destX the top-left x-coord of the destination blit region
  1690. * @param destY the top-left y-coord of the destination blit region
  1691. * @param destWidth the width of the destination blit region
  1692. * @param destHeight the height of the destination blit region
  1693. * @param destRedMask the destination red channel mask
  1694. * @param destGreenMask the destination green channel mask
  1695. * @param destBlueMask the destination blue channel mask
  1696. * @param flipX if true the resulting image is flipped along the vertical axis
  1697. * @param flipY if true the resulting image is flipped along the horizontal axis
  1698. */
  1699. static void blit(int op,
  1700. byte[] srcData, int srcDepth, int srcStride, int srcOrder,
  1701. int srcX, int srcY, int srcWidth, int srcHeight,
  1702. int srcRedMask, int srcGreenMask, int srcBlueMask,
  1703. int alphaMode, byte[] alphaData, int alphaStride, int alphaX, int alphaY,
  1704. byte[] destData, int destDepth, int destStride, int destOrder,
  1705. int destX, int destY, int destWidth, int destHeight,
  1706. int destRedMask, int destGreenMask, int destBlueMask,
  1707. boolean flipX, boolean flipY) {
  1708. if ((destWidth <= 0) || (destHeight <= 0) || (alphaMode == ALPHA_TRANSPARENT)) return;
  1709. // these should be supplied as params later
  1710. final int srcAlphaMask = 0, destAlphaMask = 0;
  1711. /*** Prepare scaling data ***/
  1712. final int dwm1 = destWidth - 1;
  1713. final int sfxi = (dwm1 != 0) ? (int)((((long)srcWidth << 16) - 1) / dwm1) : 0;
  1714. final int dhm1 = destHeight - 1;
  1715. final int sfyi = (dhm1 != 0) ? (int)((((long)srcHeight << 16) - 1) / dhm1) : 0;
  1716. /*** Prepare source-related data ***/
  1717. final int sbpp, stype;
  1718. switch (srcDepth) {
  1719. case 8:
  1720. sbpp = 1;
  1721. stype = TYPE_GENERIC_8;
  1722. break;
  1723. case 16:
  1724. sbpp = 2;
  1725. stype = (srcOrder == MSB_FIRST) ? TYPE_GENERIC_16_MSB : TYPE_GENERIC_16_LSB;
  1726. break;
  1727. case 24:
  1728. sbpp = 3;
  1729. stype = TYPE_GENERIC_24;
  1730. break;
  1731. case 32:
  1732. sbpp = 4;
  1733. stype = (srcOrder == MSB_FIRST) ? TYPE_GENERIC_32_MSB : TYPE_GENERIC_32_LSB;
  1734. break;
  1735. default:
  1736. //throw new IllegalArgumentException("Invalid source type");
  1737. return;
  1738. }
  1739. int spr = srcY * srcStride + srcX * sbpp;
  1740. /*** Prepare destination-related data ***/
  1741. final int dbpp, dtype;
  1742. switch (destDepth) {
  1743. case 8:
  1744. dbpp = 1;
  1745. dtype = TYPE_GENERIC_8;
  1746. break;
  1747. case 16:
  1748. dbpp = 2;
  1749. dtype = (destOrder == MSB_FIRST) ? TYPE_GENERIC_16_MSB : TYPE_GENERIC_16_LSB;
  1750. break;
  1751. case 24:
  1752. dbpp = 3;
  1753. dtype = TYPE_GENERIC_24;
  1754. break;
  1755. case 32:
  1756. dbpp = 4;
  1757. dtype = (destOrder == MSB_FIRST) ? TYPE_GENERIC_32_MSB : TYPE_GENERIC_32_LSB;
  1758. break;
  1759. default:
  1760. //throw new IllegalArgumentException("Invalid destination type");
  1761. return;
  1762. }
  1763. int dpr = ((flipY) ? destY + dhm1 : destY) * destStride + ((flipX) ? destX + dwm1 : destX) * dbpp;
  1764. final int dprxi = (flipX) ? -dbpp : dbpp;
  1765. final int dpryi = (flipY) ? -destStride : destStride;
  1766. /*** Prepare special processing data ***/
  1767. int apr;
  1768. if ((op & BLIT_ALPHA) != 0) {
  1769. switch (alphaMode) {
  1770. case ALPHA_MASK_UNPACKED:
  1771. case ALPHA_CHANNEL_SEPARATE:
  1772. if (alphaData == null) alphaMode = 0x10000;
  1773. apr = alphaY * alphaStride + alphaX;
  1774. break;
  1775. case ALPHA_MASK_PACKED:
  1776. if (alphaData == null) alphaMode = 0x10000;
  1777. alphaStride <<= 3;
  1778. apr = alphaY * alphaStride + alphaX;
  1779. break;
  1780. case ALPHA_MASK_INDEX:
  1781. //throw new IllegalArgumentException("Invalid alpha type");
  1782. return;
  1783. case ALPHA_MASK_RGB:
  1784. if (alphaData == null) alphaMode = 0x10000;
  1785. apr = 0;
  1786. break;
  1787. default:
  1788. alphaMode = (alphaMode << 16) / 255; // prescale
  1789. case ALPHA_CHANNEL_SOURCE:
  1790. apr = 0;
  1791. break;
  1792. }
  1793. } else {
  1794. alphaMode = 0x10000;
  1795. apr = 0;
  1796. }
  1797. /*** Blit ***/
  1798. int dp = dpr;
  1799. int sp = spr;
  1800. if ((alphaMode == 0x10000) && (stype == dtype) &&
  1801. (srcRedMask == destRedMask) && (srcGreenMask == destGreenMask) &&
  1802. (srcBlueMask == destBlueMask) && (srcAlphaMask == destAlphaMask)) {
  1803. /*** Fast blit (straight copy) ***/
  1804. switch (sbpp) {
  1805. case 1:
  1806. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  1807. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  1808. destData[dp] = srcData[sp];
  1809. sp += (sfx >>> 16);
  1810. }
  1811. }
  1812. break;
  1813. case 2:
  1814. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  1815. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  1816. destData[dp] = srcData[sp];
  1817. destData[dp + 1] = srcData[sp + 1];
  1818. sp += (sfx >>> 16) * 2;
  1819. }
  1820. }
  1821. break;
  1822. case 3:
  1823. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  1824. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  1825. destData[dp] = srcData[sp];
  1826. destData[dp + 1] = srcData[sp + 1];
  1827. destData[dp + 2] = srcData[sp + 2];
  1828. sp += (sfx >>> 16) * 3;
  1829. }
  1830. }
  1831. break;
  1832. case 4:
  1833. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  1834. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  1835. destData[dp] = srcData[sp];
  1836. destData[dp + 1] = srcData[sp + 1];
  1837. destData[dp + 2] = srcData[sp + 2];
  1838. destData[dp + 3] = srcData[sp + 3];
  1839. sp += (sfx >>> 16) * 4;
  1840. }
  1841. }
  1842. break;
  1843. }
  1844. return;
  1845. }
  1846. /*Fast 32 to 32 blit */
  1847. if (alphaMode == 0x10000 && stype == TYPE_GENERIC_32_MSB && dtype == TYPE_GENERIC_32_MSB) {
  1848. if (srcRedMask == 0xFF00 && srcGreenMask == 0xff0000 && srcBlueMask == 0xff000000 && destRedMask == 0xFF0000 && destGreenMask == 0xff00 && destBlueMask == 0xff) {
  1849. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  1850. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  1851. destData[dp] = srcData[sp + 3];
  1852. destData[dp + 1] = srcData[sp + 2];
  1853. destData[dp + 2] = srcData[sp + 1];
  1854. destData[dp + 3] = srcData[sp];
  1855. sp += (sfx >>> 16) * 4;
  1856. }
  1857. }
  1858. return;
  1859. }
  1860. }
  1861. /*Fast 24 to 32 blit */
  1862. if (alphaMode == 0x10000 && stype == TYPE_GENERIC_24 && dtype == TYPE_GENERIC_32_MSB) {
  1863. if (srcRedMask == 0xFF && srcGreenMask == 0xff00 && srcBlueMask == 0xff0000 && destRedMask == 0xFF0000 && destGreenMask == 0xff00 && destBlueMask == 0xff) {
  1864. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  1865. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  1866. destData[dp] = 0;
  1867. destData[dp + 1] = srcData[sp + 2];
  1868. destData[dp + 2] = srcData[sp + 1];
  1869. destData[dp + 3] = srcData[sp];
  1870. sp += (sfx >>> 16) * 3;
  1871. }
  1872. }
  1873. return;
  1874. }
  1875. }
  1876. /*** Comprehensive blit (apply transformations) ***/
  1877. final int srcRedShift = getChannelShift(srcRedMask);
  1878. final byte[] srcReds = ANY_TO_EIGHT[getChannelWidth(srcRedMask, srcRedShift)];
  1879. final int srcGreenShift = getChannelShift(srcGreenMask);
  1880. final byte[] srcGreens = ANY_TO_EIGHT[getChannelWidth(srcGreenMask, srcGreenShift)];
  1881. final int srcBlueShift = getChannelShift(srcBlueMask);
  1882. final byte[] srcBlues = ANY_TO_EIGHT[getChannelWidth(srcBlueMask, srcBlueShift)];
  1883. final int srcAlphaShift = getChannelShift(srcAlphaMask);
  1884. final byte[] srcAlphas = ANY_TO_EIGHT[getChannelWidth(srcAlphaMask, srcAlphaShift)];
  1885. final int destRedShift = getChannelShift(destRedMask);
  1886. final int destRedWidth = getChannelWidth(destRedMask, destRedShift);
  1887. final byte[] destReds = ANY_TO_EIGHT[destRedWidth];
  1888. final int destRedPreShift = 8 - destRedWidth;
  1889. final int destGreenShift = getChannelShift(destGreenMask);
  1890. final int destGreenWidth = getChannelWidth(destGreenMask, destGreenShift);
  1891. final byte[] destGreens = ANY_TO_EIGHT[destGreenWidth];
  1892. final int destGreenPreShift = 8 - destGreenWidth;
  1893. final int destBlueShift = getChannelShift(destBlueMask);
  1894. final int destBlueWidth = getChannelWidth(destBlueMask, destBlueShift);
  1895. final byte[] destBlues = ANY_TO_EIGHT[destBlueWidth];
  1896. final int destBluePreShift = 8 - destBlueWidth;
  1897. final int destAlphaShift = getChannelShift(destAlphaMask);
  1898. final int destAlphaWidth = getChannelWidth(destAlphaMask, destAlphaShift);
  1899. final byte[] destAlphas = ANY_TO_EIGHT[destAlphaWidth];
  1900. final int destAlphaPreShift = 8 - destAlphaWidth;
  1901. int ap = apr, alpha = alphaMode;
  1902. int r = 0, g = 0, b = 0, a = 0;
  1903. int rq = 0, gq = 0, bq = 0, aq = 0;
  1904. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy,
  1905. sp = spr += (sfy >>> 16) * srcStride,
  1906. ap = apr += (sfy >>> 16) * alphaStride,
  1907. sfy = (sfy & 0xffff) + sfyi,
  1908. dp = dpr += dpryi) {
  1909. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx,
  1910. dp += dprxi,
  1911. sfx = (sfx & 0xffff) + sfxi) {
  1912. /*** READ NEXT PIXEL ***/
  1913. switch (stype) {
  1914. case TYPE_GENERIC_8: {
  1915. final int data = srcData[sp] & 0xff;
  1916. sp += (sfx >>> 16);
  1917. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  1918. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  1919. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  1920. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  1921. } break;
  1922. case TYPE_GENERIC_16_MSB: {
  1923. final int data = ((srcData[sp] & 0xff) << 8) | (srcData[sp + 1] & 0xff);
  1924. sp += (sfx >>> 16) * 2;
  1925. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  1926. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  1927. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  1928. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  1929. } break;
  1930. case TYPE_GENERIC_16_LSB: {
  1931. final int data = ((srcData[sp + 1] & 0xff) << 8) | (srcData[sp] & 0xff);
  1932. sp += (sfx >>> 16) * 2;
  1933. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  1934. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  1935. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  1936. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  1937. } break;
  1938. case TYPE_GENERIC_24: {
  1939. final int data = (( ((srcData[sp] & 0xff) << 8) |
  1940. (srcData[sp + 1] & 0xff)) << 8) |
  1941. (srcData[sp + 2] & 0xff);
  1942. sp += (sfx >>> 16) * 3;
  1943. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  1944. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  1945. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  1946. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  1947. } break;
  1948. case TYPE_GENERIC_32_MSB: {
  1949. final int data = (( (( ((srcData[sp] & 0xff) << 8) |
  1950. (srcData[sp + 1] & 0xff)) << 8) |
  1951. (srcData[sp + 2] & 0xff)) << 8) |
  1952. (srcData[sp + 3] & 0xff);
  1953. sp += (sfx >>> 16) * 4;
  1954. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  1955. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  1956. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  1957. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  1958. } break;
  1959. case TYPE_GENERIC_32_LSB: {
  1960. final int data = (( (( ((srcData[sp + 3] & 0xff) << 8) |
  1961. (srcData[sp + 2] & 0xff)) << 8) |
  1962. (srcData[sp + 1] & 0xff)) << 8) |
  1963. (srcData[sp] & 0xff);
  1964. sp += (sfx >>> 16) * 4;
  1965. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  1966. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  1967. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  1968. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  1969. } break;
  1970. }
  1971. /*** DO SPECIAL PROCESSING IF REQUIRED ***/
  1972. switch (alphaMode) {
  1973. case ALPHA_CHANNEL_SEPARATE:
  1974. alpha = ((alphaData[ap] & 0xff) << 16) / 255;
  1975. ap += (sfx >> 16);
  1976. break;
  1977. case ALPHA_CHANNEL_SOURCE:
  1978. alpha = (a << 16) / 255;
  1979. break;
  1980. case ALPHA_MASK_UNPACKED:
  1981. alpha = (alphaData[ap] != 0) ? 0x10000 : 0;
  1982. ap += (sfx >> 16);
  1983. break;
  1984. case ALPHA_MASK_PACKED:
  1985. alpha = (alphaData[ap >> 3] << ((ap & 7) + 9)) & 0x10000;
  1986. ap += (sfx >> 16);
  1987. break;
  1988. case ALPHA_MASK_RGB:
  1989. alpha = 0x10000;
  1990. for (int i = 0; i < alphaData.length; i += 3) {
  1991. if ((r == alphaData[i]) && (g == alphaData[i + 1]) && (b == alphaData[i + 2])) {
  1992. alpha = 0x0000;
  1993. break;
  1994. }
  1995. }
  1996. break;
  1997. }
  1998. if (alpha != 0x10000) {
  1999. if (alpha == 0x0000) continue;
  2000. switch (dtype) {
  2001. case TYPE_GENERIC_8: {
  2002. final int data = destData[dp] & 0xff;
  2003. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2004. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2005. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2006. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2007. } break;
  2008. case TYPE_GENERIC_16_MSB: {
  2009. final int data = ((destData[dp] & 0xff) << 8) | (destData[dp + 1] & 0xff);
  2010. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2011. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2012. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2013. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2014. } break;
  2015. case TYPE_GENERIC_16_LSB: {
  2016. final int data = ((destData[dp + 1] & 0xff) << 8) | (destData[dp] & 0xff);
  2017. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2018. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2019. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2020. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2021. } break;
  2022. case TYPE_GENERIC_24: {
  2023. final int data = (( ((destData[dp] & 0xff) << 8) |
  2024. (destData[dp + 1] & 0xff)) << 8) |
  2025. (destData[dp + 2] & 0xff);
  2026. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2027. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2028. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2029. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2030. } break;
  2031. case TYPE_GENERIC_32_MSB: {
  2032. final int data = (( (( ((destData[dp] & 0xff) << 8) |
  2033. (destData[dp + 1] & 0xff)) << 8) |
  2034. (destData[dp + 2] & 0xff)) << 8) |
  2035. (destData[dp + 3] & 0xff);
  2036. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2037. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2038. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2039. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2040. } break;
  2041. case TYPE_GENERIC_32_LSB: {
  2042. final int data = (( (( ((destData[dp + 3] & 0xff) << 8) |
  2043. (destData[dp + 2] & 0xff)) << 8) |
  2044. (destData[dp + 1] & 0xff)) << 8) |
  2045. (destData[dp] & 0xff);
  2046. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2047. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2048. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2049. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2050. } break;
  2051. }
  2052. // Perform alpha blending
  2053. a = aq + ((a - aq) * alpha >> 16);
  2054. r = rq + ((r - rq) * alpha >> 16);
  2055. g = gq + ((g - gq) * alpha >> 16);
  2056. b = bq + ((b - bq) * alpha >> 16);
  2057. }
  2058. /*** WRITE NEXT PIXEL ***/
  2059. final int data =
  2060. (r >>> destRedPreShift << destRedShift) |
  2061. (g >>> destGreenPreShift << destGreenShift) |
  2062. (b >>> destBluePreShift << destBlueShift) |
  2063. (a >>> destAlphaPreShift << destAlphaShift);
  2064. switch (dtype) {
  2065. case TYPE_GENERIC_8: {
  2066. destData[dp] = (byte) data;
  2067. } break;
  2068. case TYPE_GENERIC_16_MSB: {
  2069. destData[dp] = (byte) (data >>> 8);
  2070. destData[dp + 1] = (byte) (data & 0xff);
  2071. } break;
  2072. case TYPE_GENERIC_16_LSB: {
  2073. destData[dp] = (byte) (data & 0xff);
  2074. destData[dp + 1] = (byte) (data >>> 8);
  2075. } break;
  2076. case TYPE_GENERIC_24: {
  2077. destData[dp] = (byte) (data >>> 16);
  2078. destData[dp + 1] = (byte) (data >>> 8);
  2079. destData[dp + 2] = (byte) (data & 0xff);
  2080. } break;
  2081. case TYPE_GENERIC_32_MSB: {
  2082. destData[dp] = (byte) (data >>> 24);
  2083. destData[dp + 1] = (byte) (data >>> 16);
  2084. destData[dp + 2] = (byte) (data >>> 8);
  2085. destData[dp + 3] = (byte) (data & 0xff);
  2086. } break;
  2087. case TYPE_GENERIC_32_LSB: {
  2088. destData[dp] = (byte) (data & 0xff);
  2089. destData[dp + 1] = (byte) (data >>> 8);
  2090. destData[dp + 2] = (byte) (data >>> 16);
  2091. destData[dp + 3] = (byte) (data >>> 24);
  2092. } break;
  2093. }
  2094. }
  2095. }
  2096. }
  2097. /**
  2098. * Blits an index palette image into an index palette image.
  2099. * <p>
  2100. * Note: The source and destination red, green, and blue
  2101. * arrays may be null if no alpha blending or dither is to be
  2102. * performed.
  2103. * </p>
  2104. *
  2105. * @param op the blitter operation: a combination of BLIT_xxx flags
  2106. * (see BLIT_xxx constants)
  2107. * @param srcData the source byte array containing image data
  2108. * @param srcDepth the source depth: one of 1, 2, 4, 8
  2109. * @param srcStride the source number of bytes per line
  2110. * @param srcOrder the source byte ordering: one of MSB_FIRST or LSB_FIRST;
  2111. * ignored if srcDepth is not 1
  2112. * @param srcX the top-left x-coord of the source blit region
  2113. * @param srcY the top-left y-coord of the source blit region
  2114. * @param srcWidth the width of the source blit region
  2115. * @param srcHeight the height of the source blit region
  2116. * @param srcReds the source palette red component intensities
  2117. * @param srcGreens the source palette green component intensities
  2118. * @param srcBlues the source palette blue component intensities
  2119. * @param alphaMode the alpha blending or mask mode, may be
  2120. * an integer 0-255 for global alpha; ignored if BLIT_ALPHA
  2121. * not specified in the blitter operations
  2122. * (see ALPHA_MODE_xxx constants)
  2123. * @param alphaData the alpha blending or mask data, varies depending
  2124. * on the value of alphaMode and sometimes ignored
  2125. * @param alphaStride the alpha data number of bytes per line
  2126. * @param alphaX the top-left x-coord of the alpha blit region
  2127. * @param alphaY the top-left y-coord of the alpha blit region
  2128. * @param destData the destination byte array containing image data
  2129. * @param destDepth the destination depth: one of 1, 2, 4, 8
  2130. * @param destStride the destination number of bytes per line
  2131. * @param destOrder the destination byte ordering: one of MSB_FIRST or LSB_FIRST;
  2132. * ignored if destDepth is not 1
  2133. * @param destX the top-left x-coord of the destination blit region
  2134. * @param destY the top-left y-coord of the destination blit region
  2135. * @param destWidth the width of the destination blit region
  2136. * @param destHeight the height of the destination blit region
  2137. * @param destReds the destination palette red component intensities
  2138. * @param destGreens the destination palette green component intensities
  2139. * @param destBlues the destination palette blue component intensities
  2140. * @param flipX if true the resulting image is flipped along the vertical axis
  2141. * @param flipY if true the resulting image is flipped along the horizontal axis
  2142. */
  2143. static void blit(int op,
  2144. byte[] srcData, int srcDepth, int srcStride, int srcOrder,
  2145. int srcX, int srcY, int srcWidth, int srcHeight,
  2146. byte[] srcReds, byte[] srcGreens, byte[] srcBlues,
  2147. int alphaMode, byte[] alphaData, int alphaStride, int alphaX, int alphaY,
  2148. byte[] destData, int destDepth, int destStride, int destOrder,
  2149. int destX, int destY, int destWidth, int destHeight,
  2150. byte[] destReds, byte[] destGreens, byte[] destBlues,
  2151. boolean flipX, boolean flipY) {
  2152. if ((destWidth <= 0) || (destHeight <= 0) || (alphaMode == ALPHA_TRANSPARENT)) return;
  2153. /*** Prepare scaling data ***/
  2154. final int dwm1 = destWidth - 1;
  2155. final int sfxi = (dwm1 != 0) ? (int)((((long)srcWidth << 16) - 1) / dwm1) : 0;
  2156. final int dhm1 = destHeight - 1;
  2157. final int sfyi = (dhm1 != 0) ? (int)((((long)srcHeight << 16) - 1) / dhm1) : 0;
  2158. /*** Prepare source-related data ***/
  2159. final int stype;
  2160. switch (srcDepth) {
  2161. case 8:
  2162. stype = TYPE_INDEX_8;
  2163. break;
  2164. case 4:
  2165. srcStride <<= 1;
  2166. stype = TYPE_INDEX_4;
  2167. break;
  2168. case 2:
  2169. srcStride <<= 2;
  2170. stype = TYPE_INDEX_2;
  2171. break;
  2172. case 1:
  2173. srcStride <<= 3;
  2174. stype = (srcOrder == MSB_FIRST) ? TYPE_INDEX_1_MSB : TYPE_INDEX_1_LSB;
  2175. break;
  2176. default:
  2177. //throw new IllegalArgumentException("Invalid source type");
  2178. return;
  2179. }
  2180. int spr = srcY * srcStride + srcX;
  2181. /*** Prepare destination-related data ***/
  2182. final int dtype;
  2183. switch (destDepth) {
  2184. case 8:
  2185. dtype = TYPE_INDEX_8;
  2186. break;
  2187. case 4:
  2188. destStride <<= 1;
  2189. dtype = TYPE_INDEX_4;
  2190. break;
  2191. case 2:
  2192. destStride <<= 2;
  2193. dtype = TYPE_INDEX_2;
  2194. break;
  2195. case 1:
  2196. destStride <<= 3;
  2197. dtype = (destOrder == MSB_FIRST) ? TYPE_INDEX_1_MSB : TYPE_INDEX_1_LSB;
  2198. break;
  2199. default:
  2200. //throw new IllegalArgumentException("Invalid source type");
  2201. return;
  2202. }
  2203. int dpr = ((flipY) ? destY + dhm1 : destY) * destStride + ((flipX) ? destX + dwm1 : destX);
  2204. final int dprxi = (flipX) ? -1 : 1;
  2205. final int dpryi = (flipY) ? -destStride : destStride;
  2206. /*** Prepare special processing data ***/
  2207. int apr;
  2208. if ((op & BLIT_ALPHA) != 0) {
  2209. switch (alphaMode) {
  2210. case ALPHA_MASK_UNPACKED:
  2211. case ALPHA_CHANNEL_SEPARATE:
  2212. if (alphaData == null) alphaMode = 0x10000;
  2213. apr = alphaY * alphaStride + alphaX;
  2214. break;
  2215. case ALPHA_MASK_PACKED:
  2216. if (alphaData == null) alphaMode = 0x10000;
  2217. alphaStride <<= 3;
  2218. apr = alphaY * alphaStride + alphaX;
  2219. break;
  2220. case ALPHA_MASK_INDEX:
  2221. case ALPHA_MASK_RGB:
  2222. if (alphaData == null) alphaMode = 0x10000;
  2223. apr = 0;
  2224. break;
  2225. default:
  2226. alphaMode = (alphaMode << 16) / 255; // prescale
  2227. case ALPHA_CHANNEL_SOURCE:
  2228. apr = 0;
  2229. break;
  2230. }
  2231. } else {
  2232. alphaMode = 0x10000;
  2233. apr = 0;
  2234. }
  2235. final boolean ditherEnabled = (op & BLIT_DITHER) != 0;
  2236. /*** Blit ***/
  2237. int dp = dpr;
  2238. int sp = spr;
  2239. int ap = apr;
  2240. int destPaletteSize = 1 << destDepth;
  2241. if ((destReds != null) && (destReds.length < destPaletteSize)) destPaletteSize = destReds.length;
  2242. byte[] paletteMapping = null;
  2243. boolean isExactPaletteMapping = true;
  2244. switch (alphaMode) {
  2245. case 0x10000:
  2246. /*** If the palettes and formats are equivalent use a one-to-one mapping ***/
  2247. if ((stype == dtype) &&
  2248. (srcReds == destReds) && (srcGreens == destGreens) && (srcBlues == destBlues)) {
  2249. paletteMapping = ONE_TO_ONE_MAPPING;
  2250. break;
  2251. /*** If palettes have not been supplied, supply a suitable mapping ***/
  2252. } else if ((srcReds == null) || (destReds == null)) {
  2253. if (srcDepth <= destDepth) {
  2254. paletteMapping = ONE_TO_ONE_MAPPING;
  2255. } else {
  2256. paletteMapping = new byte[1 << srcDepth];
  2257. int mask = (0xff << destDepth) >>> 8;
  2258. for (int i = 0; i < paletteMapping.length; ++i) paletteMapping[i] = (byte)(i & mask);
  2259. }
  2260. break;
  2261. }
  2262. case ALPHA_MASK_UNPACKED:
  2263. case ALPHA_MASK_PACKED:
  2264. case ALPHA_MASK_INDEX:
  2265. case ALPHA_MASK_RGB:
  2266. /*** Generate a palette mapping ***/
  2267. int srcPaletteSize = 1 << srcDepth;
  2268. paletteMapping = new byte[srcPaletteSize];
  2269. if ((srcReds != null) && (srcReds.length < srcPaletteSize)) srcPaletteSize = srcReds.length;
  2270. for (int i = 0, r, g, b, index; i < srcPaletteSize; ++i) {
  2271. r = srcReds[i] & 0xff;
  2272. g = srcGreens[i] & 0xff;
  2273. b = srcBlues[i] & 0xff;
  2274. index = 0;
  2275. int minDistance = 0x7fffffff;
  2276. for (int j = 0, dr, dg, db, distance; j < destPaletteSize; ++j) {
  2277. dr = (destReds[j] & 0xff) - r;
  2278. dg = (destGreens[j] & 0xff) - g;
  2279. db = (destBlues[j] & 0xff) - b;
  2280. distance = dr * dr + dg * dg + db * db;
  2281. if (distance < minDistance) {
  2282. index = j;
  2283. if (distance == 0) break;
  2284. minDistance = distance;
  2285. }
  2286. }
  2287. paletteMapping[i] = (byte)index;
  2288. if (minDistance != 0) isExactPaletteMapping = false;
  2289. }
  2290. break;
  2291. }
  2292. if ((paletteMapping != null) && (isExactPaletteMapping || ! ditherEnabled)) {
  2293. if ((stype == dtype) && (alphaMode == 0x10000)) {
  2294. /*** Fast blit (copy w/ mapping) ***/
  2295. switch (stype) {
  2296. case TYPE_INDEX_8:
  2297. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  2298. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  2299. destData[dp] = paletteMapping[srcData[sp] & 0xff];
  2300. sp += (sfx >>> 16);
  2301. }
  2302. }
  2303. break;
  2304. case TYPE_INDEX_4:
  2305. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  2306. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  2307. final int v;
  2308. if ((sp & 1) != 0) v = paletteMapping[srcData[sp >> 1] & 0x0f];
  2309. else v = (srcData[sp >> 1] >>> 4) & 0x0f;
  2310. sp += (sfx >>> 16);
  2311. if ((dp & 1) != 0) destData[dp >> 1] = (byte)((destData[dp >> 1] & 0xf0) | v);
  2312. else destData[dp >> 1] = (byte)((destData[dp >> 1] & 0x0f) | (v << 4));
  2313. }
  2314. }
  2315. break;
  2316. case TYPE_INDEX_2:
  2317. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  2318. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  2319. final int index = paletteMapping[(srcData[sp >> 2] >>> (6 - (sp & 3) * 2)) & 0x03];
  2320. sp += (sfx >>> 16);
  2321. final int shift = 6 - (dp & 3) * 2;
  2322. destData[dp >> 2] = (byte)(destData[dp >> 2] & ~(0x03 << shift) | (index << shift));
  2323. }
  2324. }
  2325. break;
  2326. case TYPE_INDEX_1_MSB:
  2327. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  2328. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  2329. final int index = paletteMapping[(srcData[sp >> 3] >>> (7 - (sp & 7))) & 0x01];
  2330. sp += (sfx >>> 16);
  2331. final int shift = 7 - (dp & 7);
  2332. destData[dp >> 3] = (byte)(destData[dp >> 3] & ~(0x01 << shift) | (index << shift));
  2333. }
  2334. }
  2335. break;
  2336. case TYPE_INDEX_1_LSB:
  2337. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy, sp = spr += (sfy >>> 16) * srcStride, sfy = (sfy & 0xffff) + sfyi, dp = dpr += dpryi) {
  2338. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx, dp += dprxi, sfx = (sfx & 0xffff) + sfxi) {
  2339. final int index = paletteMapping[(srcData[sp >> 3] >>> (sp & 7)) & 0x01];
  2340. sp += (sfx >>> 16);
  2341. final int shift = dp & 7;
  2342. destData[dp >> 3] = (byte)(destData[dp >> 3] & ~(0x01 << shift) | (index << shift));
  2343. }
  2344. }
  2345. break;
  2346. }
  2347. } else {
  2348. /*** Convert between indexed modes using mapping and mask ***/
  2349. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy,
  2350. sp = spr += (sfy >>> 16) * srcStride,
  2351. sfy = (sfy & 0xffff) + sfyi,
  2352. dp = dpr += dpryi) {
  2353. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx,
  2354. dp += dprxi,
  2355. sfx = (sfx & 0xffff) + sfxi) {
  2356. int index;
  2357. /*** READ NEXT PIXEL ***/
  2358. switch (stype) {
  2359. case TYPE_INDEX_8:
  2360. index = srcData[sp] & 0xff;
  2361. sp += (sfx >>> 16);
  2362. break;
  2363. case TYPE_INDEX_4:
  2364. if ((sp & 1) != 0) index = srcData[sp >> 1] & 0x0f;
  2365. else index = (srcData[sp >> 1] >>> 4) & 0x0f;
  2366. sp += (sfx >>> 16);
  2367. break;
  2368. case TYPE_INDEX_2:
  2369. index = (srcData[sp >> 2] >>> (6 - (sp & 3) * 2)) & 0x03;
  2370. sp += (sfx >>> 16);
  2371. break;
  2372. case TYPE_INDEX_1_MSB:
  2373. index = (srcData[sp >> 3] >>> (7 - (sp & 7))) & 0x01;
  2374. sp += (sfx >>> 16);
  2375. break;
  2376. case TYPE_INDEX_1_LSB:
  2377. index = (srcData[sp >> 3] >>> (sp & 7)) & 0x01;
  2378. sp += (sfx >>> 16);
  2379. break;
  2380. default:
  2381. return;
  2382. }
  2383. /*** APPLY MASK ***/
  2384. switch (alphaMode) {
  2385. case ALPHA_MASK_UNPACKED: {
  2386. final byte mask = alphaData[ap];
  2387. ap += (sfx >> 16);
  2388. if (mask == 0) continue;
  2389. } break;
  2390. case ALPHA_MASK_PACKED: {
  2391. final int mask = alphaData[ap >> 3] & (1 << (ap & 7));
  2392. ap += (sfx >> 16);
  2393. if (mask == 0) continue;
  2394. } break;
  2395. case ALPHA_MASK_INDEX: {
  2396. int i = 0;
  2397. while (i < alphaData.length) {
  2398. if (index == (alphaData[i] & 0xff)) break;
  2399. }
  2400. if (i < alphaData.length) continue;
  2401. } break;
  2402. case ALPHA_MASK_RGB: {
  2403. final byte r = srcReds[index], g = srcGreens[index], b = srcBlues[index];
  2404. int i = 0;
  2405. while (i < alphaData.length) {
  2406. if ((r == alphaData[i]) && (g == alphaData[i + 1]) && (b == alphaData[i + 2])) break;
  2407. i += 3;
  2408. }
  2409. if (i < alphaData.length) continue;
  2410. } break;
  2411. }
  2412. index = paletteMapping[index] & 0xff;
  2413. /*** WRITE NEXT PIXEL ***/
  2414. switch (dtype) {
  2415. case TYPE_INDEX_8:
  2416. destData[dp] = (byte) index;
  2417. break;
  2418. case TYPE_INDEX_4:
  2419. if ((dp & 1) != 0) destData[dp >> 1] = (byte)((destData[dp >> 1] & 0xf0) | index);
  2420. else destData[dp >> 1] = (byte)((destData[dp >> 1] & 0x0f) | (index << 4));
  2421. break;
  2422. case TYPE_INDEX_2: {
  2423. final int shift = 6 - (dp & 3) * 2;
  2424. destData[dp >> 2] = (byte)(destData[dp >> 2] & ~(0x03 << shift) | (index << shift));
  2425. } break;
  2426. case TYPE_INDEX_1_MSB: {
  2427. final int shift = 7 - (dp & 7);
  2428. destData[dp >> 3] = (byte)(destData[dp >> 3] & ~(0x01 << shift) | (index << shift));
  2429. } break;
  2430. case TYPE_INDEX_1_LSB: {
  2431. final int shift = dp & 7;
  2432. destData[dp >> 3] = (byte)(destData[dp >> 3] & ~(0x01 << shift) | (index << shift));
  2433. } break;
  2434. }
  2435. }
  2436. }
  2437. }
  2438. return;
  2439. }
  2440. /*** Comprehensive blit (apply transformations) ***/
  2441. int alpha = alphaMode;
  2442. int index = 0;
  2443. int indexq = 0;
  2444. int lastindex = 0, lastr = -1, lastg = -1, lastb = -1;
  2445. final int[] rerr, gerr, berr;
  2446. if (ditherEnabled) {
  2447. rerr = new int[destWidth + 2];
  2448. gerr = new int[destWidth + 2];
  2449. berr = new int[destWidth + 2];
  2450. } else {
  2451. rerr = null; gerr = null; berr = null;
  2452. }
  2453. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy,
  2454. sp = spr += (sfy >>> 16) * srcStride,
  2455. ap = apr += (sfy >>> 16) * alphaStride,
  2456. sfy = (sfy & 0xffff) + sfyi,
  2457. dp = dpr += dpryi) {
  2458. int lrerr = 0, lgerr = 0, lberr = 0;
  2459. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx,
  2460. dp += dprxi,
  2461. sfx = (sfx & 0xffff) + sfxi) {
  2462. /*** READ NEXT PIXEL ***/
  2463. switch (stype) {
  2464. case TYPE_INDEX_8:
  2465. index = srcData[sp] & 0xff;
  2466. sp += (sfx >>> 16);
  2467. break;
  2468. case TYPE_INDEX_4:
  2469. if ((sp & 1) != 0) index = srcData[sp >> 1] & 0x0f;
  2470. else index = (srcData[sp >> 1] >>> 4) & 0x0f;
  2471. sp += (sfx >>> 16);
  2472. break;
  2473. case TYPE_INDEX_2:
  2474. index = (srcData[sp >> 2] >>> (6 - (sp & 3) * 2)) & 0x03;
  2475. sp += (sfx >>> 16);
  2476. break;
  2477. case TYPE_INDEX_1_MSB:
  2478. index = (srcData[sp >> 3] >>> (7 - (sp & 7))) & 0x01;
  2479. sp += (sfx >>> 16);
  2480. break;
  2481. case TYPE_INDEX_1_LSB:
  2482. index = (srcData[sp >> 3] >>> (sp & 7)) & 0x01;
  2483. sp += (sfx >>> 16);
  2484. break;
  2485. }
  2486. /*** DO SPECIAL PROCESSING IF REQUIRED ***/
  2487. int r = srcReds[index] & 0xff, g = srcGreens[index] & 0xff, b = srcBlues[index] & 0xff;
  2488. switch (alphaMode) {
  2489. case ALPHA_CHANNEL_SEPARATE:
  2490. alpha = ((alphaData[ap] & 0xff) << 16) / 255;
  2491. ap += (sfx >> 16);
  2492. break;
  2493. case ALPHA_MASK_UNPACKED:
  2494. alpha = (alphaData[ap] != 0) ? 0x10000 : 0;
  2495. ap += (sfx >> 16);
  2496. break;
  2497. case ALPHA_MASK_PACKED:
  2498. alpha = (alphaData[ap >> 3] << ((ap & 7) + 9)) & 0x10000;
  2499. ap += (sfx >> 16);
  2500. break;
  2501. case ALPHA_MASK_INDEX: { // could speed up using binary search if we sorted the indices
  2502. int i = 0;
  2503. while (i < alphaData.length) {
  2504. if (index == (alphaData[i] & 0xff)) break;
  2505. }
  2506. if (i < alphaData.length) continue;
  2507. } break;
  2508. case ALPHA_MASK_RGB: {
  2509. int i = 0;
  2510. while (i < alphaData.length) {
  2511. if ((r == (alphaData[i] & 0xff)) &&
  2512. (g == (alphaData[i + 1] & 0xff)) &&
  2513. (b == (alphaData[i + 2] & 0xff))) break;
  2514. i += 3;
  2515. }
  2516. if (i < alphaData.length) continue;
  2517. } break;
  2518. }
  2519. if (alpha != 0x10000) {
  2520. if (alpha == 0x0000) continue;
  2521. switch (dtype) {
  2522. case TYPE_INDEX_8:
  2523. indexq = destData[dp] & 0xff;
  2524. break;
  2525. case TYPE_INDEX_4:
  2526. if ((dp & 1) != 0) indexq = destData[dp >> 1] & 0x0f;
  2527. else indexq = (destData[dp >> 1] >>> 4) & 0x0f;
  2528. break;
  2529. case TYPE_INDEX_2:
  2530. indexq = (destData[dp >> 2] >>> (6 - (dp & 3) * 2)) & 0x03;
  2531. break;
  2532. case TYPE_INDEX_1_MSB:
  2533. indexq = (destData[dp >> 3] >>> (7 - (dp & 7))) & 0x01;
  2534. break;
  2535. case TYPE_INDEX_1_LSB:
  2536. indexq = (destData[dp >> 3] >>> (dp & 7)) & 0x01;
  2537. break;
  2538. }
  2539. // Perform alpha blending
  2540. final int rq = destReds[indexq] & 0xff;
  2541. final int gq = destGreens[indexq] & 0xff;
  2542. final int bq = destBlues[indexq] & 0xff;
  2543. r = rq + ((r - rq) * alpha >> 16);
  2544. g = gq + ((g - gq) * alpha >> 16);
  2545. b = bq + ((b - bq) * alpha >> 16);
  2546. }
  2547. /*** MAP COLOR TO THE PALETTE ***/
  2548. if (ditherEnabled) {
  2549. // Floyd-Steinberg error diffusion
  2550. r += rerr[dx] >> 4;
  2551. if (r < 0) r = 0; else if (r > 255) r = 255;
  2552. g += gerr[dx] >> 4;
  2553. if (g < 0) g = 0; else if (g > 255) g = 255;
  2554. b += berr[dx] >> 4;
  2555. if (b < 0) b = 0; else if (b > 255) b = 255;
  2556. rerr[dx] = lrerr;
  2557. gerr[dx] = lgerr;
  2558. berr[dx] = lberr;
  2559. }
  2560. if (r != lastr || g != lastg || b != lastb) {
  2561. // moving the variable declarations out seems to make the JDK JIT happier...
  2562. for (int j = 0, dr, dg, db, distance, minDistance = 0x7fffffff; j < destPaletteSize; ++j) {
  2563. dr = (destReds[j] & 0xff) - r;
  2564. dg = (destGreens[j] & 0xff) - g;
  2565. db = (destBlues[j] & 0xff) - b;
  2566. distance = dr * dr + dg * dg + db * db;
  2567. if (distance < minDistance) {
  2568. lastindex = j;
  2569. if (distance == 0) break;
  2570. minDistance = distance;
  2571. }
  2572. }
  2573. lastr = r; lastg = g; lastb = b;
  2574. }
  2575. if (ditherEnabled) {
  2576. // Floyd-Steinberg error diffusion, cont'd...
  2577. final int dxm1 = dx - 1, dxp1 = dx + 1;
  2578. int acc;
  2579. rerr[dxp1] += acc = (lrerr = r - (destReds[lastindex] & 0xff)) + lrerr + lrerr;
  2580. rerr[dx] += acc += lrerr + lrerr;
  2581. rerr[dxm1] += acc + lrerr + lrerr;
  2582. gerr[dxp1] += acc = (lgerr = g - (destGreens[lastindex] & 0xff)) + lgerr + lgerr;
  2583. gerr[dx] += acc += lgerr + lgerr;
  2584. gerr[dxm1] += acc + lgerr + lgerr;
  2585. berr[dxp1] += acc = (lberr = b - (destBlues[lastindex] & 0xff)) + lberr + lberr;
  2586. berr[dx] += acc += lberr + lberr;
  2587. berr[dxm1] += acc + lberr + lberr;
  2588. }
  2589. /*** WRITE NEXT PIXEL ***/
  2590. switch (dtype) {
  2591. case TYPE_INDEX_8:
  2592. destData[dp] = (byte) lastindex;
  2593. break;
  2594. case TYPE_INDEX_4:
  2595. if ((dp & 1) != 0) destData[dp >> 1] = (byte)((destData[dp >> 1] & 0xf0) | lastindex);
  2596. else destData[dp >> 1] = (byte)((destData[dp >> 1] & 0x0f) | (lastindex << 4));
  2597. break;
  2598. case TYPE_INDEX_2: {
  2599. final int shift = 6 - (dp & 3) * 2;
  2600. destData[dp >> 2] = (byte)(destData[dp >> 2] & ~(0x03 << shift) | (lastindex << shift));
  2601. } break;
  2602. case TYPE_INDEX_1_MSB: {
  2603. final int shift = 7 - (dp & 7);
  2604. destData[dp >> 3] = (byte)(destData[dp >> 3] & ~(0x01 << shift) | (lastindex << shift));
  2605. } break;
  2606. case TYPE_INDEX_1_LSB: {
  2607. final int shift = dp & 7;
  2608. destData[dp >> 3] = (byte)(destData[dp >> 3] & ~(0x01 << shift) | (lastindex << shift));
  2609. } break;
  2610. }
  2611. }
  2612. }
  2613. }
  2614. /**
  2615. * Blits an index palette image into a direct palette image.
  2616. * <p>
  2617. * Note: The source and destination masks and palettes must
  2618. * always be fully specified.
  2619. * </p>
  2620. *
  2621. * @param op the blitter operation: a combination of BLIT_xxx flags
  2622. * (see BLIT_xxx constants)
  2623. * @param srcData the source byte array containing image data
  2624. * @param srcDepth the source depth: one of 1, 2, 4, 8
  2625. * @param srcStride the source number of bytes per line
  2626. * @param srcOrder the source byte ordering: one of MSB_FIRST or LSB_FIRST;
  2627. * ignored if srcDepth is not 1
  2628. * @param srcX the top-left x-coord of the source blit region
  2629. * @param srcY the top-left y-coord of the source blit region
  2630. * @param srcWidth the width of the source blit region
  2631. * @param srcHeight the height of the source blit region
  2632. * @param srcReds the source palette red component intensities
  2633. * @param srcGreens the source palette green component intensities
  2634. * @param srcBlues the source palette blue component intensities
  2635. * @param alphaMode the alpha blending or mask mode, may be
  2636. * an integer 0-255 for global alpha; ignored if BLIT_ALPHA
  2637. * not specified in the blitter operations
  2638. * (see ALPHA_MODE_xxx constants)
  2639. * @param alphaData the alpha blending or mask data, varies depending
  2640. * on the value of alphaMode and sometimes ignored
  2641. * @param alphaStride the alpha data number of bytes per line
  2642. * @param alphaX the top-left x-coord of the alpha blit region
  2643. * @param alphaY the top-left y-coord of the alpha blit region
  2644. * @param destData the destination byte array containing image data
  2645. * @param destDepth the destination depth: one of 8, 16, 24, 32
  2646. * @param destStride the destination number of bytes per line
  2647. * @param destOrder the destination byte ordering: one of MSB_FIRST or LSB_FIRST;
  2648. * ignored if destDepth is not 16 or 32
  2649. * @param destX the top-left x-coord of the destination blit region
  2650. * @param destY the top-left y-coord of the destination blit region
  2651. * @param destWidth the width of the destination blit region
  2652. * @param destHeight the height of the destination blit region
  2653. * @param destRedMask the destination red channel mask
  2654. * @param destGreenMask the destination green channel mask
  2655. * @param destBlueMask the destination blue channel mask
  2656. * @param flipX if true the resulting image is flipped along the vertical axis
  2657. * @param flipY if true the resulting image is flipped along the horizontal axis
  2658. */
  2659. static void blit(int op,
  2660. byte[] srcData, int srcDepth, int srcStride, int srcOrder,
  2661. int srcX, int srcY, int srcWidth, int srcHeight,
  2662. byte[] srcReds, byte[] srcGreens, byte[] srcBlues,
  2663. int alphaMode, byte[] alphaData, int alphaStride, int alphaX, int alphaY,
  2664. byte[] destData, int destDepth, int destStride, int destOrder,
  2665. int destX, int destY, int destWidth, int destHeight,
  2666. int destRedMask, int destGreenMask, int destBlueMask,
  2667. boolean flipX, boolean flipY) {
  2668. if ((destWidth <= 0) || (destHeight <= 0) || (alphaMode == ALPHA_TRANSPARENT)) return;
  2669. /*** Fast blit (straight copy) ***/
  2670. if (srcX == 0 && srcY == 0 && destX == 0 && destY == 0 && destWidth == srcWidth && destHeight == srcHeight) {
  2671. if (destDepth == 24 && srcDepth == 8 && (op & BLIT_ALPHA) == 0 && destRedMask == 0xFF0000 && destGreenMask == 0xFF00 && destBlueMask == 0xFF) {
  2672. for (int y = 0, sp = 0, dp = 0, spad = srcStride - srcWidth, dpad = destStride - (destWidth * 3); y < destHeight; y++, sp += spad, dp += dpad) {
  2673. for (int x = 0; x < destWidth; x++) {
  2674. int index = srcData[sp++] & 0xff;
  2675. destData[dp++] = srcReds[index];
  2676. destData[dp++] = srcGreens[index];
  2677. destData[dp++] = srcBlues[index];
  2678. }
  2679. }
  2680. return;
  2681. }
  2682. if (destDepth == 32 && destOrder == MSB_FIRST && srcDepth == 8 && (op & BLIT_ALPHA) == 0 && destRedMask == 0xFF0000 && destGreenMask == 0xFF00 && destBlueMask == 0xFF) {
  2683. for (int y = 0, sp = 0, dp = 0, spad = srcStride - srcWidth, dpad = destStride - (destWidth * 4); y < destHeight; y++, sp += spad, dp += dpad) {
  2684. for (int x = 0; x < destWidth; x++) {
  2685. int index = srcData[sp++] & 0xff;
  2686. dp++;
  2687. destData[dp++] = srcReds[index];
  2688. destData[dp++] = srcGreens[index];
  2689. destData[dp++] = srcBlues[index];
  2690. }
  2691. }
  2692. return;
  2693. }
  2694. }
  2695. // these should be supplied as params later
  2696. final int destAlphaMask = 0;
  2697. /*** Prepare scaling data ***/
  2698. final int dwm1 = destWidth - 1;
  2699. final int sfxi = (dwm1 != 0) ? (int)((((long)srcWidth << 16) - 1) / dwm1) : 0;
  2700. final int dhm1 = destHeight - 1;
  2701. final int sfyi = (dhm1 != 0) ? (int)((((long)srcHeight << 16) - 1) / dhm1) : 0;
  2702. /*** Prepare source-related data ***/
  2703. final int stype;
  2704. switch (srcDepth) {
  2705. case 8:
  2706. stype = TYPE_INDEX_8;
  2707. break;
  2708. case 4:
  2709. srcStride <<= 1;
  2710. stype = TYPE_INDEX_4;
  2711. break;
  2712. case 2:
  2713. srcStride <<= 2;
  2714. stype = TYPE_INDEX_2;
  2715. break;
  2716. case 1:
  2717. srcStride <<= 3;
  2718. stype = (srcOrder == MSB_FIRST) ? TYPE_INDEX_1_MSB : TYPE_INDEX_1_LSB;
  2719. break;
  2720. default:
  2721. //throw new IllegalArgumentException("Invalid source type");
  2722. return;
  2723. }
  2724. int spr = srcY * srcStride + srcX;
  2725. /*** Prepare destination-related data ***/
  2726. final int dbpp, dtype;
  2727. switch (destDepth) {
  2728. case 8:
  2729. dbpp = 1;
  2730. dtype = TYPE_GENERIC_8;
  2731. break;
  2732. case 16:
  2733. dbpp = 2;
  2734. dtype = (destOrder == MSB_FIRST) ? TYPE_GENERIC_16_MSB : TYPE_GENERIC_16_LSB;
  2735. break;
  2736. case 24:
  2737. dbpp = 3;
  2738. dtype = TYPE_GENERIC_24;
  2739. break;
  2740. case 32:
  2741. dbpp = 4;
  2742. dtype = (destOrder == MSB_FIRST) ? TYPE_GENERIC_32_MSB : TYPE_GENERIC_32_LSB;
  2743. break;
  2744. default:
  2745. //throw new IllegalArgumentException("Invalid destination type");
  2746. return;
  2747. }
  2748. int dpr = ((flipY) ? destY + dhm1 : destY) * destStride + ((flipX) ? destX + dwm1 : destX) * dbpp;
  2749. final int dprxi = (flipX) ? -dbpp : dbpp;
  2750. final int dpryi = (flipY) ? -destStride : destStride;
  2751. /*** Prepare special processing data ***/
  2752. int apr;
  2753. if ((op & BLIT_ALPHA) != 0) {
  2754. switch (alphaMode) {
  2755. case ALPHA_MASK_UNPACKED:
  2756. case ALPHA_CHANNEL_SEPARATE:
  2757. if (alphaData == null) alphaMode = 0x10000;
  2758. apr = alphaY * alphaStride + alphaX;
  2759. break;
  2760. case ALPHA_MASK_PACKED:
  2761. if (alphaData == null) alphaMode = 0x10000;
  2762. alphaStride <<= 3;
  2763. apr = alphaY * alphaStride + alphaX;
  2764. break;
  2765. case ALPHA_MASK_INDEX:
  2766. case ALPHA_MASK_RGB:
  2767. if (alphaData == null) alphaMode = 0x10000;
  2768. apr = 0;
  2769. break;
  2770. default:
  2771. alphaMode = (alphaMode << 16) / 255; // prescale
  2772. case ALPHA_CHANNEL_SOURCE:
  2773. apr = 0;
  2774. break;
  2775. }
  2776. } else {
  2777. alphaMode = 0x10000;
  2778. apr = 0;
  2779. }
  2780. /*** Comprehensive blit (apply transformations) ***/
  2781. final int destRedShift = getChannelShift(destRedMask);
  2782. final int destRedWidth = getChannelWidth(destRedMask, destRedShift);
  2783. final byte[] destReds = ANY_TO_EIGHT[destRedWidth];
  2784. final int destRedPreShift = 8 - destRedWidth;
  2785. final int destGreenShift = getChannelShift(destGreenMask);
  2786. final int destGreenWidth = getChannelWidth(destGreenMask, destGreenShift);
  2787. final byte[] destGreens = ANY_TO_EIGHT[destGreenWidth];
  2788. final int destGreenPreShift = 8 - destGreenWidth;
  2789. final int destBlueShift = getChannelShift(destBlueMask);
  2790. final int destBlueWidth = getChannelWidth(destBlueMask, destBlueShift);
  2791. final byte[] destBlues = ANY_TO_EIGHT[destBlueWidth];
  2792. final int destBluePreShift = 8 - destBlueWidth;
  2793. final int destAlphaShift = getChannelShift(destAlphaMask);
  2794. final int destAlphaWidth = getChannelWidth(destAlphaMask, destAlphaShift);
  2795. final byte[] destAlphas = ANY_TO_EIGHT[destAlphaWidth];
  2796. final int destAlphaPreShift = 8 - destAlphaWidth;
  2797. int dp = dpr;
  2798. int sp = spr;
  2799. int ap = apr, alpha = alphaMode;
  2800. int r = 0, g = 0, b = 0, a = 0, index = 0;
  2801. int rq = 0, gq = 0, bq = 0, aq = 0;
  2802. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy,
  2803. sp = spr += (sfy >>> 16) * srcStride,
  2804. ap = apr += (sfy >>> 16) * alphaStride,
  2805. sfy = (sfy & 0xffff) + sfyi,
  2806. dp = dpr += dpryi) {
  2807. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx,
  2808. dp += dprxi,
  2809. sfx = (sfx & 0xffff) + sfxi) {
  2810. /*** READ NEXT PIXEL ***/
  2811. switch (stype) {
  2812. case TYPE_INDEX_8:
  2813. index = srcData[sp] & 0xff;
  2814. sp += (sfx >>> 16);
  2815. break;
  2816. case TYPE_INDEX_4:
  2817. if ((sp & 1) != 0) index = srcData[sp >> 1] & 0x0f;
  2818. else index = (srcData[sp >> 1] >>> 4) & 0x0f;
  2819. sp += (sfx >>> 16);
  2820. break;
  2821. case TYPE_INDEX_2:
  2822. index = (srcData[sp >> 2] >>> (6 - (sp & 3) * 2)) & 0x03;
  2823. sp += (sfx >>> 16);
  2824. break;
  2825. case TYPE_INDEX_1_MSB:
  2826. index = (srcData[sp >> 3] >>> (7 - (sp & 7))) & 0x01;
  2827. sp += (sfx >>> 16);
  2828. break;
  2829. case TYPE_INDEX_1_LSB:
  2830. index = (srcData[sp >> 3] >>> (sp & 7)) & 0x01;
  2831. sp += (sfx >>> 16);
  2832. break;
  2833. }
  2834. /*** DO SPECIAL PROCESSING IF REQUIRED ***/
  2835. r = srcReds[index] & 0xff;
  2836. g = srcGreens[index] & 0xff;
  2837. b = srcBlues[index] & 0xff;
  2838. switch (alphaMode) {
  2839. case ALPHA_CHANNEL_SEPARATE:
  2840. alpha = ((alphaData[ap] & 0xff) << 16) / 255;
  2841. ap += (sfx >> 16);
  2842. break;
  2843. case ALPHA_MASK_UNPACKED:
  2844. alpha = (alphaData[ap] != 0) ? 0x10000 : 0;
  2845. ap += (sfx >> 16);
  2846. break;
  2847. case ALPHA_MASK_PACKED:
  2848. alpha = (alphaData[ap >> 3] << ((ap & 7) + 9)) & 0x10000;
  2849. ap += (sfx >> 16);
  2850. break;
  2851. case ALPHA_MASK_INDEX: { // could speed up using binary search if we sorted the indices
  2852. int i = 0;
  2853. while (i < alphaData.length) {
  2854. if (index == (alphaData[i] & 0xff)) break;
  2855. }
  2856. if (i < alphaData.length) continue;
  2857. } break;
  2858. case ALPHA_MASK_RGB: {
  2859. int i = 0;
  2860. while (i < alphaData.length) {
  2861. if ((r == (alphaData[i] & 0xff)) &&
  2862. (g == (alphaData[i + 1] & 0xff)) &&
  2863. (b == (alphaData[i + 2] & 0xff))) break;
  2864. i += 3;
  2865. }
  2866. if (i < alphaData.length) continue;
  2867. } break;
  2868. }
  2869. if (alpha != 0x10000) {
  2870. if (alpha == 0x0000) continue;
  2871. switch (dtype) {
  2872. case TYPE_GENERIC_8: {
  2873. final int data = destData[dp] & 0xff;
  2874. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2875. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2876. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2877. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2878. } break;
  2879. case TYPE_GENERIC_16_MSB: {
  2880. final int data = ((destData[dp] & 0xff) << 8) | (destData[dp + 1] & 0xff);
  2881. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2882. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2883. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2884. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2885. } break;
  2886. case TYPE_GENERIC_16_LSB: {
  2887. final int data = ((destData[dp + 1] & 0xff) << 8) | (destData[dp] & 0xff);
  2888. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2889. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2890. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2891. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2892. } break;
  2893. case TYPE_GENERIC_24: {
  2894. final int data = (( ((destData[dp] & 0xff) << 8) |
  2895. (destData[dp + 1] & 0xff)) << 8) |
  2896. (destData[dp + 2] & 0xff);
  2897. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2898. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2899. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2900. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2901. } break;
  2902. case TYPE_GENERIC_32_MSB: {
  2903. final int data = (( (( ((destData[dp] & 0xff) << 8) |
  2904. (destData[dp + 1] & 0xff)) << 8) |
  2905. (destData[dp + 2] & 0xff)) << 8) |
  2906. (destData[dp + 3] & 0xff);
  2907. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2908. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2909. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2910. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2911. } break;
  2912. case TYPE_GENERIC_32_LSB: {
  2913. final int data = (( (( ((destData[dp + 3] & 0xff) << 8) |
  2914. (destData[dp + 2] & 0xff)) << 8) |
  2915. (destData[dp + 1] & 0xff)) << 8) |
  2916. (destData[dp] & 0xff);
  2917. rq = destReds[(data & destRedMask) >>> destRedShift] & 0xff;
  2918. gq = destGreens[(data & destGreenMask) >>> destGreenShift] & 0xff;
  2919. bq = destBlues[(data & destBlueMask) >>> destBlueShift] & 0xff;
  2920. aq = destAlphas[(data & destAlphaMask) >>> destAlphaShift] & 0xff;
  2921. } break;
  2922. }
  2923. // Perform alpha blending
  2924. a = aq + ((a - aq) * alpha >> 16);
  2925. r = rq + ((r - rq) * alpha >> 16);
  2926. g = gq + ((g - gq) * alpha >> 16);
  2927. b = bq + ((b - bq) * alpha >> 16);
  2928. }
  2929. /*** WRITE NEXT PIXEL ***/
  2930. final int data =
  2931. (r >>> destRedPreShift << destRedShift) |
  2932. (g >>> destGreenPreShift << destGreenShift) |
  2933. (b >>> destBluePreShift << destBlueShift) |
  2934. (a >>> destAlphaPreShift << destAlphaShift);
  2935. switch (dtype) {
  2936. case TYPE_GENERIC_8: {
  2937. destData[dp] = (byte) data;
  2938. } break;
  2939. case TYPE_GENERIC_16_MSB: {
  2940. destData[dp] = (byte) (data >>> 8);
  2941. destData[dp + 1] = (byte) (data & 0xff);
  2942. } break;
  2943. case TYPE_GENERIC_16_LSB: {
  2944. destData[dp] = (byte) (data & 0xff);
  2945. destData[dp + 1] = (byte) (data >>> 8);
  2946. } break;
  2947. case TYPE_GENERIC_24: {
  2948. destData[dp] = (byte) (data >>> 16);
  2949. destData[dp + 1] = (byte) (data >>> 8);
  2950. destData[dp + 2] = (byte) (data & 0xff);
  2951. } break;
  2952. case TYPE_GENERIC_32_MSB: {
  2953. destData[dp] = (byte) (data >>> 24);
  2954. destData[dp + 1] = (byte) (data >>> 16);
  2955. destData[dp + 2] = (byte) (data >>> 8);
  2956. destData[dp + 3] = (byte) (data & 0xff);
  2957. } break;
  2958. case TYPE_GENERIC_32_LSB: {
  2959. destData[dp] = (byte) (data & 0xff);
  2960. destData[dp + 1] = (byte) (data >>> 8);
  2961. destData[dp + 2] = (byte) (data >>> 16);
  2962. destData[dp + 3] = (byte) (data >>> 24);
  2963. } break;
  2964. }
  2965. }
  2966. }
  2967. }
  2968. /**
  2969. * Blits a direct palette image into an index palette image.
  2970. * <p>
  2971. * Note: The source and destination masks and palettes must
  2972. * always be fully specified.
  2973. * </p>
  2974. *
  2975. * @param op the blitter operation: a combination of BLIT_xxx flags
  2976. * (see BLIT_xxx constants)
  2977. * @param srcData the source byte array containing image data
  2978. * @param srcDepth the source depth: one of 8, 16, 24, 32
  2979. * @param srcStride the source number of bytes per line
  2980. * @param srcOrder the source byte ordering: one of MSB_FIRST or LSB_FIRST;
  2981. * ignored if srcDepth is not 16 or 32
  2982. * @param srcX the top-left x-coord of the source blit region
  2983. * @param srcY the top-left y-coord of the source blit region
  2984. * @param srcWidth the width of the source blit region
  2985. * @param srcHeight the height of the source blit region
  2986. * @param srcRedMask the source red channel mask
  2987. * @param srcGreenMask the source green channel mask
  2988. * @param srcBlueMask the source blue channel mask
  2989. * @param alphaMode the alpha blending or mask mode, may be
  2990. * an integer 0-255 for global alpha; ignored if BLIT_ALPHA
  2991. * not specified in the blitter operations
  2992. * (see ALPHA_MODE_xxx constants)
  2993. * @param alphaData the alpha blending or mask data, varies depending
  2994. * on the value of alphaMode and sometimes ignored
  2995. * @param alphaStride the alpha data number of bytes per line
  2996. * @param alphaX the top-left x-coord of the alpha blit region
  2997. * @param alphaY the top-left y-coord of the alpha blit region
  2998. * @param destData the destination byte array containing image data
  2999. * @param destDepth the destination depth: one of 1, 2, 4, 8
  3000. * @param destStride the destination number of bytes per line
  3001. * @param destOrder the destination byte ordering: one of MSB_FIRST or LSB_FIRST;
  3002. * ignored if destDepth is not 1
  3003. * @param destX the top-left x-coord of the destination blit region
  3004. * @param destY the top-left y-coord of the destination blit region
  3005. * @param destWidth the width of the destination blit region
  3006. * @param destHeight the height of the destination blit region
  3007. * @param destReds the destination palette red component intensities
  3008. * @param destGreens the destination palette green component intensities
  3009. * @param destBlues the destination palette blue component intensities
  3010. * @param flipX if true the resulting image is flipped along the vertical axis
  3011. * @param flipY if true the resulting image is flipped along the horizontal axis
  3012. */
  3013. static void blit(int op,
  3014. byte[] srcData, int srcDepth, int srcStride, int srcOrder,
  3015. int srcX, int srcY, int srcWidth, int srcHeight,
  3016. int srcRedMask, int srcGreenMask, int srcBlueMask,
  3017. int alphaMode, byte[] alphaData, int alphaStride, int alphaX, int alphaY,
  3018. byte[] destData, int destDepth, int destStride, int destOrder,
  3019. int destX, int destY, int destWidth, int destHeight,
  3020. byte[] destReds, byte[] destGreens, byte[] destBlues,
  3021. boolean flipX, boolean flipY) {
  3022. if ((destWidth <= 0) || (destHeight <= 0) || (alphaMode == ALPHA_TRANSPARENT)) return;
  3023. // these should be supplied as params later
  3024. final int srcAlphaMask = 0;
  3025. /*** Prepare scaling data ***/
  3026. final int dwm1 = destWidth - 1;
  3027. final int sfxi = (dwm1 != 0) ? (int)((((long)srcWidth << 16) - 1) / dwm1) : 0;
  3028. final int dhm1 = destHeight - 1;
  3029. final int sfyi = (dhm1 != 0) ? (int)((((long)srcHeight << 16) - 1) / dhm1) : 0;
  3030. /*** Prepare source-related data ***/
  3031. final int sbpp, stype;
  3032. switch (srcDepth) {
  3033. case 8:
  3034. sbpp = 1;
  3035. stype = TYPE_GENERIC_8;
  3036. break;
  3037. case 16:
  3038. sbpp = 2;
  3039. stype = (srcOrder == MSB_FIRST) ? TYPE_GENERIC_16_MSB : TYPE_GENERIC_16_LSB;
  3040. break;
  3041. case 24:
  3042. sbpp = 3;
  3043. stype = TYPE_GENERIC_24;
  3044. break;
  3045. case 32:
  3046. sbpp = 4;
  3047. stype = (srcOrder == MSB_FIRST) ? TYPE_GENERIC_32_MSB : TYPE_GENERIC_32_LSB;
  3048. break;
  3049. default:
  3050. //throw new IllegalArgumentException("Invalid source type");
  3051. return;
  3052. }
  3053. int spr = srcY * srcStride + srcX * sbpp;
  3054. /*** Prepare destination-related data ***/
  3055. final int dtype;
  3056. switch (destDepth) {
  3057. case 8:
  3058. dtype = TYPE_INDEX_8;
  3059. break;
  3060. case 4:
  3061. destStride <<= 1;
  3062. dtype = TYPE_INDEX_4;
  3063. break;
  3064. case 2:
  3065. destStride <<= 2;
  3066. dtype = TYPE_INDEX_2;
  3067. break;
  3068. case 1:
  3069. destStride <<= 3;
  3070. dtype = (destOrder == MSB_FIRST) ? TYPE_INDEX_1_MSB : TYPE_INDEX_1_LSB;
  3071. break;
  3072. default:
  3073. //throw new IllegalArgumentException("Invalid source type");
  3074. return;
  3075. }
  3076. int dpr = ((flipY) ? destY + dhm1 : destY) * destStride + ((flipX) ? destX + dwm1 : destX);
  3077. final int dprxi = (flipX) ? -1 : 1;
  3078. final int dpryi = (flipY) ? -destStride : destStride;
  3079. /*** Prepare special processing data ***/
  3080. int apr;
  3081. if ((op & BLIT_ALPHA) != 0) {
  3082. switch (alphaMode) {
  3083. case ALPHA_MASK_UNPACKED:
  3084. case ALPHA_CHANNEL_SEPARATE:
  3085. if (alphaData == null) alphaMode = 0x10000;
  3086. apr = alphaY * alphaStride + alphaX;
  3087. break;
  3088. case ALPHA_MASK_PACKED:
  3089. if (alphaData == null) alphaMode = 0x10000;
  3090. alphaStride <<= 3;
  3091. apr = alphaY * alphaStride + alphaX;
  3092. break;
  3093. case ALPHA_MASK_INDEX:
  3094. //throw new IllegalArgumentException("Invalid alpha type");
  3095. return;
  3096. case ALPHA_MASK_RGB:
  3097. if (alphaData == null) alphaMode = 0x10000;
  3098. apr = 0;
  3099. break;
  3100. default:
  3101. alphaMode = (alphaMode << 16) / 255; // prescale
  3102. case ALPHA_CHANNEL_SOURCE:
  3103. apr = 0;
  3104. break;
  3105. }
  3106. } else {
  3107. alphaMode = 0x10000;
  3108. apr = 0;
  3109. }
  3110. final boolean ditherEnabled = (op & BLIT_DITHER) != 0;
  3111. /*** Comprehensive blit (apply transformations) ***/
  3112. final int srcRedShift = getChannelShift(srcRedMask);
  3113. final byte[] srcReds = ANY_TO_EIGHT[getChannelWidth(srcRedMask, srcRedShift)];
  3114. final int srcGreenShift = getChannelShift(srcGreenMask);
  3115. final byte[] srcGreens = ANY_TO_EIGHT[getChannelWidth(srcGreenMask, srcGreenShift)];
  3116. final int srcBlueShift = getChannelShift(srcBlueMask);
  3117. final byte[] srcBlues = ANY_TO_EIGHT[getChannelWidth(srcBlueMask, srcBlueShift)];
  3118. final int srcAlphaShift = getChannelShift(srcAlphaMask);
  3119. final byte[] srcAlphas = ANY_TO_EIGHT[getChannelWidth(srcAlphaMask, srcAlphaShift)];
  3120. int dp = dpr;
  3121. int sp = spr;
  3122. int ap = apr, alpha = alphaMode;
  3123. int r = 0, g = 0, b = 0, a = 0;
  3124. int indexq = 0;
  3125. int lastindex = 0, lastr = -1, lastg = -1, lastb = -1;
  3126. final int[] rerr, gerr, berr;
  3127. int destPaletteSize = 1 << destDepth;
  3128. if ((destReds != null) && (destReds.length < destPaletteSize)) destPaletteSize = destReds.length;
  3129. if (ditherEnabled) {
  3130. rerr = new int[destWidth + 2];
  3131. gerr = new int[destWidth + 2];
  3132. berr = new int[destWidth + 2];
  3133. } else {
  3134. rerr = null; gerr = null; berr = null;
  3135. }
  3136. for (int dy = destHeight, sfy = sfyi; dy > 0; --dy,
  3137. sp = spr += (sfy >>> 16) * srcStride,
  3138. ap = apr += (sfy >>> 16) * alphaStride,
  3139. sfy = (sfy & 0xffff) + sfyi,
  3140. dp = dpr += dpryi) {
  3141. int lrerr = 0, lgerr = 0, lberr = 0;
  3142. for (int dx = destWidth, sfx = sfxi; dx > 0; --dx,
  3143. dp += dprxi,
  3144. sfx = (sfx & 0xffff) + sfxi) {
  3145. /*** READ NEXT PIXEL ***/
  3146. switch (stype) {
  3147. case TYPE_GENERIC_8: {
  3148. final int data = srcData[sp] & 0xff;
  3149. sp += (sfx >>> 16);
  3150. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  3151. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  3152. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  3153. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  3154. } break;
  3155. case TYPE_GENERIC_16_MSB: {
  3156. final int data = ((srcData[sp] & 0xff) << 8) | (srcData[sp + 1] & 0xff);
  3157. sp += (sfx >>> 16) * 2;
  3158. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  3159. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  3160. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  3161. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  3162. } break;
  3163. case TYPE_GENERIC_16_LSB: {
  3164. final int data = ((srcData[sp + 1] & 0xff) << 8) | (srcData[sp] & 0xff);
  3165. sp += (sfx >>> 16) * 2;
  3166. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  3167. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  3168. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  3169. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  3170. } break;
  3171. case TYPE_GENERIC_24: {
  3172. final int data = (( ((srcData[sp] & 0xff) << 8) |
  3173. (srcData[sp + 1] & 0xff)) << 8) |
  3174. (srcData[sp + 2] & 0xff);
  3175. sp += (sfx >>> 16) * 3;
  3176. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  3177. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  3178. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  3179. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  3180. } break;
  3181. case TYPE_GENERIC_32_MSB: {
  3182. final int data = (( (( ((srcData[sp] & 0xff) << 8) |
  3183. (srcData[sp + 1] & 0xff)) << 8) |
  3184. (srcData[sp + 2] & 0xff)) << 8) |
  3185. (srcData[sp + 3] & 0xff);
  3186. sp += (sfx >>> 16) * 4;
  3187. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  3188. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  3189. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  3190. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  3191. } break;
  3192. case TYPE_GENERIC_32_LSB: {
  3193. final int data = (( (( ((srcData[sp + 3] & 0xff) << 8) |
  3194. (srcData[sp + 2] & 0xff)) << 8) |
  3195. (srcData[sp + 1] & 0xff)) << 8) |
  3196. (srcData[sp] & 0xff);
  3197. sp += (sfx >>> 16) * 4;
  3198. r = srcReds[(data & srcRedMask) >>> srcRedShift] & 0xff;
  3199. g = srcGreens[(data & srcGreenMask) >>> srcGreenShift] & 0xff;
  3200. b = srcBlues[(data & srcBlueMask) >>> srcBlueShift] & 0xff;
  3201. a = srcAlphas[(data & srcAlphaMask) >>> srcAlphaShift] & 0xff;
  3202. } break;
  3203. }
  3204. /*** DO SPECIAL PROCESSING IF REQUIRED ***/
  3205. switch (alphaMode) {
  3206. case ALPHA_CHANNEL_SEPARATE:
  3207. alpha = ((alphaData[ap] & 0xff) << 16) / 255;
  3208. ap += (sfx >> 16);
  3209. break;
  3210. case ALPHA_CHANNEL_SOURCE:
  3211. alpha = (a << 16) / 255;
  3212. break;
  3213. case ALPHA_MASK_UNPACKED:
  3214. alpha = (alphaData[ap] != 0) ? 0x10000 : 0;
  3215. ap += (sfx >> 16);
  3216. break;
  3217. case ALPHA_MASK_PACKED:
  3218. alpha = (alphaData[ap >> 3] << ((ap & 7) + 9)) & 0x10000;
  3219. ap += (sfx >> 16);
  3220. break;
  3221. case ALPHA_MASK_RGB:
  3222. alpha = 0x10000;
  3223. for (int i = 0; i < alphaData.length; i += 3) {
  3224. if ((r == alphaData[i]) && (g == alphaData[i + 1]) && (b == alphaData[i + 2])) {
  3225. alpha = 0x0000;
  3226. break;
  3227. }
  3228. }
  3229. break;
  3230. }
  3231. if (alpha != 0x10000) {
  3232. if (alpha == 0x0000) continue;
  3233. switch (dtype) {
  3234. case TYPE_INDEX_8:
  3235. indexq = destData[dp] & 0xff;
  3236. break;
  3237. case TYPE_INDEX_4:
  3238. if ((dp & 1) != 0) indexq = destData[dp >> 1] & 0x0f;
  3239. else indexq = (destData[dp >> 1] >>> 4) & 0x0f;
  3240. break;
  3241. case TYPE_INDEX_2:
  3242. indexq = (destData[dp >> 2] >>> (6 - (dp & 3) * 2)) & 0x03;
  3243. break;
  3244. case TYPE_INDEX_1_MSB:
  3245. indexq = (destData[dp >> 3] >>> (7 - (dp & 7))) & 0x01;
  3246. break;
  3247. case TYPE_INDEX_1_LSB:
  3248. indexq = (destData[dp >> 3] >>> (dp & 7)) & 0x01;
  3249. break;
  3250. }
  3251. // Perform alpha blending
  3252. final int rq = destReds[indexq] & 0xff;
  3253. final int gq = destGreens[indexq] & 0xff;
  3254. final int bq = destBlues[indexq] & 0xff;
  3255. r = rq + ((r - rq) * alpha >> 16);
  3256. g = gq + ((g - gq) * alpha >> 16);
  3257. b = bq + ((b - bq) * alpha >> 16);
  3258. }
  3259. /*** MAP COLOR TO THE PALETTE ***/
  3260. if (ditherEnabled) {
  3261. // Floyd-Steinberg error diffusion
  3262. r += rerr[dx] >> 4;
  3263. if (r < 0) r = 0; else if (r > 255) r = 255;
  3264. g += gerr[dx] >> 4;
  3265. if (g < 0) g = 0; else if (g > 255) g = 255;
  3266. b += berr[dx] >> 4;
  3267. if (b < 0) b = 0; else if (b > 255) b = 255;
  3268. rerr[dx] = lrerr;
  3269. gerr[dx] = lgerr;
  3270. berr[dx] = lberr;
  3271. }
  3272. if (r != lastr || g != lastg || b != lastb) {
  3273. // moving the variable declarations out seems to make the JDK JIT happier...
  3274. for (int j = 0, dr, dg, db, distance, minDistance = 0x7fffffff; j < destPaletteSize; ++j) {
  3275. dr = (destReds[j] & 0xff) - r;
  3276. dg = (destGreens[j] & 0xff) - g;
  3277. db = (destBlues[j] & 0xff) - b;
  3278. distance = dr * dr + dg * dg + db * db;
  3279. if (distance < minDistance) {
  3280. lastindex = j;
  3281. if (distance == 0) break;
  3282. minDistance = distance;
  3283. }
  3284. }
  3285. lastr = r; lastg = g; lastb = b;
  3286. }
  3287. if (ditherEnabled) {
  3288. // Floyd-Steinberg error diffusion, cont'd...
  3289. final int dxm1 = dx - 1, dxp1 = dx + 1;
  3290. int acc;
  3291. rerr[dxp1] += acc = (lrerr = r - (destReds[lastindex] & 0xff)) + lrerr + lrerr;
  3292. rerr[dx] += acc += lrerr + lrerr;
  3293. rerr[dxm1] += acc + lrerr + lrerr;
  3294. gerr[dxp1] += acc = (lgerr = g - (destGreens[lastindex] & 0xff)) + lgerr + lgerr;
  3295. gerr[dx] += acc += lgerr + lgerr;
  3296. gerr[dxm1] += acc + lgerr + lgerr;
  3297. berr[dxp1] += acc = (lberr = b - (destBlues[lastindex] & 0xff)) + lberr + lberr;
  3298. berr[dx] += acc += lberr + lberr;
  3299. berr[dxm1] += acc + lberr + lberr;
  3300. }
  3301. /*** WRITE NEXT PIXEL ***/
  3302. switch (dtype) {
  3303. case TYPE_INDEX_8:
  3304. destData[dp] = (byte) lastindex;
  3305. break;
  3306. case TYPE_INDEX_4:
  3307. if ((dp & 1) != 0) destData[dp >> 1] = (byte)((destData[dp >> 1] & 0xf0) | lastindex);
  3308. else destData[dp >> 1] = (byte)((destData[dp >> 1] & 0x0f) | (lastindex << 4));
  3309. break;
  3310. case TYPE_INDEX_2: {
  3311. final int shift = 6 - (dp & 3) * 2;
  3312. destData[dp >> 2] = (byte)(destData[dp >> 2] & ~(0x03 << shift) | (lastindex << shift));
  3313. } break;
  3314. case TYPE_INDEX_1_MSB: {
  3315. final int shift = 7 - (dp & 7);
  3316. destData[dp >> 3] = (byte)(destData[dp >> 3] & ~(0x01 << shift) | (lastindex << shift));
  3317. } break;
  3318. case TYPE_INDEX_1_LSB: {
  3319. final int shift = dp & 7;
  3320. destData[dp >> 3] = (byte)(destData[dp >> 3] & ~(0x01 << shift) | (lastindex << shift));
  3321. } break;
  3322. }
  3323. }
  3324. }
  3325. }
  3326. /**
  3327. * Computes the required channel shift from a mask.
  3328. */
  3329. static int getChannelShift(int mask) {
  3330. if (mask == 0) return 0;
  3331. int i;
  3332. for (i = 0; ((mask & 1) == 0) && (i < 32); ++i) {
  3333. mask >>>= 1;
  3334. }
  3335. return i;
  3336. }
  3337. /**
  3338. * Computes the required channel width (depth) from a mask.
  3339. */
  3340. static int getChannelWidth(int mask, int shift) {
  3341. if (mask == 0) return 0;
  3342. int i;
  3343. mask >>>= shift;
  3344. for (i = shift; ((mask & 1) != 0) && (i < 32); ++i) {
  3345. mask >>>= 1;
  3346. }
  3347. return i - shift;
  3348. }
  3349. /**
  3350. * Extracts a field from packed RGB data given a mask for that field.
  3351. */
  3352. static byte getChannelField(int data, int mask) {
  3353. final int shift = getChannelShift(mask);
  3354. return ANY_TO_EIGHT[getChannelWidth(mask, shift)][(data & mask) >>> shift];
  3355. }
  3356. /**
  3357. * Creates an ImageData containing one band's worth of a gradient filled
  3358. * block. If <code>vertical</code> is true, the band must be tiled
  3359. * horizontally to fill a region, otherwise it must be tiled vertically.
  3360. *
  3361. * @param width the width of the region to be filled
  3362. * @param height the height of the region to be filled
  3363. * @param vertical if true sweeps from top to bottom, else
  3364. * sweeps from left to right
  3365. * @param fromRGB the color to start with
  3366. * @param toRGB the color to end with
  3367. * @param redBits the number of significant red bits, 0 for palette modes
  3368. * @param greenBits the number of significant green bits, 0 for palette modes
  3369. * @param blueBits the number of significant blue bits, 0 for palette modes
  3370. * @return the new ImageData
  3371. */
  3372. static ImageData createGradientBand(
  3373. int width, int height, boolean vertical,
  3374. RGB fromRGB, RGB toRGB,
  3375. int redBits, int greenBits, int blueBits) {
  3376. /* Gradients are drawn as tiled bands */
  3377. final int bandWidth, bandHeight, bitmapDepth;
  3378. final byte[] bitmapData;
  3379. final PaletteData paletteData;
  3380. /* Select an algorithm depending on the depth of the screen */
  3381. if (redBits != 0 && greenBits != 0 && blueBits != 0) {
  3382. paletteData = new PaletteData(0x0000ff00, 0x00ff0000, 0xff000000);
  3383. bitmapDepth = 32;
  3384. if (redBits >= 8 && greenBits >= 8 && blueBits >= 8) {
  3385. /* Precise color */
  3386. final int steps;
  3387. if (vertical) {
  3388. bandWidth = 1;
  3389. bandHeight = height;
  3390. steps = bandHeight > 1 ? bandHeight - 1 : 1;
  3391. } else {
  3392. bandWidth = width;
  3393. bandHeight = 1;
  3394. steps = bandWidth > 1 ? bandWidth - 1 : 1;
  3395. }
  3396. final int bytesPerLine = bandWidth * 4;
  3397. bitmapData = new byte[bandHeight * bytesPerLine];
  3398. buildPreciseGradientChannel(fromRGB.blue, toRGB.blue, steps, bandWidth, bandHeight, vertical, bitmapData, 0, bytesPerLine);
  3399. buildPreciseGradientChannel(fromRGB.green, toRGB.green, steps, bandWidth, bandHeight, vertical, bitmapData, 1, bytesPerLine);
  3400. buildPreciseGradientChannel(fromRGB.red, toRGB.red, steps, bandWidth, bandHeight, vertical, bitmapData, 2, bytesPerLine);
  3401. } else {
  3402. /* Dithered color */
  3403. final int steps;
  3404. if (vertical) {
  3405. bandWidth = (width < 8) ? width : 8;
  3406. bandHeight = height;
  3407. steps = bandHeight > 1 ? bandHeight - 1 : 1;
  3408. } else {
  3409. bandWidth = width;
  3410. bandHeight = (height < 8) ? height : 8;
  3411. steps = bandWidth > 1 ? bandWidth - 1 : 1;
  3412. }
  3413. final int bytesPerLine = bandWidth * 4;
  3414. bitmapData = new byte[bandHeight * bytesPerLine];
  3415. buildDitheredGradientChannel(fromRGB.blue, toRGB.blue, steps, bandWidth, bandHeight, vertical, bitmapData, 0, bytesPerLine, blueBits);
  3416. buildDitheredGradientChannel(fromRGB.green, toRGB.green, steps, bandWidth, bandHeight, vertical, bitmapData, 1, bytesPerLine, greenBits);
  3417. buildDitheredGradientChannel(fromRGB.red, toRGB.red, steps, bandWidth, bandHeight, vertical, bitmapData, 2, bytesPerLine, redBits);
  3418. }
  3419. } else {
  3420. /* Dithered two tone */
  3421. paletteData = new PaletteData(new RGB[] { fromRGB, toRGB });
  3422. bitmapDepth = 8;
  3423. final int blendi;
  3424. if (vertical) {
  3425. bandWidth = (width < 8) ? width : 8;
  3426. bandHeight = height;
  3427. blendi = (bandHeight > 1) ? 0x1040000 / (bandHeight - 1) + 1 : 1;
  3428. } else {
  3429. bandWidth = width;
  3430. bandHeight = (height < 8) ? height : 8;
  3431. blendi = (bandWidth > 1) ? 0x1040000 / (bandWidth - 1) + 1 : 1;
  3432. }
  3433. final int bytesPerLine = (bandWidth + 3) & -4;
  3434. bitmapData = new byte[bandHeight * bytesPerLine];
  3435. if (vertical) {
  3436. for (int dy = 0, blend = 0, dp = 0; dy < bandHeight;
  3437. ++dy, blend += blendi, dp += bytesPerLine) {
  3438. for (int dx = 0; dx < bandWidth; ++dx) {
  3439. bitmapData[dp + dx] = (blend + DITHER_MATRIX[dy & 7][dx]) <
  3440. 0x1000000 ? (byte)0 : (byte)1;
  3441. }
  3442. }
  3443. } else {
  3444. for (int dx = 0, blend = 0; dx < bandWidth; ++dx, blend += blendi) {
  3445. for (int dy = 0, dptr = dx; dy < bandHeight; ++dy, dptr += bytesPerLine) {
  3446. bitmapData[dptr] = (blend + DITHER_MATRIX[dy][dx & 7]) <
  3447. 0x1000000 ? (byte)0 : (byte)1;
  3448. }
  3449. }
  3450. }
  3451. }
  3452. return new ImageData(bandWidth, bandHeight, bitmapDepth, paletteData, 4, bitmapData);
  3453. }
  3454. /*
  3455. * Fill in gradated values for a color channel
  3456. */
  3457. static final void buildPreciseGradientChannel(int from, int to, int steps,
  3458. int bandWidth, int bandHeight, boolean vertical,
  3459. byte[] bitmapData, int dp, int bytesPerLine) {
  3460. int val = from << 16;
  3461. final int inc = ((to << 16) - val) / steps + 1;
  3462. if (vertical) {
  3463. for (int dy = 0; dy < bandHeight; ++dy, dp += bytesPerLine) {
  3464. bitmapData[dp] = (byte)(val >>> 16);
  3465. val += inc;
  3466. }
  3467. } else {
  3468. for (int dx = 0; dx < bandWidth; ++dx, dp += 4) {
  3469. bitmapData[dp] = (byte)(val >>> 16);
  3470. val += inc;
  3471. }
  3472. }
  3473. }
  3474. /*
  3475. * Fill in dithered gradated values for a color channel
  3476. */
  3477. static final void buildDitheredGradientChannel(int from, int to, int steps,
  3478. int bandWidth, int bandHeight, boolean vertical,
  3479. byte[] bitmapData, int dp, int bytesPerLine, int bits) {
  3480. final int mask = 0xff00 >>> bits;
  3481. int val = from << 16;
  3482. final int inc = ((to << 16) - val) / steps + 1;
  3483. if (vertical) {
  3484. for (int dy = 0; dy < bandHeight; ++dy, dp += bytesPerLine) {
  3485. for (int dx = 0, dptr = dp; dx < bandWidth; ++dx, dptr += 4) {
  3486. final int thresh = DITHER_MATRIX[dy & 7][dx] >>> bits;
  3487. int temp = val + thresh;
  3488. if (temp > 0xffffff) bitmapData[dptr] = -1;
  3489. else bitmapData[dptr] = (byte)((temp >>> 16) & mask);
  3490. }
  3491. val += inc;
  3492. }
  3493. } else {
  3494. for (int dx = 0; dx < bandWidth; ++dx, dp += 4) {
  3495. for (int dy = 0, dptr = dp; dy < bandHeight; ++dy, dptr += bytesPerLine) {
  3496. final int thresh = DITHER_MATRIX[dy][dx & 7] >>> bits;
  3497. int temp = val + thresh;
  3498. if (temp > 0xffffff) bitmapData[dptr] = -1;
  3499. else bitmapData[dptr] = (byte)((temp >>> 16) & mask);
  3500. }
  3501. val += inc;
  3502. }
  3503. }
  3504. }
  3505. /**
  3506. * Renders a gradient onto a GC.
  3507. * <p>
  3508. * This is a GC helper.
  3509. * </p>
  3510. *
  3511. * @param gc the GC to render the gradient onto
  3512. * @param device the device the GC belongs to
  3513. * @param x the top-left x coordinate of the region to be filled
  3514. * @param y the top-left y coordinate of the region to be filled
  3515. * @param width the width of the region to be filled
  3516. * @param height the height of the region to be filled
  3517. * @param vertical if true sweeps from top to bottom, else
  3518. * sweeps from left to right
  3519. * @param fromRGB the color to start with
  3520. * @param toRGB the color to end with
  3521. * @param redBits the number of significant red bits, 0 for palette modes
  3522. * @param greenBits the number of significant green bits, 0 for palette modes
  3523. * @param blueBits the number of significant blue bits, 0 for palette modes
  3524. */
  3525. static void fillGradientRectangle(GC gc, Device device,
  3526. int x, int y, int width, int height, boolean vertical,
  3527. RGB fromRGB, RGB toRGB,
  3528. int redBits, int greenBits, int blueBits) {
  3529. /* Create the bitmap and tile it */
  3530. ImageData band = createGradientBand(width, height, vertical,
  3531. fromRGB, toRGB, redBits, greenBits, blueBits);
  3532. Image image = new Image(device, band);
  3533. if ((band.width == 1) || (band.height == 1)) {
  3534. gc.drawImage(image, 0, 0, band.width, band.height, x, y, width, height);
  3535. } else {
  3536. if (vertical) {
  3537. for (int dx = 0; dx < width; dx += band.width) {
  3538. int blitWidth = width - dx;
  3539. if (blitWidth > band.width) blitWidth = band.width;
  3540. gc.drawImage(image, 0, 0, blitWidth, band.height, dx + x, y, blitWidth, band.height);
  3541. }
  3542. } else {
  3543. for (int dy = 0; dy < height; dy += band.height) {
  3544. int blitHeight = height - dy;
  3545. if (blitHeight > band.height) blitHeight = band.height;
  3546. gc.drawImage(image, 0, 0, band.width, blitHeight, x, dy + y, band.width, blitHeight);
  3547. }
  3548. }
  3549. }
  3550. image.dispose();
  3551. }
  3552. }