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

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

Add pipi_release_pixels and pipi_set_colorspace in prevision of
pipi_get_pixels using the tiles system. These functions should
disappear one day, because they're going to leak like crazy.

File size: 4.0 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        [DllImport("libpipi-0.dll", CallingConvention=CallingConvention.Cdecl),
94         SuppressUnmanagedCodeSecurity]
95        private static extern void pipi_release_pixels(IntPtr img, IntPtr p);
96        public byte[] GetPixels(int w, int h, int x, int y)
97        {
98            byte[] array = new byte[w * h * 4];
99            IntPtr pixels = pipi_get_pixels(_picture, 0);
100            PixelsStruct p;
101            Int64 address;
102
103            p = (PixelsStruct)Marshal.PtrToStructure(pixels,
104                                                     typeof(PixelsStruct));
105            address = p.pixels.ToInt64();
106
107            unsafe
108            {
109                for(int j = 0; j < h; j++)
110                {
111                    Marshal.Copy((IntPtr)(address + ((j + y) * p.w + x) * 4),
112                                 array, j * w * 4, w * 4);
113                    for(int i = 0; i < w; i++)
114                    {
115                        byte c = array[j * w * 4 + i * 4];
116                        array[j * w * 4 + i * 4] = array[j * w * 4 + i * 4 + 2];
117                        array[j * w * 4 + i * 4 + 2] = c;
118                    }
119                }
120            }
121
122            pipi_release_pixels(_picture, pixels);
123
124            return array;
125        }
126    }
127}
128
Note: See TracBrowser for help on using the repository browser.