PageRenderTime 57ms CodeModel.GetById 7ms app.highlight 1ms RepoModel.GetById 48ms app.codeStats 0ms

/freeimage/Bitmap.ooc

http://github.com/tsion/ooc-freeimage
Unknown | 161 lines | 129 code | 32 blank | 0 comment | 0 complexity | 91ef6c8447c4e084955289f42c476f35 MD5 | raw file
  1use freeimage
  2import io/File
  3import freeimage/[ImageFormat, IOHandler]
  4
  5Bitmap: cover from FIBITMAP* {
  6    new: static func (width, height, bpp: Int) -> This {
  7        FreeImage_Allocate(width, height, bpp, 0, 0, 0)
  8    }
  9
 10    new: static func ~fromPath (path: String) -> This {
 11        fif := ImageFormat unknown
 12        fif = FreeImage_GetFileType(path, 0)
 13
 14        if (fif == ImageFormat unknown) {
 15            fif = FreeImage_GetFIFFromFilename(path)
 16        }
 17
 18        if ((fif != ImageFormat unknown) && FreeImage_FIFSupportsReading(fif)) {
 19            bitmap := FreeImage_Load(fif, path, 0)
 20            return bitmap
 21        }
 22
 23        return null
 24    }
 25
 26    new: static func ~fromFile (file: File) -> This {
 27        This new(file path)
 28    }
 29
 30    new: static func ~fromHandle (io: IOHandler*, handle: FStream) -> This {
 31        fif := ImageFormat unknown
 32        fif = FreeImage_GetFileTypeFromHandle(io, handle, 0)
 33
 34        if ((fif != ImageFormat unknown) && FreeImage_FIFSupportsReading(fif)) {
 35            bitmap := FreeImage_LoadFromHandle(fif, io, handle, 0)
 36            return bitmap
 37        }
 38
 39        return null
 40    }
 41
 42    clone: extern(FreeImage_Clone) func -> This
 43
 44    save: func (filename: String, format: ImageFormat) -> Bool {
 45        FreeImage_Save(format, this, filename, 0)
 46    }
 47
 48    unload: extern(FreeImage_Unload) func
 49
 50    width: Int {
 51        get: extern(FreeImage_GetWidth)
 52    }
 53
 54    height: Int {
 55        get: extern(FreeImage_GetHeight)
 56    }
 57
 58    bpp: Int {
 59        get: extern(FreeImage_GetBPP)
 60    }
 61
 62    imageType: extern(FreeImage_GetImageType) func -> ImageType
 63
 64    // Pixel access functions
 65    bits: extern(FreeImage_GetBits) func -> UInt8*
 66
 67    scanline: extern(FreeImage_GetScanLine) func (Int) -> UInt8*
 68
 69    getPixelIndex: func (x, y: UInt) -> UInt8 {
 70        index: UInt8
 71        FreeImage_GetPixelIndex(this, x, y, index&)
 72        return index
 73    }
 74
 75    getPixelColor: func (x, y: UInt) -> RGBQuad {
 76        color := RGBQuad new()
 77        FreeImage_GetPixelColor(this, x, y, color)
 78        return color
 79    }
 80
 81    setPixelIndex: func (x, y: UInt, index: UInt8) {
 82        FreeImage_SetPixelIndex(this, x, y, index&)
 83    }
 84
 85    setPixelColor: func (x, y: UInt, color: RGBQuad) {
 86        FreeImage_SetPixelColor(this, x, y, color)
 87    }
 88
 89    // Toolkit functions
 90    rescale: extern(FreeImage_Rescale) func (width, height: Int, filter: ImageFilter) -> This
 91
 92    thumbnail: extern(FreeImage_MakeThumbnail) func (max: Int, convert: Bool) -> This
 93
 94    thumbnail: func ~defaultConvert (max: Int) -> This {
 95        thumbnail(max, true)
 96    }
 97}
 98
 99FreeImage_Allocate: extern func (Int, Int, Int, Int, Int, Int) -> Bitmap
100FreeImage_Load: extern func (ImageFormat, String, Int) -> Bitmap
101FreeImage_LoadFromHandle: extern func (ImageFormat, IOHandler*, FStream, Int) -> Bitmap
102FreeImage_Save: extern func (ImageFormat, Bitmap, String, Int) -> Bool
103FreeImage_GetFileType: extern func (String, Int) -> ImageFormat
104FreeImage_GetFileTypeFromHandle: extern func (IOHandler*, FStream, Int) -> ImageFormat
105FreeImage_GetFIFFromFilename: extern func (String) -> ImageFormat
106FreeImage_FIFSupportsReading: extern func (ImageFormat) -> Bool
107FreeImage_GetPixelIndex: extern func (Bitmap, UInt, UInt, UInt8*) -> Bool
108FreeImage_GetPixelColor: extern func (Bitmap, UInt, UInt, RGBQuad*) -> Bool
109FreeImage_SetPixelIndex: extern func (Bitmap, UInt, UInt, UInt8*) -> Bool
110FreeImage_SetPixelColor: extern func (Bitmap, UInt, UInt, RGBQuad*) -> Bool
111
112RGBQuadStruct: cover from RGBQUAD {
113    red:      extern(rgbRed)      UInt8
114    green:    extern(rgbGreen)    UInt8
115    blue:     extern(rgbBlue)     UInt8
116    reserved: extern(rgbReserved) UInt8
117}
118
119RGBQuad: cover from RGBQuadStruct* {
120    new: static func (red, green, blue, reserved: UInt8) -> This {
121        this := gc_malloc(RGBQuadStruct size) as This
122        this@ red = red
123        this@ green = green
124        this@ blue = blue
125        this@ reserved = reserved
126        return this
127    }
128
129    new: static func ~withoutAlpha (red, green, blue: UInt8) -> This {
130        This new(red, green, blue, 255)
131    }
132
133    new: static func ~blank -> This {
134        This new(0, 0, 0, 0)
135    }
136}
137
138ImageType: extern(FREE_IMAGE_TYPE) enum {
139    unknown: extern(FIT_UNKNOWN)
140    bitmap:  extern(FIT_BITMAP)
141    uint16:  extern(FIT_UINT16)
142    int16:   extern(FIT_INT16)
143    uint32:  extern(FIT_UINT32)
144    int32:   extern(FIT_INT32)
145    float:   extern(FIT_FLOAT)
146    double:  extern(FIT_DOUBLE)
147    complex: extern(FIT_COMPLEX)
148    rgb16:   extern(FIT_RGB16)
149    rgba16:  extern(FIT_RGBA16)
150    rgbf:    extern(FIT_RGBF)
151    rgbaf:   extern(FIT_RGBAF)
152}
153
154ImageFilter: extern(FREE_IMAGE_FILTER) enum {
155    box:        extern(FILTER_BOX)
156    bicubic:    extern(FILTER_BICUBIC)
157    bilinear:   extern(FILTER_BILINEAR)
158    bspline:    extern(FILTER_BSPLINE)
159    catmullrom: extern(FILTER_CATMULLROM)
160    lanczos3:   extern(FILTER_LANCZOS3)
161}