No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_get_pixels(IntPtr img, int type);
  81. [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
  82. SuppressUnmanagedCodeSecurity]
  83. private static extern void pipi_release_pixels(IntPtr img, IntPtr p);
  84. public byte[] GetPixels(int w, int h, int x, int y)
  85. {
  86. byte[] array = new byte[w * h * 4];
  87. IntPtr pixels = pipi_get_pixels(_picture, 0);
  88. PixelsStruct p;
  89. Int64 address;
  90. p = (PixelsStruct)Marshal.PtrToStructure(pixels,
  91. typeof(PixelsStruct));
  92. address = p.pixels.ToInt64();
  93. unsafe
  94. {
  95. for(int j = 0; j < h; j++)
  96. {
  97. Marshal.Copy((IntPtr)(address + ((j + y) * p.w + x) * 4),
  98. array, j * w * 4, w * 4);
  99. for(int i = 0; i < w; i++)
  100. {
  101. byte c = array[j * w * 4 + i * 4];
  102. array[j * w * 4 + i * 4] = array[j * w * 4 + i * 4 + 2];
  103. array[j * w * 4 + i * 4 + 2] = c;
  104. }
  105. }
  106. }
  107. pipi_release_pixels(_picture, pixels);
  108. return array;
  109. }
  110. }
  111. }