~cosme/ubuntu/precise/freeimage/freeimage-3.15.1

« back to all changes in this revision

Viewing changes to Wrapper/FreeImage.NET/cs/Library/Classes/FreeImageStreamIO.cs

  • Committer: Stefano Rivera
  • Date: 2010-07-24 15:35:51 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: stefanor@ubuntu.com-20100724153551-6s3fth1653huk31a
Tags: upstream-3.13.1
ImportĀ upstreamĀ versionĀ 3.31.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ==========================================================
 
2
// FreeImage 3 .NET wrapper
 
3
// Original FreeImage 3 functions and .NET compatible derived functions
 
4
//
 
5
// Design and implementation by
 
6
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
 
7
// - Carsten Klein (cklein05@users.sourceforge.net)
 
8
//
 
9
// Contributors:
 
10
// - David Boland (davidboland@vodafone.ie)
 
11
//
 
12
// Main reference : MSDN Knowlede Base
 
13
//
 
14
// This file is part of FreeImage 3
 
15
//
 
16
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
 
17
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
 
18
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
 
19
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
 
20
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
 
21
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
 
22
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
 
23
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
 
24
// THIS DISCLAIMER.
 
25
//
 
26
// Use at your own risk!
 
27
// ==========================================================
 
28
 
 
29
// ==========================================================
 
30
// CVS
 
31
// $Revision: 1.5 $
 
32
// $Date: 2009/09/15 11:47:46 $
 
33
// $Id: FreeImageStreamIO.cs,v 1.5 2009/09/15 11:47:46 cklein05 Exp $
 
34
// ==========================================================
 
35
 
 
36
using System;
 
37
using System.IO;
 
38
using System.Runtime.InteropServices;
 
39
using System.Diagnostics;
 
40
 
 
41
namespace FreeImageAPI.IO
 
42
{
 
43
        /// <summary>
 
44
        /// Internal class wrapping stream io functions.
 
45
        /// </summary>
 
46
        /// <remarks>
 
47
        /// FreeImage can read files from a disk or a network drive but also allows the user to
 
48
        /// implement their own loading or saving functions to load them directly from an ftp or web
 
49
        /// server for example.
 
50
        /// <para/>
 
51
        /// In .NET streams are a common way to handle data. The <b>FreeImageStreamIO</b> class handles
 
52
        /// the loading and saving from and to streams. It implements the funtions FreeImage needs
 
53
        /// to load data from an an arbitrary source.
 
54
        /// <para/>
 
55
        /// The class is for internal use only.
 
56
        /// </remarks>
 
57
        internal static class FreeImageStreamIO
 
58
        {
 
59
                /// <summary>
 
60
                /// <see cref="FreeImageAPI.IO.FreeImageIO"/> structure that can be used to read from streams via
 
61
                /// <see cref="FreeImageAPI.FreeImage.LoadFromHandle(FREE_IMAGE_FORMAT, ref FreeImageIO, fi_handle, FREE_IMAGE_LOAD_FLAGS)"/>.
 
62
                /// </summary>
 
63
                public static readonly FreeImageIO io;
 
64
 
 
65
                /// <summary>
 
66
                /// Initializes a new instances which can be used to
 
67
                /// create a FreeImage compatible <see cref="FreeImageAPI.IO.FreeImageIO"/> structure.
 
68
                /// </summary>
 
69
                static FreeImageStreamIO()
 
70
                {
 
71
                        io.readProc = new ReadProc(streamRead);
 
72
                        io.writeProc = new WriteProc(streamWrite);
 
73
                        io.seekProc = new SeekProc(streamSeek);
 
74
                        io.tellProc = new TellProc(streamTell);
 
75
                }
 
76
 
 
77
                /// <summary>
 
78
                /// Reads the requested data from the stream and writes it to the given address.
 
79
                /// </summary>
 
80
                static unsafe uint streamRead(IntPtr buffer, uint size, uint count, fi_handle handle)
 
81
                {
 
82
                        Stream stream = handle.GetObject() as Stream;
 
83
                        if ((stream == null) || (!stream.CanRead))
 
84
                        {
 
85
                                return 0;
 
86
                        }
 
87
                        uint readCount = 0;
 
88
                        byte* ptr = (byte*)buffer;
 
89
                        byte[] bufferTemp = new byte[size];
 
90
                        int read;
 
91
                        while (readCount < count)
 
92
                        {
 
93
                                read = stream.Read(bufferTemp, 0, (int)size);
 
94
                                if (read != (int)size)
 
95
                                {
 
96
                                        stream.Seek(-read, SeekOrigin.Current);
 
97
                                        break;
 
98
                                }
 
99
                                for (int i = 0; i < read; i++, ptr++)
 
100
                                {
 
101
                                        *ptr = bufferTemp[i];
 
102
                                }
 
103
                                readCount++;
 
104
                        }
 
105
                        return (uint)readCount;
 
106
                }
 
107
 
 
108
                /// <summary>
 
109
                /// Reads the given data and writes it into the stream.
 
110
                /// </summary>
 
111
                static unsafe uint streamWrite(IntPtr buffer, uint size, uint count, fi_handle handle)
 
112
                {
 
113
                        Stream stream = handle.GetObject() as Stream;
 
114
                        if ((stream == null) || (!stream.CanWrite))
 
115
                        {
 
116
                                return 0;
 
117
                        }
 
118
                        uint writeCount = 0;
 
119
                        byte[] bufferTemp = new byte[size];
 
120
                        byte* ptr = (byte*)buffer;
 
121
                        while (writeCount < count)
 
122
                        {
 
123
                                for (int i = 0; i < size; i++, ptr++)
 
124
                                {
 
125
                                        bufferTemp[i] = *ptr;
 
126
                                }
 
127
                                try
 
128
                                {
 
129
                                        stream.Write(bufferTemp, 0, bufferTemp.Length);
 
130
                                }
 
131
                                catch
 
132
                                {
 
133
                                        return writeCount;
 
134
                                }
 
135
                                writeCount++;
 
136
                        }
 
137
                        return writeCount;
 
138
                }
 
139
 
 
140
                /// <summary>
 
141
                /// Moves the streams position.
 
142
                /// </summary>
 
143
                static int streamSeek(fi_handle handle, int offset, SeekOrigin origin)
 
144
                {
 
145
                        Stream stream = handle.GetObject() as Stream;
 
146
                        if (stream == null)
 
147
                        {
 
148
                                return 1;
 
149
                        }
 
150
                        stream.Seek((long)offset, origin);
 
151
                        return 0;
 
152
                }
 
153
 
 
154
                /// <summary>
 
155
                /// Returns the streams current position
 
156
                /// </summary>
 
157
                static int streamTell(fi_handle handle)
 
158
                {
 
159
                        Stream stream = handle.GetObject() as Stream;
 
160
                        if (stream == null)
 
161
                        {
 
162
                                return -1;
 
163
                        }
 
164
                        return (int)stream.Position;
 
165
                }
 
166
        }
 
167
}
 
 
b'\\ No newline at end of file'