Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

110 строки
3.4 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. public readonly string FileName;
  23. [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl),
  24. SuppressUnmanagedCodeSecurity]
  25. private static extern IntPtr pipi_load(string s);
  26. public Picture(string s)
  27. {
  28. _picture = pipi_load(s);
  29. FileName = s;
  30. }
  31. [DllImport("libpipi.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.dll", CallingConvention=CallingConvention.Cdecl),
  39. SuppressUnmanagedCodeSecurity]
  40. private static extern int pipi_save(IntPtr p, string s);
  41. public int Save(string s)
  42. {
  43. return pipi_save(_picture, s);
  44. }
  45. [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl),
  46. SuppressUnmanagedCodeSecurity]
  47. private static extern int pipi_get_image_width(IntPtr img);
  48. public int Width
  49. {
  50. get { return pipi_get_image_width(_picture); }
  51. }
  52. [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl),
  53. SuppressUnmanagedCodeSecurity]
  54. private static extern int pipi_get_image_height(IntPtr img);
  55. public int Height
  56. {
  57. get { return pipi_get_image_height(_picture); }
  58. }
  59. [StructLayout(LayoutKind.Sequential)]
  60. public struct PixelsStruct
  61. {
  62. public IntPtr pixels;
  63. public Int32 w, h, pitch, bpp;
  64. public Int64 bytes;
  65. }
  66. [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl),
  67. SuppressUnmanagedCodeSecurity]
  68. private static extern IntPtr pipi_getpixels(IntPtr img, int type);
  69. public byte[] GetPixels(int w, int h, int x, int y)
  70. {
  71. byte[] array = new byte[w * h * 4];
  72. IntPtr pixels = pipi_getpixels(_picture, 0);
  73. PixelsStruct p;
  74. Int64 address;
  75. p = (PixelsStruct)Marshal.PtrToStructure(pixels,
  76. typeof(PixelsStruct));
  77. address = p.pixels.ToInt64();
  78. unsafe
  79. {
  80. for(int j = 0; j < h; j++)
  81. {
  82. Marshal.Copy((IntPtr)(address + ((j + y) * p.w + x) * 4),
  83. array, j * w * 4, w * 4);
  84. for(int i = 0; i < w; i++)
  85. {
  86. byte c = array[j * w * 4 + i * 4];
  87. array[j * w * 4 + i * 4] = array[j * w * 4 + i * 4 + 2];
  88. array[j * w * 4 + i * 4 + 2] = c;
  89. }
  90. }
  91. }
  92. return array;
  93. }
  94. }
  95. }