25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

124 satır
3.8 KiB

  1. //
  2. // libpipi Pathetic image processing interface library
  3. // Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. // All Rights Reserved
  5. //
  6. // $Id$
  7. //
  8. // This library is free software. It comes without any warranty, to
  9. // the extent permitted by applicable law. You can redistribute it
  10. // and/or modify it under the terms of the Do What The Fuck You Want
  11. // To Public License, Version 2, as published by Sam Hocevar. See
  12. // http://sam.zoy.org/wtfpl/COPYING for more details.
  13. //
  14. using System;
  15. using System.Runtime.InteropServices;
  16. using System.Security;
  17. namespace Pipi
  18. {
  19. public class Picture
  20. {
  21. private IntPtr _picture;
  22. private string _filename;
  23. public string FileName
  24. {
  25. get { return _filename; }
  26. }
  27. private Picture(IntPtr p)
  28. {
  29. _picture = p;
  30. }
  31. [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
  32. SuppressUnmanagedCodeSecurity]
  33. private static extern int pipi_free(IntPtr img);
  34. ~Picture()
  35. {
  36. pipi_free(_picture);
  37. }
  38. [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
  39. SuppressUnmanagedCodeSecurity]
  40. private static extern IntPtr pipi_load(string s);
  41. public static Picture Load(string s)
  42. {
  43. IntPtr p = pipi_load(s);
  44. if(p == IntPtr.Zero)
  45. return null;
  46. Picture ret = new Picture(p);
  47. ret._filename = s;
  48. return ret;
  49. }
  50. [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
  51. SuppressUnmanagedCodeSecurity]
  52. private static extern int pipi_save(IntPtr p, string s);
  53. public int Save(string s)
  54. {
  55. return pipi_save(_picture, s);
  56. }
  57. [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
  58. SuppressUnmanagedCodeSecurity]
  59. private static extern int pipi_get_image_width(IntPtr img);
  60. public int Width
  61. {
  62. get { return pipi_get_image_width(_picture); }
  63. }
  64. [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
  65. SuppressUnmanagedCodeSecurity]
  66. private static extern int pipi_get_image_height(IntPtr img);
  67. public int Height
  68. {
  69. get { return pipi_get_image_height(_picture); }
  70. }
  71. [StructLayout(LayoutKind.Sequential)]
  72. public struct PixelsStruct
  73. {
  74. public IntPtr pixels;
  75. public Int32 w, h, pitch, bpp;
  76. public Int64 bytes;
  77. }
  78. [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
  79. SuppressUnmanagedCodeSecurity]
  80. private static extern IntPtr pipi_getpixels(IntPtr img, int type);
  81. public byte[] GetPixels(int w, int h, int x, int y)
  82. {
  83. byte[] array = new byte[w * h * 4];
  84. IntPtr pixels = pipi_getpixels(_picture, 0);
  85. PixelsStruct p;
  86. Int64 address;
  87. p = (PixelsStruct)Marshal.PtrToStructure(pixels,
  88. typeof(PixelsStruct));
  89. address = p.pixels.ToInt64();
  90. unsafe
  91. {
  92. for(int j = 0; j < h; j++)
  93. {
  94. Marshal.Copy((IntPtr)(address + ((j + y) * p.w + x) * 4),
  95. array, j * w * 4, w * 4);
  96. for(int i = 0; i < w; i++)
  97. {
  98. byte c = array[j * w * 4 + i * 4];
  99. array[j * w * 4 + i * 4] = array[j * w * 4 + i * 4 + 2];
  100. array[j * w * 4 + i * 4 + 2] = c;
  101. }
  102. }
  103. }
  104. return array;
  105. }
  106. }
  107. }