File: Image.hs

package info (click to toggle)
haskell-sdl-image 0.6.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 216 kB
  • sloc: haskell: 187; makefile: 3
file content (250 lines) | stat: -rw-r--r-- 6,511 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
-----------------------------------------------------------------------------
-- |
-- Module      :  Graphics.UI.SDL.Image
-- Copyright   :  (c) David Himmelstrup 2005
-- License     :  BSD-like
--
-- Maintainer  :  lemmih@gmail.com
-- Stability   :  provisional
-- Portability :  portable
--
-----------------------------------------------------------------------------
module Graphics.UI.SDL.Image
    ( load
    , loadRW
    , ImageType(..)
    , loadTypedRW
    , loadTyped
    , isTypedRW
    , isTyped
    , isBMPRW
    , isBMP
    , isPNMRW
    , isPNM
    , isXPMRW
    , isXPM
    , isXCFRW
    , isXCF
    , isPCXRW
    , isPCX
    , isGIFRW
    , isGIF
    , isJPGRW
    , isJPG
    , isTIFRW
    , isTIF
    , isPNGRW
    , isPNG
    , isLBMRW
    , isLBM
    , module Graphics.UI.SDL.Image.Version
    ) where

import Graphics.UI.SDL.Image.Version

import Foreign
import Foreign.C

import Graphics.UI.SDL.Video
import Graphics.UI.SDL.General
import Graphics.UI.SDL.Types
import Graphics.UI.SDL.RWOps as RW

finalizeWhenNotNull :: String -> Ptr SurfaceStruct -> IO Surface
finalizeWhenNotNull errMsg image
    = if image == nullPtr
         then failWithError errMsg
         else mkFinalizedSurface image

-- SDL_Surface *IMG_Load(const char *file)
foreign import ccall unsafe "IMG_Load" imgLoad :: CString -> IO (Ptr SurfaceStruct)
load :: FilePath -> IO Surface
load filepath
    = withCString filepath $ \cPath ->
      imgLoad cPath >>= finalizeWhenNotNull "IMG_Load"

-- SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
foreign import ccall "IMG_Load_RW" imgLoadRW :: Ptr RWopsStruct -> Int -> IO (Ptr SurfaceStruct)
loadRW :: RWops -> Bool -> IO Surface
loadRW rw freesrc
    = withForeignPtr rw $ \rwPtr ->
      imgLoadRW rwPtr (fromBool freesrc) >>= finalizeWhenNotNull "IMG_Load_RW"



data ImageType
    = TGA
    | BMP
    | PNM
    | XPM
    | XCF
    | PCX
    | GIF
    | JPG
    | TIF
    | LBM
    | PNG
    | Other String

imageTypeToString :: ImageType -> String
imageTypeToString TGA = "TGA"
imageTypeToString BMP = "BMP"
imageTypeToString PNM = "PNM"
imageTypeToString XPM = "XPM"
imageTypeToString XCF = "XCF"
imageTypeToString PCX = "PCX"
imageTypeToString GIF = "GIF"
imageTypeToString JPG = "JPG"
imageTypeToString TIF = "TIF"
imageTypeToString LBM = "LBM"
imageTypeToString PNG = "PNG"
imageTypeToString (Other format) = format

-- SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
foreign import ccall "IMG_LoadTyped_RW" imgLoadTypedRW :: Ptr RWopsStruct -> Int -> CString -> IO (Ptr SurfaceStruct)
loadTypedRW :: RWops -> Bool -> ImageType -> IO Surface
loadTypedRW rw freesrc imageType
    = withForeignPtr rw $ \rwPtr ->
      withCString (imageTypeToString imageType) $ \cType ->
      imgLoadTypedRW rwPtr (fromBool freesrc) cType >>= finalizeWhenNotNull "IMG_LoadTyped_RW"

loadTyped :: FilePath -> ImageType -> IO Surface
loadTyped filepath imageType
    = RW.with filepath "rb" $ \rw ->
      loadTypedRW rw False imageType

isTypedRW :: ImageType -> RWops -> IO Bool
isTypedRW BMP = isBMPRW
isTypedRW PNM = isPNMRW
isTypedRW XPM = isXPMRW
isTypedRW XCF = isXCFRW
isTypedRW PCX = isPCXRW
isTypedRW GIF = isGIFRW
isTypedRW JPG = isJPGRW
isTypedRW TIF = isTIFRW
isTypedRW PNG = isPNGRW
isTypedRW LBM = isLBMRW
isTypedRW _other = const (return False)

isTyped :: ImageType -> FilePath -> IO Bool
isTyped imageType path
    = isTypedRW imageType =<< RW.fromFile path "rb"


