source: libpipi/trunk/pipi-sharp/Picture.cs @ 3340

Last change on this file since 3340 was 3340, checked in by Sam Hocevar, 14 years ago

Rename pipi_getpixel into pipi_get_pixels.

File size: 3.8 KB
Line 
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
15using System;
16using System.Runtime.InteropServices;
17using System.Security;
18
19namespace Pipi
20{
21    public class Picture
22    {
23        private IntPtr _picture;
24        private string _filename;
25
26        public string FileName
27        {
28            get { return _filename; }
29        }
30
31        private Picture(IntPtr p)
32        {
33            _picture = p;
34        }
35
36        [DllImport("libpipi-0.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-0.dll", CallingConvention=CallingConvention.Cdecl),
45         SuppressUnmanagedCodeSecurity]
46        private static extern IntPtr pipi_load(string s);
47        public static Picture Load(string s)
48        {
49            IntPtr p = pipi_load(s);
50            if(p == IntPtr.Zero)
51                return null;
52
53            Picture ret = new Picture(p);
54            ret._filename = s;
55            return ret;
56        }
57
58        [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
59         SuppressUnmanagedCodeSecurity]
60        private static extern int pipi_save(IntPtr p, string s);
61        public int Save(string s)
62        {
63            return pipi_save(_picture, s);
64        }
65
66        [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
67         SuppressUnmanagedCodeSecurity]
68        private static extern int pipi_get_image_width(IntPtr img);
69        public int Width
70        {
71            get { return pipi_get_image_width(_picture); }
72        }
73
74        [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
75         SuppressUnmanagedCodeSecurity]
76        private static extern int pipi_get_image_height(IntPtr img);
77        public int Height
78        {
79            get { return pipi_get_image_height(_picture); }
80        }
81
82        [StructLayout(LayoutKind.Sequential)]
83        public struct PixelsStruct
84        {
85            public IntPtr pixels;
86            public Int32 w, h, pitch, bpp;
87            public Int64 bytes;
88        }
89
90        [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
91         SuppressUnmanagedCodeSecurity]
92        private static extern IntPtr pipi_get_pixels(IntPtr img, int type);
93        public byte[] GetPixels(int w, int h, int x, int y)
94        {
95            byte[] array = new byte[w * h * 4];
96            IntPtr pixels = pipi_get_pixels(_picture, 0);
97            PixelsStruct p;
98            Int64 address;
99
100            p = (PixelsStruct)Marshal.PtrToStructure(pixels,
101                                                     typeof(PixelsStruct));
102            address = p.pixels.ToInt64();
103
104            unsafe
105            {
106                for(int j = 0; j < h; j++)
107                {
108                    Marshal.Copy((IntPtr)(address + ((j + y) * p.w + x) * 4),
109                                 array, j * w * 4, w * 4);
110                    for(int i = 0; i < w; i++)
111                    {
112                        byte c = array[j * w * 4 + i * 4];
113                        array[j * w * 4 + i * 4] = array[j * w * 4 + i * 4 + 2];
114                        array[j * w * 4 + i * 4 + 2] = c;
115                    }
116                }
117            }
118
119            return array;
120        }
121    }
122}
123
Note: See TracBrowser for help on using the repository browser.