PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

/dwt/graphics/ImageData.d

https://github.com/drewet/dwt-mac
D | 3730 lines | 2714 code | 128 blank | 888 comment | 562 complexity | f5aaf21d4995d3f83418f9f1d98c727f MD5 | raw file

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

  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2009 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *
  11. * Port to the D programming language:
  12. * Frank Benoit <benoit@tionex.de>
  13. * Jacob Carlborg <jacob.carlborg@gmail.com>
  14. *******************************************************************************/
  15. module dwt.graphics.ImageData;
  16. import dwt.dwthelper.utils;
  17. import dwt.dwthelper.System;
  18. import dwt.DWT;
  19. import dwt.DWTException;
  20. import dwt.graphics.Device;
  21. import dwt.graphics.GC;
  22. import dwt.graphics.Image;
  23. import dwt.graphics.ImageDataLoader;
  24. import dwt.graphics.PaletteData;
  25. import dwt.graphics.RGB;
  26. import dwt.internal.CloneableCompatibility;
  27. import dwt.dwthelper.InputStream;
  28. /**
  29. * Instances of this class are device-independent descriptions
  30. * of images. They are typically used as an intermediate format
  31. * between loading from or writing to streams and creating an
  32. * <code>Image</code>.
  33. * <p>
  34. * Note that the public fields <code>x</code>, <code>y</code>,
  35. * <code>disposalMethod</code> and <code>delayTime</code> are
  36. * typically only used when the image is in a set of images used
  37. * for animation.
  38. * </p>
  39. *
  40. * @see Image
  41. * @see ImageLoader
  42. * @see <a href="http://www.eclipse.org/swt/snippets/#image">ImageData snippets</a>
  43. * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ImageAnalyzer</a>
  44. * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
  45. */
  46. public final class ImageData : CloneableCompatibility {
  47. /**
  48. * The width of the image, in pixels.
  49. */
  50. public int width;
  51. /**
  52. * The height of the image, in pixels.
  53. */
  54. public int height;
  55. /**
  56. * The color depth of the image, in bits per pixel.
  57. * <p>
  58. * Note that a depth of 8 or less does not necessarily
  59. * mean that the image is palette indexed, or
  60. * conversely that a depth greater than 8 means that
  61. * the image is direct color. Check the associated
  62. * PaletteData's isDirect field for such determinations.
  63. */
  64. public int depth;
  65. /**
  66. * The scanline padding.
  67. * <p>
  68. * If one scanline of the image is not a multiple of
  69. * this number, it will be padded with zeros until it is.
  70. * </p>
  71. */
  72. public int scanlinePad;
  73. /**
  74. * The number of bytes per scanline.
  75. * <p>
  76. * This is a multiple of the scanline padding.
  77. * </p>
  78. */
  79. public int bytesPerLine;
  80. /**
  81. * The pixel data of the image.
  82. * <p>
  83. * Note that for 16 bit depth images the pixel data is stored
  84. * in least significant byte order; however, for 24bit and
  85. * 32bit depth images the pixel data is stored in most
  86. * significant byte order.
  87. * </p>
  88. */
  89. public byte[] data;
  90. /**
  91. * The color table for the image.
  92. */
  93. public PaletteData palette;
  94. /**
  95. * The transparent pixel.
  96. * <p>
  97. * Pixels with this value are transparent.
  98. * </p><p>
  99. * The default is -1 which means 'no transparent pixel'.
  100. * </p>
  101. */
  102. public int transparentPixel;
  103. /**
  104. * An icon-specific field containing the data from the icon mask.
  105. * <p>
  106. * This is a 1 bit bitmap stored with the most significant
  107. * bit first. The number of bytes per scanline is
  108. * '((width + 7) / 8 + (maskPad - 1)) / maskPad * maskPad'.
  109. * </p><p>
  110. * The default is null which means 'no transparency mask'.
  111. * </p>
  112. */
  113. public byte[] maskData;
  114. /**
  115. * An icon-specific field containing the scanline pad of the mask.
  116. * <p>
  117. * If one scanline of the transparency mask is not a
  118. * multiple of this number, it will be padded with zeros until
  119. * it is.
  120. * </p>
  121. */
  122. public int maskPad;
  123. /**
  124. * The alpha data of the image.
  125. * <p>
  126. * Every pixel can have an <em>alpha blending</em> value that
  127. * varies from 0, meaning fully transparent, to 255 meaning
  128. * fully opaque. The number of bytes per scanline is
  129. * 'width'.
  130. * </p>
  131. */
  132. public byte[] alphaData;
  133. /**
  134. * The global alpha value to be used for every pixel.
  135. * <p>
  136. * If this value is set, the <code>alphaData</code> field
  137. * is ignored and when the image is rendered each pixel
  138. * will be blended with the background an amount
  139. * proportional to this value.
  140. * </p><p>
  141. * The default is -1 which means 'no global alpha value'
  142. * </p>
  143. */
  144. public int alpha;
  145. /**
  146. * The type of file from which the image was read.
  147. *
  148. * It is expressed as one of the following values:
  149. * <dl>
  150. * <dt><code>IMAGE_BMP</code></dt>
  151. * <dd>Windows BMP file format, no compression</dd>
  152. * <dt><code>IMAGE_BMP_RLE</code></dt>
  153. * <dd>Windows BMP file format, RLE compression if appropriate</dd>
  154. * <dt><code>IMAGE_GIF</code></dt>
  155. * <dd>GIF file format</dd>
  156. * <dt><code>IMAGE_ICO</code></dt>
  157. * <dd>Windows ICO file format</dd>
  158. * <dt><code>IMAGE_JPEG</code></dt>
  159. * <dd>JPEG file format</dd>
  160. * <dt><code>IMAGE_PNG</code></dt>
  161. * <dd>PNG file format</dd>
  162. * </dl>
  163. */
  164. public int type;
  165. /**
  166. * The x coordinate of the top left corner of the image
  167. * within the logical screen (this field corresponds to
  168. * the GIF89a Image Left Position value).
  169. */
  170. public int x;
  171. /**
  172. * The y coordinate of the top left corner of the image
  173. * within the logical screen (this field corresponds to
  174. * the GIF89a Image Top Position value).
  175. */
  176. public int y;
  177. /**
  178. * A description of how to dispose of the current image
  179. * before displaying the next.
  180. *
  181. * It is expressed as one of the following values:
  182. * <dl>
  183. * <dt><code>DM_UNSPECIFIED</code></dt>
  184. * <dd>disposal method not specified</dd>
  185. * <dt><code>DM_FILL_NONE</code></dt>
  186. * <dd>do nothing - leave the image in place</dd>
  187. * <dt><code>DM_FILL_BACKGROUND</code></dt>
  188. * <dd>fill with the background color</dd>
  189. * <dt><code>DM_FILL_PREVIOUS</code></dt>
  190. * <dd>restore the previous picture</dd>
  191. * </dl>
  192. * (this field corresponds to the GIF89a Disposal Method value)
  193. */
  194. public int disposalMethod;
  195. /**
  196. * The time to delay before displaying the next image
  197. * in an animation (this field corresponds to the GIF89a
  198. * Delay Time value).
  199. */
  200. public int delayTime;
  201. /**
  202. * Arbitrary channel width data to 8-bit conversion table.
  203. */
  204. private static byte[][] _ANY_TO_EIGHT;
  205. static byte[][] ANY_TO_EIGHT ()
  206. {
  207. return _ANY_TO_EIGHT = _ANY_TO_EIGHT.length > 0 ? _ANY_TO_EIGHT : new byte[][](9);
  208. }
  209. static byte[] ONE_TO_ONE_MAPPING ()
  210. {
  211. if (_ONE_TO_ONE_MAPPING.length > 0)
  212. return _ONE_TO_ONE_MAPPING;
  213. for (int b = 0; b < 9; ++b) {
  214. byte[] data = ANY_TO_EIGHT[b] = new byte[1 << b];
  215. if (b == 0) continue;
  216. int inc = 0;
  217. for (int bit = 0x10000; (bit >>= b) != 0;) inc |= bit;
  218. for (int v = 0, p = 0; v < 0x10000; v+= inc) data[p++] = cast(byte)(v >> 8);
  219. }
  220. _ONE_TO_ONE_MAPPING = ANY_TO_EIGHT[8];
  221. }
  222. private static byte[] _ONE_TO_ONE_MAPPING;
  223. /**
  224. * Scaled 8x8 Bayer dither matrix.
  225. */
  226. static const int[][] DITHER_MATRIX = [
  227. [ 0xfc0000, 0x7c0000, 0xdc0000, 0x5c0000, 0xf40000, 0x740000, 0xd40000, 0x540000 ],
  228. [ 0x3c0000, 0xbc0000, 0x1c0000, 0x9c0000, 0x340000, 0xb40000, 0x140000, 0x940000 ],
  229. [ 0xcc0000, 0x4c0000, 0xec0000, 0x6c0000, 0xc40000, 0x440000, 0xe40000, 0x640000 ],
  230. [ 0x0c0000, 0x8c0000, 0x2c0000, 0xac0000, 0x040000, 0x840000, 0x240000, 0xa40000 ],
  231. [ 0xf00000, 0x700000, 0xd00000, 0x500000, 0xf80000, 0x780000, 0xd80000, 0x580000 ],
  232. [ 0x300000, 0xb00000, 0x100000, 0x900000, 0x380000, 0xb80000, 0x180000, 0x980000 ],
  233. [ 0xc00000, 0x400000, 0xe00000, 0x600000, 0xc80000, 0x480000, 0xe80000, 0x680000 ],
  234. [ 0x000000, 0x800000, 0x200000, 0xa00000, 0x080000, 0x880000, 0x280000, 0xa80000 ]
  235. ];
  236. /**
  237. * Constructs a new, empty ImageData with the given width, height,
  238. * depth and palette. The data will be initialized to an (all zero)
  239. * array of the appropriate size.
  240. *
  241. * @param width the width of the image
  242. * @param height the height of the image
  243. * @param depth the depth of the image
  244. * @param palette the palette of the image (must not be null)
  245. *
  246. * @exception IllegalArgumentException <ul>
  247. * <li>ERROR_INVALID_ARGUMENT - if the width or height is zero or negative, or if the depth is not
  248. * one of 1, 2, 4, 8, 16, 24 or 32</li>
  249. * <li>ERROR_NULL_ARGUMENT - if the palette is null</li>
  250. * </ul>
  251. */
  252. public this(int width, int height, int depth, PaletteData palette) {
  253. this(width, height, depth, palette,
  254. 4, null, 0, null,
  255. null, -1, -1, DWT.IMAGE_UNDEFINED,
  256. 0, 0, 0, 0);
  257. }
  258. /**
  259. * Constructs a new, empty ImageData with the given width, height,
  260. * depth, palette, scanlinePad and data.
  261. *
  262. * @param width the width of the image
  263. * @param height the height of the image
  264. * @param depth the depth of the image
  265. * @param palette the palette of the image
  266. * @param scanlinePad the padding of each line, in bytes
  267. * @param data the data of the image
  268. *
  269. * @exception IllegalArgumentException <ul>
  270. * <li>ERROR_INVALID_ARGUMENT - if the width or height is zero or negative, or if the depth is not
  271. * one of 1, 2, 4, 8, 16, 24 or 32, or the data array is too small to contain the image data</li>
  272. * <li>ERROR_NULL_ARGUMENT - if the palette or data is null</li>
  273. * <li>ERROR_CANNOT_BE_ZERO - if the scanlinePad is zero</li>
  274. * </ul>
  275. */
  276. public this(int width, int height, int depth, PaletteData palette, int scanlinePad, byte[] data) {
  277. this(width, height, depth, palette,
  278. scanlinePad, checkData(data), 0, null,
  279. null, -1, -1, DWT.IMAGE_UNDEFINED,
  280. 0, 0, 0, 0);
  281. }
  282. /**
  283. * Constructs an <code>ImageData</code> loaded from the specified
  284. * input stream. Throws an error if an error occurs while loading
  285. * the image, or if the image has an unsupported type. Application
  286. * code is still responsible for closing the input stream.
  287. * <p>
  288. * This constructor is provided for convenience when loading a single
  289. * image only. If the stream contains multiple images, only the first
  290. * one will be loaded. To load multiple images, use
  291. * <code>ImageLoader.load()</code>.
  292. * </p><p>
  293. * This constructor may be used to load a resource as follows:
  294. * </p>
  295. * <pre>
  296. * static ImageData loadImageData (Class clazz, String string) {
  297. * InputStream stream = clazz.getResourceAsStream (string);
  298. * if (stream is null) return null;
  299. * ImageData imageData = null;
  300. * try {
  301. * imageData = new ImageData (stream);
  302. * } catch (DWTException ex) {
  303. * } finally {
  304. * try {
  305. * stream.close ();
  306. * } catch (IOException ex) {}
  307. * }
  308. * return imageData;
  309. * }
  310. * </pre>
  311. *
  312. * @param stream the input stream to load the image from (must not be null)
  313. *
  314. * @exception IllegalArgumentException <ul>
  315. * <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
  316. * </ul>
  317. * @exception DWTException <ul>
  318. * <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
  319. * <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
  320. * <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
  321. * </ul>
  322. *
  323. * @see ImageLoader#load(InputStream)
  324. */
  325. public this(InputStream stream) {
  326. ImageData[] data = ImageDataLoader.load(stream);
  327. if (data.length < 1) DWT.error(DWT.ERROR_INVALID_IMAGE);
  328. ImageData i = data[0];
  329. setAllFields(
  330. i.width,
  331. i.height,
  332. i.depth,
  333. i.scanlinePad,
  334. i.bytesPerLine,
  335. i.data,
  336. i.palette,
  337. i.transparentPixel,
  338. i.maskData,
  339. i.maskPad,
  340. i.alphaData,
  341. i.alpha,
  342. i.type,
  343. i.x,
  344. i.y,
  345. i.disposalMethod,
  346. i.delayTime);
  347. }
  348. /**
  349. * Constructs an <code>ImageData</code> loaded from a file with the
  350. * specified name. Throws an error if an error occurs loading the
  351. * image, or if the image has an unsupported type.
  352. * <p>
  353. * This constructor is provided for convenience when loading a single
  354. * image only. If the file contains multiple images, only the first
  355. * one will be loaded. To load multiple images, use
  356. * <code>ImageLoader.load()</code>.
  357. * </p>
  358. *
  359. * @param filename the name of the file to load the image from (must not be null)
  360. *
  361. * @exception IllegalArgumentException <ul>
  362. * <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
  363. * </ul>
  364. * @exception DWTException <ul>
  365. * <li>ERROR_IO - if an IO error occurs while reading from the file</li>
  366. * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
  367. * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
  368. * </ul>
  369. */
  370. public this(String filename) {
  371. ImageData[] data = ImageDataLoader.load(filename);
  372. if (data.length < 1) DWT.error(DWT.ERROR_INVALID_IMAGE);
  373. ImageData i = data[0];
  374. setAllFields(
  375. i.width,
  376. i.height,
  377. i.depth,
  378. i.scanlinePad,
  379. i.bytesPerLine,
  380. i.data,
  381. i.palette,
  382. i.transparentPixel,
  383. i.maskData,
  384. i.maskPad,
  385. i.alphaData,
  386. i.alpha,
  387. i.type,
  388. i.x,
  389. i.y,
  390. i.disposalMethod,
  391. i.delayTime);
  392. }
  393. /**
  394. * Prevents uninitialized instances from being created outside the package.
  395. */
  396. private this() {
  397. }
  398. /**
  399. * Constructs an image data by giving values for all non-computable fields.
  400. * <p>
  401. * This method is for internal use, and is not described further.
  402. * </p>
  403. */
  404. this(
  405. int width, int height, int depth, PaletteData palette,
  406. int scanlinePad, byte[] data, int maskPad, byte[] maskData,
  407. byte[] alphaData, int alpha, int transparentPixel, int type,
  408. int x, int y, int disposalMethod, int delayTime)
  409. {
  410. if (palette is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
  411. if (!(depth is 1 || depth is 2 || depth is 4 || depth is 8
  412. || depth is 16 || depth is 24 || depth is 32)) {
  413. DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  414. }
  415. if (width <= 0 || height <= 0) {
  416. DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  417. }
  418. if (scanlinePad is 0) DWT.error (DWT.ERROR_CANNOT_BE_ZERO);
  419. int bytesPerLine = (((width * depth + 7) / 8) + (scanlinePad - 1))
  420. / scanlinePad * scanlinePad;
  421. /*
  422. * When the image is being loaded from a PNG, we need to use the theoretical minimum
  423. * number of bytes per line to check whether there is enough data, because the actual
  424. * number of bytes per line is calculated based on the given depth, which may be larger
  425. * than the actual depth of the PNG.
  426. */
  427. int minBytesPerLine = type is DWT.IMAGE_PNG ? ((((width + 7) / 8) + 3) / 4) * 4 : bytesPerLine;
  428. if (data !is null && data.length < minBytesPerLine * height) {
  429. DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  430. }
  431. setAllFields(
  432. width,
  433. height,
  434. depth,
  435. scanlinePad,
  436. bytesPerLine,
  437. data !is null ? data : new byte[bytesPerLine * height],
  438. palette,
  439. transparentPixel,
  440. maskData,
  441. maskPad,
  442. alphaData,
  443. alpha,
  444. type,
  445. x,
  446. y,
  447. disposalMethod,
  448. delayTime);
  449. }
  450. /**
  451. * Initializes all fields in the receiver. This method must be called
  452. * by all public constructors to ensure that all fields are initialized
  453. * for a new ImageData object. If a new field is added to the class,
  454. * then it must be added to this method.
  455. * <p>
  456. * This method is for internal use, and is not described further.
  457. * </p>
  458. */
  459. void setAllFields(int width, int height, int depth, int scanlinePad,
  460. int bytesPerLine, byte[] data, PaletteData palette, int transparentPixel,
  461. byte[] maskData, int maskPad, byte[] alphaData, int alpha,
  462. int type, int x, int y, int disposalMethod, int delayTime) {
  463. this.width = width;
  464. this.height = height;
  465. this.depth = depth;
  466. this.scanlinePad = scanlinePad;
  467. this.bytesPerLine = bytesPerLine;
  468. this.data = data;
  469. this.palette = palette;
  470. this.transparentPixel = transparentPixel;
  471. this.maskData = maskData;
  472. this.maskPad = maskPad;
  473. this.alphaData = alphaData;
  474. this.alpha = alpha;
  475. this.type = type;
  476. this.x = x;
  477. this.y = y;
  478. this.disposalMethod = disposalMethod;
  479. this.delayTime = delayTime;
  480. }
  481. /**
  482. * Invokes internal DWT functionality to create a new instance of
  483. * this class.
  484. * <p>
  485. * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
  486. * API for <code>ImageData</code>. It is marked public only so that it
  487. * can be shared within the packages provided by DWT. It is subject
  488. * to change without notice, and should never be called from
  489. * application code.
  490. * </p>
  491. * <p>
  492. * This method is for internal use, and is not described further.
  493. * </p>
  494. */
  495. public static ImageData internal_new(
  496. int width, int height, int depth, PaletteData palette,
  497. int scanlinePad, byte[] data, int maskPad, byte[] maskData,
  498. byte[] alphaData, int alpha, int transparentPixel, int type,
  499. int x, int y, int disposalMethod, int delayTime)
  500. {
  501. return new ImageData(
  502. width, height, depth, palette, scanlinePad, data, maskPad, maskData,
  503. alphaData, alpha, transparentPixel, type, x, y, disposalMethod, delayTime);
  504. }
  505. ImageData colorMaskImage(int pixel) {
  506. ImageData mask = new ImageData(width, height, 1, bwPalette(),
  507. 2, null, 0, null, null, -1, -1, DWT.IMAGE_UNDEFINED,
  508. 0, 0, 0, 0);
  509. int[] row = new int[width];
  510. for (int y = 0; y < height; y++) {
  511. getPixels(0, y, width, row, 0);
  512. for (int i = 0; i < width; i++) {
  513. if (pixel !is -1 && row[i] is pixel) {
  514. row[i] = 0;
  515. } else {
  516. row[i] = 1;
  517. }
  518. }
  519. mask.setPixels(0, y, width, row, 0);
  520. }
  521. return mask;
  522. }
  523. static byte[] checkData(byte [] data) {
  524. if (data is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
  525. return data;
  526. }
  527. /**
  528. * Returns a new instance of the same class as the receiver,
  529. * whose slots have been filled in with <em>copies</em> of
  530. * the values in the slots of the receiver. That is, the
  531. * returned object is a <em>deep copy</em> of the receiver.
  532. *
  533. * @return a copy of the receiver.
  534. */
  535. public Object clone() {
  536. byte[] cloneData = new byte[data.length];
  537. System.arraycopy(data, 0, cloneData, 0, data.length);
  538. byte[] cloneMaskData = null;
  539. if (maskData !is null) {
  540. cloneMaskData = new byte[maskData.length];
  541. System.arraycopy(maskData, 0, cloneMaskData, 0, maskData.length);
  542. }
  543. byte[] cloneAlphaData = null;
  544. if (alphaData !is null) {
  545. cloneAlphaData = new byte[alphaData.length];
  546. System.arraycopy(alphaData, 0, cloneAlphaData, 0, alphaData.length);
  547. }
  548. return new ImageData(
  549. width,
  550. height,
  551. depth,
  552. palette,
  553. scanlinePad,
  554. cloneData,
  555. maskPad,
  556. cloneMaskData,
  557. cloneAlphaData,
  558. alpha,
  559. transparentPixel,
  560. type,
  561. x,
  562. y,
  563. disposalMethod,
  564. delayTime);
  565. }
  566. /**
  567. * Returns the alpha value at offset <code>x</code> in
  568. * scanline <code>y</code> in the receiver's alpha data.
  569. * The alpha value is between 0 (transparent) and
  570. * 255 (opaque).
  571. *
  572. * @param x the x coordinate of the pixel to get the alpha value of
  573. * @param y the y coordinate of the pixel to get the alpha value of
  574. * @return the alpha value at the given coordinates
  575. *
  576. * @exception IllegalArgumentException <ul>
  577. * <li>ERROR_INVALID_ARGUMENT - if either argument is out of range</li>
  578. * </ul>
  579. */
  580. public int getAlpha(int x, int y) {
  581. if (x >= width || y >= height || x < 0 || y < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  582. if (alphaData is null) return 255;
  583. return alphaData[y * width + x] & 0xFF;
  584. }
  585. /**
  586. * Returns <code>getWidth</code> alpha values starting at offset
  587. * <code>x</code> in scanline <code>y</code> in the receiver's alpha
  588. * data starting at <code>startIndex</code>. The alpha values
  589. * are unsigned, between <code>(byte)0</code> (transparent) and
  590. * <code>(byte)255</code> (opaque).
  591. *
  592. * @param x the x position of the pixel to begin getting alpha values
  593. * @param y the y position of the pixel to begin getting alpha values
  594. * @param getWidth the width of the data to get
  595. * @param alphas the buffer in which to put the alpha values
  596. * @param startIndex the offset into the image to begin getting alpha values
  597. *
  598. * @exception IndexOutOfBoundsException if getWidth is too large
  599. * @exception IllegalArgumentException <ul>
  600. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  601. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  602. * <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li>
  603. * </ul>
  604. */
  605. public void getAlphas(int x, int y, int getWidth, byte[] alphas, int startIndex) {
  606. if (alphas is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
  607. if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  608. if (getWidth is 0) return;
  609. if (alphaData is null) {
  610. int endIndex = startIndex + getWidth;
  611. for (int i = startIndex; i < endIndex; i++) {
  612. alphas[i] = cast(byte)255;
  613. }
  614. return;
  615. }
  616. // may throw an IndexOutOfBoundsException
  617. System.arraycopy(alphaData, y * width + x, alphas, startIndex, getWidth);
  618. }
  619. /**
  620. * Returns the pixel value at offset <code>x</code> in
  621. * scanline <code>y</code> in the receiver's data.
  622. *
  623. * @param x the x position of the pixel to get
  624. * @param y the y position of the pixel to get
  625. * @return the pixel at the given coordinates
  626. *
  627. * @exception IllegalArgumentException <ul>
  628. * <li>ERROR_INVALID_ARGUMENT - if either argument is out of bounds</li>
  629. * </ul>
  630. * @exception DWTException <ul>
  631. * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li>
  632. * </ul>
  633. */
  634. public int getPixel(int x, int y) {
  635. if (x >= width || y >= height || x < 0 || y < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  636. int index;
  637. int theByte;
  638. int mask;
  639. switch (depth) {
  640. case 32:
  641. index = (y * bytesPerLine) + (x * 4);
  642. return ((data[index] & 0xFF) << 24) + ((data[index+1] & 0xFF) << 16) +
  643. ((data[index+2] & 0xFF) << 8) + (data[index+3] & 0xFF);
  644. case 24:
  645. index = (y * bytesPerLine) + (x * 3);
  646. return ((data[index] & 0xFF) << 16) + ((data[index+1] & 0xFF) << 8) +
  647. (data[index+2] & 0xFF);
  648. case 16:
  649. index = (y * bytesPerLine) + (x * 2);
  650. return ((data[index+1] & 0xFF) << 8) + (data[index] & 0xFF);
  651. case 8:
  652. index = (y * bytesPerLine) + x ;
  653. return data[index] & 0xFF;
  654. case 4:
  655. index = (y * bytesPerLine) + (x >> 1);
  656. theByte = data[index] & 0xFF;
  657. if ((x & 0x1) is 0) {
  658. return theByte >> 4;
  659. } else {
  660. return theByte & 0x0F;
  661. }
  662. case 2:
  663. index = (y * bytesPerLine) + (x >> 2);
  664. theByte = data[index] & 0xFF;
  665. int offset = 3 - (x % 4);
  666. mask = 3 << (offset * 2);
  667. return (theByte & mask) >> (offset * 2);
  668. case 1:
  669. index = (y * bytesPerLine) + (x >> 3);
  670. theByte = data[index] & 0xFF;
  671. mask = 1 << (7 - (x & 0x7));
  672. if ((theByte & mask) is 0) {
  673. return 0;
  674. } else {
  675. return 1;
  676. }
  677. default:
  678. }
  679. DWT.error(DWT.ERROR_UNSUPPORTED_DEPTH);
  680. return 0;
  681. }
  682. /**
  683. * Returns <code>getWidth</code> pixel values starting at offset
  684. * <code>x</code> in scanline <code>y</code> in the receiver's
  685. * data starting at <code>startIndex</code>.
  686. *
  687. * @param x the x position of the first pixel to get
  688. * @param y the y position of the first pixel to get
  689. * @param getWidth the width of the data to get
  690. * @param pixels the buffer in which to put the pixels
  691. * @param startIndex the offset into the byte array to begin storing pixels
  692. *
  693. * @exception IndexOutOfBoundsException if getWidth is too large
  694. * @exception IllegalArgumentException <ul>
  695. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  696. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  697. * <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li>
  698. * </ul>
  699. * @exception DWTException <ul>
  700. * <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4 or 8
  701. * (For higher depths, use the int[] version of this method.)</li>
  702. * </ul>
  703. */
  704. public void getPixels(int x, int y, int getWidth, byte[] pixels, int startIndex) {
  705. if (pixels is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
  706. if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  707. if (getWidth is 0) return;
  708. int index;
  709. int theByte;
  710. int mask = 0;
  711. int n = getWidth;
  712. int i = startIndex;
  713. int srcX = x, srcY = y;
  714. switch (depth) {
  715. case 8:
  716. index = (y * bytesPerLine) + x;
  717. for (int j = 0; j < getWidth; j++) {
  718. pixels[i] = data[index];
  719. i++;
  720. srcX++;
  721. if (srcX >= width) {
  722. srcY++;
  723. index = srcY * bytesPerLine;
  724. srcX = 0;
  725. } else {
  726. index++;
  727. }
  728. }
  729. return;
  730. case 4:
  731. index = (y * bytesPerLine) + (x >> 1);
  732. if ((x & 0x1) is 1) {
  733. theByte = data[index] & 0xFF;
  734. pixels[i] = cast(byte)(theByte & 0x0F);
  735. i++;
  736. n--;
  737. srcX++;
  738. if (srcX >= width) {
  739. srcY++;
  740. index = srcY * bytesPerLine;
  741. srcX = 0;
  742. } else {
  743. index++;
  744. }
  745. }
  746. while (n > 1) {
  747. theByte = data[index] & 0xFF;
  748. pixels[i] = cast(byte)(theByte >> 4);
  749. i++;
  750. n--;
  751. srcX++;
  752. if (srcX >= width) {
  753. srcY++;
  754. index = srcY * bytesPerLine;
  755. srcX = 0;
  756. } else {
  757. pixels[i] = cast(byte)(theByte & 0x0F);
  758. i++;
  759. n--;
  760. srcX++;
  761. if (srcX >= width) {
  762. srcY++;
  763. index = srcY * bytesPerLine;
  764. srcX = 0;
  765. } else {
  766. index++;
  767. }
  768. }
  769. }
  770. if (n > 0) {
  771. theByte = data[index] & 0xFF;
  772. pixels[i] = cast(byte)(theByte >> 4);
  773. }
  774. return;
  775. case 2:
  776. index = (y * bytesPerLine) + (x >> 2);
  777. theByte = data[index] & 0xFF;
  778. int offset;
  779. while (n > 0) {
  780. offset = 3 - (srcX % 4);
  781. mask = 3 << (offset * 2);
  782. pixels[i] = cast(byte)((theByte & mask) >> (offset * 2));
  783. i++;
  784. n--;
  785. srcX++;
  786. if (srcX >= width) {
  787. srcY++;
  788. index = srcY * bytesPerLine;
  789. if (n > 0) theByte = data[index] & 0xFF;
  790. srcX = 0;
  791. } else {
  792. if (offset is 0) {
  793. index++;
  794. theByte = data[index] & 0xFF;
  795. }
  796. }
  797. }
  798. return;
  799. case 1:
  800. index = (y * bytesPerLine) + (x >> 3);
  801. theByte = data[index] & 0xFF;
  802. while (n > 0) {
  803. mask = 1 << (7 - (srcX & 0x7));
  804. if ((theByte & mask) is 0) {
  805. pixels[i] = 0;
  806. } else {
  807. pixels[i] = 1;
  808. }
  809. i++;
  810. n--;
  811. srcX++;
  812. if (srcX >= width) {
  813. srcY++;
  814. index = srcY * bytesPerLine;
  815. if (n > 0) theByte = data[index] & 0xFF;
  816. srcX = 0;
  817. } else {
  818. if (mask is 1) {
  819. index++;
  820. if (n > 0) theByte = data[index] & 0xFF;
  821. }
  822. }
  823. }
  824. return;
  825. default:
  826. }
  827. DWT.error(DWT.ERROR_UNSUPPORTED_DEPTH);
  828. }
  829. /**
  830. * Returns <code>getWidth</code> pixel values starting at offset
  831. * <code>x</code> in scanline <code>y</code> in the receiver's
  832. * data starting at <code>startIndex</code>.
  833. *
  834. * @param x the x position of the first pixel to get
  835. * @param y the y position of the first pixel to get
  836. * @param getWidth the width of the data to get
  837. * @param pixels the buffer in which to put the pixels
  838. * @param startIndex the offset into the buffer to begin storing pixels
  839. *
  840. * @exception IndexOutOfBoundsException if getWidth is too large
  841. * @exception IllegalArgumentException <ul>
  842. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  843. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  844. * <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li>
  845. * </ul>
  846. * @exception DWTException <ul>
  847. * <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li>
  848. * </ul>
  849. */
  850. public void getPixels(int x, int y, int getWidth, int[] pixels, int startIndex) {
  851. if (pixels is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
  852. if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  853. if (getWidth is 0) return;
  854. int index;
  855. int theByte;
  856. int mask;
  857. int n = getWidth;
  858. int i = startIndex;
  859. int srcX = x, srcY = y;
  860. switch (depth) {
  861. case 32:
  862. index = (y * bytesPerLine) + (x * 4);
  863. i = startIndex;
  864. for (int j = 0; j < getWidth; j++) {
  865. pixels[i] = ((data[index] & 0xFF) << 24) | ((data[index+1] & 0xFF) << 16)
  866. | ((data[index+2] & 0xFF) << 8) | (data[index+3] & 0xFF);
  867. i++;
  868. srcX++;
  869. if (srcX >= width) {
  870. srcY++;
  871. index = srcY * bytesPerLine;
  872. srcX = 0;
  873. } else {
  874. index += 4;
  875. }
  876. }
  877. return;
  878. case 24:
  879. index = (y * bytesPerLine) + (x * 3);
  880. for (int j = 0; j < getWidth; j++) {
  881. pixels[i] = ((data[index] & 0xFF) << 16) | ((data[index+1] & 0xFF) << 8)
  882. | (data[index+2] & 0xFF);
  883. i++;
  884. srcX++;
  885. if (srcX >= width) {
  886. srcY++;
  887. index = srcY * bytesPerLine;
  888. srcX = 0;
  889. } else {
  890. index += 3;
  891. }
  892. }
  893. return;
  894. case 16:
  895. index = (y * bytesPerLine) + (x * 2);
  896. for (int j = 0; j < getWidth; j++) {
  897. pixels[i] = ((data[index+1] & 0xFF) << 8) + (data[index] & 0xFF);
  898. i++;
  899. srcX++;
  900. if (srcX >= width) {
  901. srcY++;
  902. index = srcY * bytesPerLine;
  903. srcX = 0;
  904. } else {
  905. index += 2;
  906. }
  907. }
  908. return;
  909. case 8:
  910. index = (y * bytesPerLine) + x;
  911. for (int j = 0; j < getWidth; j++) {
  912. pixels[i] = data[index] & 0xFF;
  913. i++;
  914. srcX++;
  915. if (srcX >= width) {
  916. srcY++;
  917. index = srcY * bytesPerLine;
  918. srcX = 0;
  919. } else {
  920. index++;
  921. }
  922. }
  923. return;
  924. case 4:
  925. index = (y * bytesPerLine) + (x >> 1);
  926. if ((x & 0x1) is 1) {
  927. theByte = data[index] & 0xFF;
  928. pixels[i] = theByte & 0x0F;
  929. i++;
  930. n--;
  931. srcX++;
  932. if (srcX >= width) {
  933. srcY++;
  934. index = srcY * bytesPerLine;
  935. srcX = 0;
  936. } else {
  937. index++;
  938. }
  939. }
  940. while (n > 1) {
  941. theByte = data[index] & 0xFF;
  942. pixels[i] = theByte >> 4;
  943. i++;
  944. n--;
  945. srcX++;
  946. if (srcX >= width) {
  947. srcY++;
  948. index = srcY * bytesPerLine;
  949. srcX = 0;
  950. } else {
  951. pixels[i] = theByte & 0x0F;
  952. i++;
  953. n--;
  954. srcX++;
  955. if (srcX >= width) {
  956. srcY++;
  957. index = srcY * bytesPerLine;
  958. srcX = 0;
  959. } else {
  960. index++;
  961. }
  962. }
  963. }
  964. if (n > 0) {
  965. theByte = data[index] & 0xFF;
  966. pixels[i] = theByte >> 4;
  967. }
  968. return;
  969. case 2:
  970. index = (y * bytesPerLine) + (x >> 2);
  971. theByte = data[index] & 0xFF;
  972. int offset;
  973. while (n > 0) {
  974. offset = 3 - (srcX % 4);
  975. mask = 3 << (offset * 2);
  976. pixels[i] = cast(byte)((theByte & mask) >> (offset * 2));
  977. i++;
  978. n--;
  979. srcX++;
  980. if (srcX >= width) {
  981. srcY++;
  982. index = srcY * bytesPerLine;
  983. if (n > 0) theByte = data[index] & 0xFF;
  984. srcX = 0;
  985. } else {
  986. if (offset is 0) {
  987. index++;
  988. theByte = data[index] & 0xFF;
  989. }
  990. }
  991. }
  992. return;
  993. case 1:
  994. index = (y * bytesPerLine) + (x >> 3);
  995. theByte = data[index] & 0xFF;
  996. while (n > 0) {
  997. mask = 1 << (7 - (srcX & 0x7));
  998. if ((theByte & mask) is 0) {
  999. pixels[i] = 0;
  1000. } else {
  1001. pixels[i] = 1;
  1002. }
  1003. i++;
  1004. n--;
  1005. srcX++;
  1006. if (srcX >= width) {
  1007. srcY++;
  1008. index = srcY * bytesPerLine;
  1009. if (n > 0) theByte = data[index] & 0xFF;
  1010. srcX = 0;
  1011. } else {
  1012. if (mask is 1) {
  1013. index++;
  1014. if (n > 0) theByte = data[index] & 0xFF;
  1015. }
  1016. }
  1017. }
  1018. return;
  1019. default:
  1020. }
  1021. DWT.error(DWT.ERROR_UNSUPPORTED_DEPTH);
  1022. }
  1023. /**
  1024. * Returns an array of <code>RGB</code>s which comprise the
  1025. * indexed color table of the receiver, or null if the receiver
  1026. * has a direct color model.
  1027. *
  1028. * @return the RGB values for the image or null if direct color
  1029. *
  1030. * @see PaletteData#getRGBs()
  1031. */
  1032. public RGB[] getRGBs() {
  1033. return palette.getRGBs();
  1034. }
  1035. /**
  1036. * Returns an <code>ImageData</code> which specifies the
  1037. * transparency mask information for the receiver. If the
  1038. * receiver has no transparency or is not an icon, returns
  1039. * an opaque mask.
  1040. *
  1041. * @return the transparency mask
  1042. */
  1043. public ImageData getTransparencyMask() {
  1044. if (getTransparencyType() is DWT.TRANSPARENCY_MASK) {
  1045. return new ImageData(width, height, 1, bwPalette(), maskPad, maskData);
  1046. } else {
  1047. return colorMaskImage(transparentPixel);
  1048. }
  1049. }
  1050. /**
  1051. * Returns the image transparency type, which will be one of
  1052. * <code>DWT.TRANSPARENCY_NONE</code>, <code>DWT.TRANSPARENCY_MASK</code>,
  1053. * <code>DWT.TRANSPARENCY_PIXEL</code> or <code>DWT.TRANSPARENCY_ALPHA</code>.
  1054. *
  1055. * @return the receiver's transparency type
  1056. */
  1057. public int getTransparencyType() {
  1058. if (maskData !is null) return DWT.TRANSPARENCY_MASK;
  1059. if (transparentPixel !is -1) return DWT.TRANSPARENCY_PIXEL;
  1060. if (alphaData !is null) return DWT.TRANSPARENCY_ALPHA;
  1061. return DWT.TRANSPARENCY_NONE;
  1062. }
  1063. /**
  1064. * Returns the byte order of the receiver.
  1065. *
  1066. * @return MSB_FIRST or LSB_FIRST
  1067. */
  1068. int getByteOrder() {
  1069. return depth !is 16 ? MSB_FIRST : LSB_FIRST;
  1070. }
  1071. /**
  1072. * Returns a copy of the receiver which has been stretched or
  1073. * shrunk to the specified size. If either the width or height
  1074. * is negative, the resulting image will be inverted in the
  1075. * associated axis.
  1076. *
  1077. * @param width the width of the new ImageData
  1078. * @param height the height of the new ImageData
  1079. * @return a scaled copy of the image
  1080. */
  1081. public ImageData scaledTo(int width, int height) {
  1082. /* Create a destination image with no data */
  1083. bool flipX = (width < 0);
  1084. if (flipX) width = - width;
  1085. bool flipY = (height < 0);
  1086. if (flipY) height = - height;
  1087. ImageData dest = new ImageData(
  1088. width, height, depth, palette,
  1089. scanlinePad, null, 0, null,
  1090. null, -1, transparentPixel, type,
  1091. x, y, disposalMethod, delayTime);
  1092. /* Scale the image contents */
  1093. if (palette.isDirect) blit(BLIT_SRC,
  1094. this.data, this.depth, this.bytesPerLine, this.getByteOrder(), 0, 0, this.width, this.height, 0, 0, 0,
  1095. ALPHA_OPAQUE, null, 0, 0, 0,
  1096. dest.data, dest.depth, dest.bytesPerLine, dest.getByteOrder(), 0, 0, dest.width, dest.height, 0, 0, 0,
  1097. flipX, flipY);
  1098. else blit(BLIT_SRC,
  1099. this.data, this.depth, this.bytesPerLine, this.getByteOrder(), 0, 0, this.width, this.height, null, null, null,
  1100. ALPHA_OPAQUE, null, 0, 0, 0,
  1101. dest.data, dest.depth, dest.bytesPerLine, dest.getByteOrder(), 0, 0, dest.width, dest.height, null, null, null,
  1102. flipX, flipY);
  1103. /* Scale the image mask or alpha */
  1104. if (maskData !is null) {
  1105. dest.maskPad = this.maskPad;
  1106. int destBpl = (dest.width + 7) / 8;
  1107. destBpl = (destBpl + (dest.maskPad - 1)) / dest.maskPad * dest.maskPad;
  1108. dest.maskData = new byte[destBpl * dest.height];
  1109. int srcBpl = (this.width + 7) / 8;
  1110. srcBpl = (srcBpl + (this.maskPad - 1)) / this.maskPad * this.maskPad;
  1111. blit(BLIT_SRC,
  1112. this.maskData, 1, srcBpl, MSB_FIRST, 0, 0, this.width, this.height, null, null, null,
  1113. ALPHA_OPAQUE, null, 0, 0, 0,
  1114. dest.maskData, 1, destBpl, MSB_FIRST, 0, 0, dest.width, dest.height, null, null, null,
  1115. flipX, flipY);
  1116. } else if (alpha !is -1) {
  1117. dest.alpha = this.alpha;
  1118. } else if (alphaData !is null) {
  1119. dest.alphaData = new byte[dest.width * dest.height];
  1120. blit(BLIT_SRC,
  1121. this.alphaData, 8, this.width, MSB_FIRST, 0, 0, this.width, this.height, null, null, null,
  1122. ALPHA_OPAQUE, null, 0, 0, 0,
  1123. dest.alphaData, 8, dest.width, MSB_FIRST, 0, 0, dest.width, dest.height, null, null, null,
  1124. flipX, flipY);
  1125. }
  1126. return dest;
  1127. }
  1128. /**
  1129. * Sets the alpha value at offset <code>x</code> in
  1130. * scanline <code>y</code> in the receiver's alpha data.
  1131. * The alpha value must be between 0 (transparent)
  1132. * and 255 (opaque).
  1133. *
  1134. * @param x the x coordinate of the alpha value to set
  1135. * @param y the y coordinate of the alpha value to set
  1136. * @param alpha the value to set the alpha to
  1137. *
  1138. * @exception IllegalArgumentException <ul>
  1139. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  1140. * </ul>
  1141. */
  1142. public void setAlpha(int x, int y, int alpha) {
  1143. if (x >= width || y >= height || x < 0 || y < 0 || alpha < 0 || alpha > 255)
  1144. DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  1145. if (alphaData is null) alphaData = new byte[width * height];
  1146. alphaData[y * width + x] = cast(byte)alpha;
  1147. }
  1148. /**
  1149. * Sets the alpha values starting at offset <code>x</code> in
  1150. * scanline <code>y</code> in the receiver's alpha data to the
  1151. * values from the array <code>alphas</code> starting at
  1152. * <code>startIndex</code>. The alpha values must be between
  1153. * <code>(byte)0</code> (transparent) and <code>(byte)255</code> (opaque)
  1154. *
  1155. * @param x the x coordinate of the pixel to being setting the alpha values
  1156. * @param y the y coordinate of the pixel to being setting the alpha values
  1157. * @param putWidth the width of the alpha values to set
  1158. * @param alphas the alpha values to set
  1159. * @param startIndex the index at which to begin setting
  1160. *
  1161. * @exception IndexOutOfBoundsException if putWidth is too large
  1162. * @exception IllegalArgumentException <ul>
  1163. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  1164. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  1165. * <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li>
  1166. * </ul>
  1167. */
  1168. public void setAlphas(int x, int y, int putWidth, byte[] alphas, int startIndex) {
  1169. if (alphas is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
  1170. if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  1171. if (putWidth is 0) return;
  1172. if (alphaData is null) alphaData = new byte[width * height];
  1173. // may throw an IndexOutOfBoundsException
  1174. System.arraycopy(alphas, startIndex, alphaData, y * width + x, putWidth);
  1175. }
  1176. /**
  1177. * Sets the pixel value at offset <code>x</code> in
  1178. * scanline <code>y</code> in the receiver's data.
  1179. *
  1180. * @param x the x coordinate of the pixel to set
  1181. * @param y the y coordinate of the pixel to set
  1182. * @param pixelValue the value to set the pixel to
  1183. *
  1184. * @exception IllegalArgumentException <ul>
  1185. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  1186. * </ul>
  1187. * @exception DWTException <ul>
  1188. * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li>
  1189. * </ul>
  1190. */
  1191. public void setPixel(int x, int y, int pixelValue) {
  1192. if (x >= width || y >= height || x < 0 || y < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  1193. int index;
  1194. byte theByte;
  1195. int mask;
  1196. switch (depth) {
  1197. case 32:
  1198. index = (y * bytesPerLine) + (x * 4);
  1199. data[index] = cast(byte)((pixelValue >> 24) & 0xFF);
  1200. data[index + 1] = cast(byte)((pixelValue >> 16) & 0xFF);
  1201. data[index + 2] = cast(byte)((pixelValue >> 8) & 0xFF);
  1202. data[index + 3] = cast(byte)(pixelValue & 0xFF);
  1203. return;
  1204. case 24:
  1205. index = (y * bytesPerLine) + (x * 3);
  1206. data[index] = cast(byte)((pixelValue >> 16) & 0xFF);
  1207. data[index + 1] = cast(byte)((pixelValue >> 8) & 0xFF);
  1208. data[index + 2] = cast(byte)(pixelValue & 0xFF);
  1209. return;
  1210. case 16:
  1211. index = (y * bytesPerLine) + (x * 2);
  1212. data[index + 1] = cast(byte)((pixelValue >> 8) & 0xFF);
  1213. data[index] = cast(byte)(pixelValue & 0xFF);
  1214. return;
  1215. case 8:
  1216. index = (y * bytesPerLine) + x ;
  1217. data[index] = cast(byte)(pixelValue & 0xFF);
  1218. return;
  1219. case 4:
  1220. index = (y * bytesPerLine) + (x >> 1);
  1221. if ((x & 0x1) is 0) {
  1222. data[index] = cast(byte)((data[index] & 0x0F) | ((pixelValue & 0x0F) << 4));
  1223. } else {
  1224. data[index] = cast(byte)((data[index] & 0xF0) | (pixelValue & 0x0F));
  1225. }
  1226. return;
  1227. case 2:
  1228. index = (y * bytesPerLine) + (x >> 2);
  1229. theByte = data[index];
  1230. int offset = 3 - (x % 4);
  1231. mask = 0xFF ^ (3 << (offset * 2));
  1232. data[index] = cast(byte)((data[index] & mask) | (pixelValue << (offset * 2)));
  1233. return;
  1234. case 1:
  1235. index = (y * bytesPerLine) + (x >> 3);
  1236. theByte = data[index];
  1237. mask = 1 << (7 - (x & 0x7));
  1238. if ((pixelValue & 0x1) is 1) {
  1239. data[index] = cast(byte)(theByte | mask);
  1240. } else {
  1241. data[index] = cast(byte)(theByte & (mask ^ -1));
  1242. }
  1243. return;
  1244. default:
  1245. }
  1246. DWT.error(DWT.ERROR_UNSUPPORTED_DEPTH);
  1247. }
  1248. /**
  1249. * Sets the pixel values starting at offset <code>x</code> in
  1250. * scanline <code>y</code> in the receiver's data to the
  1251. * values from the array <code>pixels</code> starting at
  1252. * <code>startIndex</code>.
  1253. *
  1254. * @param x the x position of the pixel to set
  1255. * @param y the y position of the pixel to set
  1256. * @param putWidth the width of the pixels to set
  1257. * @param pixels the pixels to set
  1258. * @param startIndex the index at which to begin setting
  1259. *
  1260. * @exception IndexOutOfBoundsException if putWidth is too large
  1261. * @exception IllegalArgumentException <ul>
  1262. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  1263. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  1264. * <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li>
  1265. * </ul>
  1266. * @exception DWTException <ul>
  1267. * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8
  1268. * (For higher depths, use the int[] version of this method.)</li>
  1269. * </ul>
  1270. */
  1271. public void setPixels(int x, int y, int putWidth, byte[] pixels, int startIndex) {
  1272. if (pixels is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
  1273. if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
  1274. if (putWidth is 0) return;
  1275. int index;
  1276. int theByte;
  1277. int mask;
  1278. int n = putWidth;
  1279. int i = startIndex;
  1280. int srcX = x, srcY = y;
  1281. switch (depth) {
  1282. case 8:
  1283. index = (y * bytesPerLine) + x;
  1284. for (int j = 0; j < putWidth; j++) {
  1285. data[index] = cast(byte)(pixels[i] & 0xFF);
  1286. i++;
  1287. srcX++;
  1288. if (srcX >= width) {
  1289. srcY++;
  1290. index = srcY * bytesPerLine;
  1291. srcX = 0;
  1292. } else {
  1293. index++;
  1294. }
  1295. }
  1296. return;
  1297. case 4:
  1298. index = (y * bytesPerLine) + (x >> 1);
  1299. bool high = (x & 0x1) is 0;
  1300. while (n > 0) {
  1301. theByte = pixels[i] & 0x0F;
  1302. if (high) {
  1303. data[index] = cast(byte)((data[index] & 0x0F) | (theByte << 4));
  1304. } else {
  1305. data[index] = cast(byte)((data[index] & 0xF0) | theByte);
  1306. }
  1307. i++;
  1308. n--;
  1309. srcX++;
  1310. if (srcX >= width) {
  1311. srcY++;
  1312. index = srcY * bytesPerLine;
  1313. high = true;
  1314. srcX = 0;
  1315. } else {
  1316. if (!high) index++;
  1317. high = !high;
  1318. }
  1319. }
  1320. return;
  1321. case 2:
  1322. byte [] masks = [ cast(byte)0xFC, cast(byte)0xF3, cast(byte)0xCF, cast(byte)0x3F ];
  1323. index = (y * bytesPerLine) + (x >> 2);
  1324. int offset = 3 - (x % 4);
  1325. while (n > 0) {
  1326. theByte = pixels[i] & 0x3;
  1327. data[index] = cast(byte)((data[index] & masks[offset]) | (theByte << (offset * 2)));
  1328. i++;
  1329. n--;
  1330. srcX++;
  1331. if (srcX >= width) {
  1332. srcY++;
  1333. index = srcY * bytesPerLine;
  1334. offset = 0;
  1335. srcX = 0;
  1336. } else {
  1337. if (offset is 0) {
  1338. index++;
  1339. offset = 3;
  1340. } else {
  1341. offset--;
  1342. }
  1343. }
  1344. }
  1345. return;
  1346. case 1:
  1347. index = (y * bytesPerLine) + (x >> 3);
  1348. while (n > 0) {
  1349. mask = 1 << (7 - (srcX & 0x7));
  1350. if ((pixels[i] & 0x1) is 1) {
  1351. data[index] = cast(byte)((data[index] & 0xFF) | mask);
  1352. } else {
  1353. data[index] = cast(byte)((data[index] & 0xFF) & (mask ^ -1));
  1354. }
  1355. i++;
  1356. n--;
  1357. srcX++;
  1358. if (srcX >= width) {
  1359. srcY++;
  1360. index = srcY * bytesPerLine;
  1361. srcX = 0;
  1362. } else {
  1363. if (mask is 1) {
  1364. index++;
  1365. }
  1366. }
  1367. }
  1368. return;
  1369. default:
  1370. }
  1371. DWT.error(DWT.ERROR_UNSUPPORTED_DEPTH);
  1372. }
  1373. /**
  1374. * Sets the pixel values starting at offset <code>x</code> in
  1375. * scanline <code>y</code> in the receiver's data to the
  1376. * values from the array <code>pixels</code> starting at
  1377. * <code>startIndex</code>.
  1378. *
  1379. * @param x the x position of the pixel to set
  1380. * @param y the y position of the pixel to set
  1381. * @param putWidth the width of the pixels to set
  1382. * @param pixels the pixels to set
  1383. * @param startIndex the index at which to begin setting
  1384. *
  1385. * @exception IndexOutOfBoundsException if putWidth is too large
  1386. * @exception IllegalArgumentException <ul>
  1387. * <li>ERROR_NULL_ARGUMENT - if pixels is null</li>
  1388. * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li>
  1389. * <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li>
  1390. * </ul>
  1391. * @exception DWTException <ul>
  1392. * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8, 16, 24 or 3…

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