/binding/win32/gdiplusbitmap.d

http://github.com/wilkie/djehuty · D · 712 lines · 475 code · 152 blank · 85 comment · 18 complexity · a313dbd271f532047e5ec055d6f42261 MD5 · raw file

  1. /*
  2. * gdiplusbitmap.d
  3. *
  4. * This module implements GdiPlusBitmap.h for D. The original
  5. * copyright info is given below.
  6. *
  7. * Author: Dave Wilkinson
  8. * Originated: November 25th, 2009
  9. *
  10. */
  11. module binding.win32.gdiplusbitmap;
  12. import binding.win32.windef;
  13. import binding.win32.winbase;
  14. import binding.win32.winnt;
  15. import binding.win32.wingdi;
  16. import binding.win32.guiddef;
  17. import binding.win32.gdiplusbase;
  18. import binding.win32.gdiplustypes;
  19. import binding.win32.gdiplusenums;
  20. import binding.win32.gdipluspixelformats;
  21. import binding.win32.gdiplusgpstubs;
  22. import binding.win32.gdiplusmetaheader;
  23. import binding.win32.gdipluspixelformats;
  24. import binding.win32.gdipluscolor;
  25. import binding.win32.gdipluscolormatrix;
  26. import binding.win32.gdiplusflat;
  27. import binding.win32.gdiplusimaging;
  28. import binding.win32.gdiplusgraphics;
  29. class Image : GdiplusBase {
  30. this(in WCHAR* filename, in BOOL useEmbeddedColorManagement = FALSE) {
  31. nativeImage = null;
  32. if(useEmbeddedColorManagement)
  33. {
  34. lastResult = GdipLoadImageFromFileICM(
  35. filename,
  36. &nativeImage
  37. );
  38. }
  39. else
  40. {
  41. lastResult = GdipLoadImageFromFile(
  42. filename,
  43. &nativeImage
  44. );
  45. }
  46. }
  47. //this(in IStream* stream, in BOOL useEmbeddedColorManagement = FALSE) {
  48. //}
  49. static Image FromFile(in WCHAR* filename, in BOOL useEmbeddedColorManagement = FALSE) {
  50. return new Image(
  51. filename,
  52. useEmbeddedColorManagement
  53. );
  54. }
  55. // static Image* FromStream(
  56. // in IStream* stream,
  57. // in BOOL useEmbeddedColorManagement = FALSE
  58. // );
  59. ~this() {
  60. GdipDisposeImage(nativeImage);
  61. }
  62. Image Clone() {
  63. GpImage *cloneimage = null;
  64. SetStatus(GdipCloneImage(nativeImage, &cloneimage));
  65. return new Image(cloneimage, lastResult);
  66. }
  67. Status Save(in WCHAR* filename,
  68. in CLSID* clsidEncoder,
  69. in EncoderParameters *encoderParams = null) {
  70. return SetStatus(GdipSaveImageToFile(nativeImage,
  71. filename,
  72. clsidEncoder,
  73. encoderParams));
  74. }
  75. // Status Save(in IStream* stream,
  76. // in CLSID* clsidEncoder,
  77. // in EncoderParameters *encoderParams = null);
  78. Status SaveAdd(in EncoderParameters* encoderParams) {
  79. return SetStatus(GdipSaveAdd(nativeImage,
  80. encoderParams));
  81. }
  82. Status SaveAdd(in Image* newImage,
  83. in EncoderParameters* encoderParams) {
  84. if ( newImage is null ) {
  85. return SetStatus(Status.InvalidParameter);
  86. }
  87. return SetStatus(GdipSaveAddImage(nativeImage,
  88. newImage.nativeImage,
  89. encoderParams));
  90. }
  91. ImageType GetType() {
  92. ImageType type = ImageType.ImageTypeUnknown;
  93. SetStatus(GdipGetImageType(nativeImage, &type));
  94. return type;
  95. }
  96. Status GetPhysicalDimension(SizeF* size) {
  97. if (size is null) {
  98. return SetStatus(Status.InvalidParameter);
  99. }
  100. REAL width, height;
  101. Status status;
  102. status = SetStatus(GdipGetImageDimension(nativeImage,
  103. &width, &height));
  104. size.Width = width;
  105. size.Height = height;
  106. return status;
  107. }
  108. Status GetBounds(RectF* srcRect,
  109. Unit* srcUnit) {
  110. return SetStatus(GdipGetImageBounds(nativeImage,
  111. srcRect, srcUnit));
  112. }
  113. UINT GetWidth() {
  114. UINT width = 0;
  115. SetStatus(GdipGetImageWidth(nativeImage, &width));
  116. return width;
  117. }
  118. UINT GetHeight() {
  119. UINT height = 0;
  120. SetStatus(GdipGetImageHeight(nativeImage, &height));
  121. return height;
  122. }
  123. REAL GetHorizontalResolution() {
  124. REAL resolution = 0.0f;
  125. SetStatus(GdipGetImageHorizontalResolution(nativeImage, &resolution));
  126. return resolution;
  127. }
  128. REAL GetVerticalResolution() {
  129. REAL resolution = 0.0f;
  130. SetStatus(GdipGetImageVerticalResolution(nativeImage, &resolution));
  131. return resolution;
  132. }
  133. UINT GetFlags() {
  134. UINT flags = 0;
  135. SetStatus(GdipGetImageFlags(nativeImage, &flags));
  136. return flags;
  137. }
  138. Status GetRawFormat(GUID *format) {
  139. return SetStatus(GdipGetImageRawFormat(nativeImage, format));
  140. }
  141. PixelFormat GetPixelFormat() {
  142. PixelFormat format;
  143. SetStatus(GdipGetImagePixelFormat(nativeImage, &format));
  144. return format;
  145. }
  146. INT GetPaletteSize() {
  147. INT size = 0;
  148. SetStatus(GdipGetImagePaletteSize(nativeImage, &size));
  149. return size;
  150. }
  151. Status GetPalette(ColorPalette* palette,
  152. in INT size) {
  153. return SetStatus(GdipGetImagePalette(nativeImage, palette, size));
  154. }
  155. Status SetPalette(in ColorPalette* palette) {
  156. return SetStatus(GdipSetImagePalette(nativeImage, palette));
  157. }
  158. Image GetThumbnailImage(in UINT thumbWidth,
  159. in UINT thumbHeight,
  160. in GetThumbnailImageAbort callback = null,
  161. in VOID* callbackData = null) {
  162. GpImage *thumbimage = null;
  163. SetStatus(GdipGetImageThumbnail(nativeImage,
  164. thumbWidth, thumbHeight,
  165. &thumbimage,
  166. callback, callbackData));
  167. Image newImage = new Image(thumbimage, lastResult);
  168. if (newImage is null) {
  169. GdipDisposeImage(thumbimage);
  170. }
  171. return newImage;
  172. }
  173. UINT GetFrameDimensionsCount() {
  174. UINT count = 0;
  175. SetStatus(GdipImageGetFrameDimensionsCount(nativeImage,
  176. &count));
  177. return count;
  178. }
  179. Status GetFrameDimensionsList(GUID* dimensionIDs,
  180. in UINT count) {
  181. return SetStatus(GdipImageGetFrameDimensionsList(nativeImage,
  182. dimensionIDs,
  183. count));
  184. }
  185. UINT GetFrameCount(in GUID* dimensionID) {
  186. UINT count = 0;
  187. SetStatus(GdipImageGetFrameCount(nativeImage,
  188. dimensionID,
  189. &count));
  190. return count;
  191. }
  192. Status SelectActiveFrame(in GUID* dimensionID,
  193. in UINT frameIndex) {
  194. return SetStatus(GdipImageSelectActiveFrame(nativeImage,
  195. dimensionID,
  196. frameIndex));
  197. }
  198. Status RotateFlip(in RotateFlipType rotateFlipType) {
  199. return SetStatus(GdipImageRotateFlip(nativeImage,
  200. rotateFlipType));
  201. }
  202. UINT GetPropertyCount() {
  203. UINT numProperty = 0;
  204. SetStatus(GdipGetPropertyCount(nativeImage,
  205. &numProperty));
  206. return numProperty;
  207. }
  208. Status GetPropertyIdList(in UINT numOfProperty,
  209. PROPID* list) {
  210. return SetStatus(GdipGetPropertyIdList(nativeImage,
  211. numOfProperty, list));
  212. }
  213. UINT GetPropertyItemSize(in PROPID propId) {
  214. UINT size = 0;
  215. SetStatus(GdipGetPropertyItemSize(nativeImage,
  216. propId,
  217. &size));
  218. return size;
  219. }
  220. Status GetPropertyItem(in PROPID propId,
  221. in UINT propSize,
  222. PropertyItem* buffer) {
  223. return SetStatus(GdipGetPropertyItem(nativeImage,
  224. propId, propSize, buffer));
  225. }
  226. Status GetPropertySize(UINT* totalBufferSize,
  227. UINT* numProperties) {
  228. return SetStatus(GdipGetPropertySize(nativeImage,
  229. totalBufferSize,
  230. numProperties));
  231. }
  232. Status GetAllPropertyItems(in UINT totalBufferSize,
  233. in UINT numProperties,
  234. PropertyItem* allItems) {
  235. if (allItems == null) {
  236. return SetStatus(Status.InvalidParameter);
  237. }
  238. return SetStatus(GdipGetAllPropertyItems(nativeImage,
  239. totalBufferSize,
  240. numProperties,
  241. allItems));
  242. }
  243. Status RemovePropertyItem(in PROPID propId) {
  244. return SetStatus(GdipRemovePropertyItem(nativeImage, propId));
  245. }
  246. Status SetPropertyItem(in PropertyItem* item) {
  247. return SetStatus(GdipSetPropertyItem(nativeImage, item));
  248. }
  249. UINT GetEncoderParameterListSize(in CLSID* clsidEncoder) {
  250. UINT size = 0;
  251. SetStatus(GdipGetEncoderParameterListSize(nativeImage,
  252. clsidEncoder,
  253. &size));
  254. return size;
  255. }
  256. Status GetEncoderParameterList(in CLSID* clsidEncoder,
  257. in UINT size,
  258. EncoderParameters* buffer) {
  259. return SetStatus(GdipGetEncoderParameterList(nativeImage,
  260. clsidEncoder,
  261. size,
  262. buffer));
  263. }
  264. version(GDIPLUS6) {
  265. Status FindFirstItem(in ImageItemData *item) {
  266. return SetStatus(GdipFindFirstImageItem(nativeImage, item));
  267. }
  268. Status FindNextItem(in ImageItemData *item) {
  269. return SetStatus(GdipFindNextImageItem(nativeImage, item));
  270. }
  271. Status GetItemData(in ImageItemData *item) {
  272. return SetStatus(GdipGetImageItemData(nativeImage, item));
  273. }
  274. Status SetAbort(GdiplusAbort *pIAbort) {
  275. return SetStatus(GdipImageSetAbort(
  276. nativeImage,
  277. pIAbort
  278. ));
  279. }
  280. }
  281. Status GetLastStatus() {
  282. Status lastStatus = lastResult;
  283. lastResult = Status.Ok;
  284. return lastStatus;
  285. }
  286. protected:
  287. this() {}
  288. package this(GpImage *nativeImage, Status status) {
  289. SetNativeImage(nativeImage);
  290. lastResult = status;
  291. }
  292. VOID SetNativeImage(GpImage* nativeImage) {
  293. this.nativeImage = nativeImage;
  294. }
  295. Status SetStatus(Status status) {
  296. if (status != Status.Ok)
  297. return (lastResult = status);
  298. else
  299. return status;
  300. }
  301. package GpImage* nativeImage;
  302. package Status lastResult;
  303. package Status loadStatus;
  304. }
  305. class Bitmap : Image {
  306. this(in WCHAR* filename, in BOOL useEmbeddedColorManagement = FALSE) {
  307. GpBitmap *bitmap = null;
  308. if(useEmbeddedColorManagement) {
  309. lastResult = GdipCreateBitmapFromFileICM(filename, &bitmap);
  310. }
  311. else {
  312. lastResult = GdipCreateBitmapFromFile(filename, &bitmap);
  313. }
  314. SetNativeImage(bitmap);
  315. }
  316. /*
  317. this(in IStream *stream, in BOOL useEmbeddedColorManagement = FALSE) {
  318. GpBitmap *bitmap = null;
  319. if(useEmbeddedColorManagement) {
  320. lastResult = GdipCreateBitmapFromStreamICM(stream, &bitmap);
  321. }
  322. else {
  323. lastResult = GdipCreateBitmapFromStream(stream, &bitmap);
  324. }
  325. SetNativeImage(bitmap);
  326. }
  327. */
  328. static Bitmap FromFile(in WCHAR* filename, in BOOL useEmbeddedColorManagement = FALSE) {
  329. return new Bitmap(
  330. filename,
  331. useEmbeddedColorManagement
  332. );
  333. }
  334. /*
  335. static Bitmap FromStream(in IStream *stream, in BOOL useEmbeddedColorManagement = FALSE) {
  336. return new Bitmap(
  337. stream,
  338. useEmbeddedColorManagement
  339. );
  340. }
  341. */
  342. this(in INT width, in INT height, in INT stride, PixelFormat format, in BYTE* scan0) {
  343. GpBitmap *bitmap = null;
  344. lastResult = GdipCreateBitmapFromScan0(width,
  345. height,
  346. stride,
  347. format,
  348. scan0,
  349. &bitmap);
  350. SetNativeImage(bitmap);
  351. }
  352. this(in INT width, in INT height, in PixelFormat format = PixelFormat32bppARGB) {
  353. GpBitmap *bitmap = null;
  354. lastResult = GdipCreateBitmapFromScan0(width,
  355. height,
  356. 0,
  357. format,
  358. null,
  359. &bitmap);
  360. SetNativeImage(bitmap);
  361. }
  362. this(in INT width, in INT height, in Graphics target) {
  363. GpBitmap *bitmap = null;
  364. lastResult = GdipCreateBitmapFromGraphics(width,
  365. height,
  366. target.nativeGraphics,
  367. &bitmap);
  368. SetNativeImage(bitmap);
  369. }
  370. Bitmap Clone(in Rect rect, in PixelFormat format) {
  371. return Clone(rect.X, rect.Y, rect.Width, rect.Height, format);
  372. }
  373. Bitmap Clone(in INT x, in INT y, in INT width, in INT height, in PixelFormat format) {
  374. GpBitmap* gpdstBitmap = null;
  375. Bitmap bitmap;
  376. lastResult = GdipCloneBitmapAreaI(
  377. x,
  378. y,
  379. width,
  380. height,
  381. format,
  382. cast(GpBitmap *)nativeImage,
  383. &gpdstBitmap);
  384. if (lastResult == Status.Ok) {
  385. bitmap = new Bitmap(gpdstBitmap);
  386. if (bitmap is null) {
  387. GdipDisposeImage(gpdstBitmap);
  388. }
  389. return bitmap;
  390. }
  391. else
  392. return null;
  393. }
  394. Bitmap Clone(in RectF rect, in PixelFormat format) {
  395. return Clone(rect.X, rect.Y, rect.Width, rect.Height, format);
  396. }
  397. Bitmap Clone(in REAL x, in REAL y, in REAL width, in REAL height, in PixelFormat format) {
  398. GpBitmap* gpdstBitmap = null;
  399. Bitmap bitmap;
  400. SetStatus(GdipCloneBitmapArea(
  401. x,
  402. y,
  403. width,
  404. height,
  405. format,
  406. cast(GpBitmap *)nativeImage,
  407. &gpdstBitmap));
  408. if (lastResult == Status.Ok) {
  409. bitmap = new Bitmap(gpdstBitmap);
  410. if (bitmap is null) {
  411. GdipDisposeImage(gpdstBitmap);
  412. }
  413. return bitmap;
  414. }
  415. else
  416. return null;
  417. }
  418. Status LockBits(in Rect* rect, in UINT flags, in PixelFormat format, BitmapData* lockedBitmapData) {
  419. return SetStatus(GdipBitmapLockBits(
  420. cast(GpBitmap*)(nativeImage),
  421. rect,
  422. flags,
  423. format,
  424. lockedBitmapData));
  425. }
  426. Status UnlockBits(in BitmapData* lockedBitmapData) {
  427. return SetStatus(GdipBitmapUnlockBits(
  428. cast(GpBitmap*)(nativeImage),
  429. lockedBitmapData));
  430. }
  431. Status GetPixel(in INT x, in INT y, Color *color) {
  432. ARGB argb;
  433. Status status = SetStatus(GdipBitmapGetPixel(
  434. cast(GpBitmap *)(nativeImage),
  435. x, y,
  436. &argb));
  437. if (status == Status.Ok) {
  438. color.SetValue(argb);
  439. }
  440. return status;
  441. }
  442. Status SetPixel(in INT x, in INT y, in Color color) {
  443. return SetStatus(GdipBitmapSetPixel(
  444. cast(GpBitmap *)(nativeImage),
  445. x, y,
  446. color.GetValue()));
  447. }
  448. /*
  449. Status ConvertFormat(PixelFormat format, DitherType dithertype, PaletteType palettetype, ColorPalette *palette, REAL alphaThresholdPercent) {
  450. }
  451. // The palette must be allocated and count must be set to the number of
  452. // entries in the palette. If there are not enough, the API will fail.
  453. static Status InitializePalette(
  454. in ColorPalette *palette, // Palette to initialize.
  455. PaletteType palettetype, // palette enumeration type.
  456. INT optimalColors, // how many optimal colors
  457. BOOL useTransparentColor, // add a transparent color to the palette.
  458. Bitmap *bitmap // optional bitmap for median cut.
  459. ) {
  460. }
  461. Status ApplyEffect(Effect *effect, RECT* ROI) {
  462. }
  463. static Status ApplyEffect(
  464. in Bitmap **inputs,
  465. in INT numInputs,
  466. in Effect *effect,
  467. in RECT *ROI, // optional parameter.
  468. RECT *outputRect, // optional parameter.
  469. Bitmap **output
  470. ) {
  471. }
  472. Status GetHistogram(
  473. in HistogramFormat format,
  474. in UINT NumberOfEntries,
  475. UINT *channel0,
  476. UINT *channel1,
  477. UINT *channel2,
  478. UINT *channel3
  479. ) {
  480. }
  481. static Status GetHistogramSize(in HistogramFormat format, UINT *NumberOfEntries) {
  482. }
  483. */
  484. Status SetResolution(in REAL xdpi, in REAL ydpi) {
  485. return SetStatus(GdipBitmapSetResolution(
  486. cast(GpBitmap *)(nativeImage),
  487. xdpi, ydpi));
  488. }
  489. this(in IDirectDrawSurface7* surface) {
  490. GpBitmap *bitmap = null;
  491. lastResult = GdipCreateBitmapFromDirectDrawSurface(surface,
  492. &bitmap);
  493. SetNativeImage(bitmap);
  494. }
  495. this(in BITMAPINFO* gdiBitmapInfo, in VOID* gdiBitmapData) {
  496. GpBitmap *bitmap = null;
  497. lastResult = GdipCreateBitmapFromGdiDib(gdiBitmapInfo,
  498. gdiBitmapData,
  499. &bitmap);
  500. SetNativeImage(bitmap);
  501. }
  502. this(in HBITMAP hbm, in HPALETTE hpal) {
  503. GpBitmap *bitmap = null;
  504. lastResult = GdipCreateBitmapFromHBITMAP(hbm, hpal, &bitmap);
  505. SetNativeImage(bitmap);
  506. }
  507. this(in HICON hicon) {
  508. GpBitmap *bitmap = null;
  509. lastResult = GdipCreateBitmapFromHICON(hicon, &bitmap);
  510. SetNativeImage(bitmap);
  511. }
  512. this(in HINSTANCE hInstance, in WCHAR * bitmapName) {
  513. GpBitmap *bitmap = null;
  514. lastResult = GdipCreateBitmapFromResource(hInstance,
  515. bitmapName,
  516. &bitmap);
  517. SetNativeImage(bitmap);
  518. }
  519. static Bitmap FromDirectDrawSurface7(in IDirectDrawSurface7* surface) {
  520. return new Bitmap(surface);
  521. }
  522. static Bitmap FromBITMAPINFO(in BITMAPINFO* gdiBitmapInfo, in VOID* gdiBitmapData) {
  523. return new Bitmap(gdiBitmapInfo, gdiBitmapData);
  524. }
  525. static Bitmap FromHBITMAP(in HBITMAP hbm, in HPALETTE hpal) {
  526. return new Bitmap(hbm, hpal);
  527. }
  528. static Bitmap FromHICON(in HICON hicon) {
  529. return new Bitmap(hicon);
  530. }
  531. static Bitmap FromResource(in HINSTANCE hInstance, in WCHAR * bitmapName) {
  532. return new Bitmap(hInstance, bitmapName);
  533. }
  534. Status GetHBITMAP(in Color colorBackground, HBITMAP *hbmReturn) {
  535. return SetStatus(GdipCreateHBITMAPFromBitmap(
  536. cast(GpBitmap*)(nativeImage),
  537. hbmReturn,
  538. colorBackground.GetValue()));
  539. }
  540. Status GetHICON(HICON *hicon) {
  541. return SetStatus(GdipCreateHICONFromBitmap(
  542. cast(GpBitmap*)(nativeImage),
  543. hicon));
  544. }
  545. protected:
  546. package this(GpBitmap *nativeBitmap) {
  547. lastResult = Status.Ok;
  548. SetNativeImage(nativeBitmap);
  549. }
  550. }