| 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 | |
|---|
| 15 | using System; |
|---|
| 16 | using System.Runtime.InteropServices; |
|---|
| 17 | using System.Security; |
|---|
| 18 | |
|---|
| 19 | namespace Pipi |
|---|
| 20 | { |
|---|
| 21 | public class Picture |
|---|
| 22 | { |
|---|
| 23 | private IntPtr _picture; |
|---|
| 24 | |
|---|
| 25 | public readonly string FileName; |
|---|
| 26 | |
|---|
| 27 | [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl), |
|---|
| 28 | SuppressUnmanagedCodeSecurity] |
|---|
| 29 | private static extern IntPtr pipi_load(string s); |
|---|
| 30 | public Picture(string s) |
|---|
| 31 | { |
|---|
| 32 | _picture = pipi_load(s); |
|---|
| 33 | FileName = s; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl), |
|---|
| 37 | SuppressUnmanagedCodeSecurity] |
|---|
| 38 | private static extern int pipi_free(IntPtr img); |
|---|
| 39 | ~Picture() |
|---|
| 40 | { |
|---|
| 41 | pipi_free(_picture); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl), |
|---|
| 45 | SuppressUnmanagedCodeSecurity] |
|---|
| 46 | private static extern int pipi_save(IntPtr p, string s); |
|---|
| 47 | public int Save(string s) |
|---|
| 48 | { |
|---|
| 49 | return pipi_save(_picture, s); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl), |
|---|
| 53 | SuppressUnmanagedCodeSecurity] |
|---|
| 54 | private static extern int pipi_get_image_width(IntPtr img); |
|---|
| 55 | public int Width |
|---|
| 56 | { |
|---|
| 57 | get { return pipi_get_image_width(_picture); } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl), |
|---|
| 61 | SuppressUnmanagedCodeSecurity] |
|---|
| 62 | private static extern int pipi_get_image_height(IntPtr img); |
|---|
| 63 | public int Height |
|---|
| 64 | { |
|---|
| 65 | get { return pipi_get_image_height(_picture); } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | [StructLayout(LayoutKind.Sequential)] |
|---|
| 69 | public struct PixelsStruct |
|---|
| 70 | { |
|---|
| 71 | public IntPtr pixels; |
|---|
| 72 | public Int32 w, h, pitch, bpp; |
|---|
| 73 | public Int64 bytes; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | [DllImport("libpipi.dll", CallingConvention=CallingConvention.Cdecl), |
|---|
| 77 | SuppressUnmanagedCodeSecurity] |
|---|
| 78 | private static extern IntPtr pipi_getpixels(IntPtr img, int type); |
|---|
| 79 | public byte[] GetPixels(int w, int h, int x, int y) |
|---|
| 80 | { |
|---|
| 81 | byte[] array = new byte[w * h * 4]; |
|---|
| 82 | IntPtr pixels = pipi_getpixels(_picture, 0); |
|---|
| 83 | PixelsStruct p; |
|---|
| 84 | Int64 address; |
|---|
| 85 | |
|---|
| 86 | p = (PixelsStruct)Marshal.PtrToStructure(pixels, |
|---|
| 87 | typeof(PixelsStruct)); |
|---|
| 88 | address = p.pixels.ToInt64(); |
|---|
| 89 | |
|---|
| 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 | |
|---|
| 105 | return array; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|