-- int IMG_isBMP(SDL_RWops *src)
foreign import ccall unsafe "IMG_isBMP" imgIsBMP :: Ptr RWopsStruct -> IO Int
isBMPRW :: RWops -> IO Bool
isBMPRW rw
    = withForeignPtr rw $ \rwPtr ->
      fmap toBool (imgIsBMP rwPtr)

isBMP :: FilePath -> IO Bool
isBMP path = RW.with path "rb" $
             isBMPRW

-- int IMG_isPNM(SDL_RWops *src)
foreign import ccall unsafe "IMG_isPNM" imgIsPNM :: Ptr RWopsStruct -> IO Int
isPNMRW :: RWops -> IO Bool
isPNMRW rw
    = withForeignPtr rw $ \rwPtr ->
      fmap toBool (imgIsPNM rwPtr)

isPNM :: FilePath -> IO Bool
isPNM path = RW.with path "rb" $
             isPNMRW

-- int IMG_isXPM(SDL_RWops *src)
foreign import ccall unsafe "IMG_isXPM" imgIsXPM :: Ptr RWopsStruct -> IO Int
isXPMRW :: RWops -> IO Bool
isXPMRW rw
    = withForeignPtr rw $ \rwPtr ->
      fmap toBool (imgIsXPM rwPtr)

isXPM :: FilePath -> IO Bool
isXPM path = RW.with path "rb" $
             isXPMRW

-- int IMG_isXCF(SDL_RWops *src)
foreign import ccall unsafe "IMG_isXCF" imgIsXCF :: Ptr RWopsStruct -> IO Int
isXCFRW :: RWops -> IO Bool
isXCFRW rw
    = withForeignPtr rw $ \rwPtr ->
      fmap toBool (imgIsXCF rwPtr)

isXCF :: FilePath -> IO Bool
isXCF path = RW.with path "rb" $
             isXCFRW


-- int IMG_isPCX(SDL_RWops *src)
foreign import ccall unsafe "IMG_isPCX" imgIsPCX :: Ptr RWopsStruct -> IO Int
isPCXRW :: RWops -> IO Bool
isPCXRW rw
    = withForeignPtr rw $ \rwPtr ->
      fmap toBool (imgIsPCX rwPtr)

isPCX :: FilePath -> IO Bool
isPCX path = RW.with path "rb" $
             isPCXRW


-- int IMG_isGIF(SDL_RWops *src)
foreign import ccall unsafe "IMG_isGIF" imgIsGIF :: Ptr RWopsStruct -> IO Int
isGIFRW :: RWops -> IO Bool
isGIFRW rw
    = withForeignPtr rw $ \rwPtr ->
      fmap toBool (imgIsGIF rwPtr)

isGIF :: FilePath -> IO Bool
isGIF path = RW.with path "rb" $
             isGIFRW


-- int IMG_isJPG(SDL_RWops *src)
foreign import ccall unsafe "IMG_isJPG" imgIsJPG :: Ptr RWopsStruct -> IO Int
isJPGRW :: RWops -> IO Bool
isJPGRW rw
    = withForeignPtr rw $ \rwPtr ->
      fmap toBool (imgIsJPG rwPtr)

isJPG :: FilePath -> IO Bool
isJPG path = RW.with path "rb" $
             isJPGRW


-- int IMG_isTIF(SDL_RWops *src)
foreign import ccall unsafe "IMG_isTIF" imgIsTIF :: Ptr RWopsStruct -> IO Int
isTIFRW :: RWops -> IO Bool
isTIFRW rw
    = withForeignPtr rw $ \rwPtr ->
      fmap toBool (imgIsTIF rwPtr)

isTIF :: FilePath -> IO Bool
isTIF path = RW.with path "rb" $
             isTIFRW


-- int IMG_isPNG(SDL_RWops *src)
foreign import ccall unsafe "IMG_isPNG" imgIsPNG :: Ptr RWopsStruct -> IO Int
isPNGRW :: RWops -> IO Bool
isPNGRW rw
    = withForeignPtr rw $ \rwPtr ->
      fmap toBool (imgIsPNG rwPtr)

isPNG :: FilePath -> IO Bool
isPNG path = RW.with path "rb" $
             isPNGRW


-- int IMG_isLBM(SDL_RWops *src)
foreign import ccall unsafe "IMG_isLBM" imgIsLBM :: Ptr RWopsStruct -> IO Int
isLBMRW :: RWops -> IO Bool
isLBMRW rw
    = withForeignPtr rw $ \rwPtr ->
      fmap toBool (imgIsLBM rwPtr)

isLBM :: FilePath -> IO Bool
isLBM path = RW.with path "rb" $
             isLBMRW