PageRenderTime 89ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 1ms

/src/org/eclipse/swt/graphics/ImageData.d

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