PageRenderTime 60ms CodeModel.GetById 20ms 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

Large files files are truncated, but you can click here to view the full 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…

Large files files are truncated, but you can click here to view the full file