image 0.2.0-alpha

a pure rust imaging library
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Types and methods for representing and manipulating pixels
use std::num;
use std::num::{ Float, Int, NumCast };
use std::default::Default;

///An enumeration over supported color types and their bit depths
#[deriving(PartialEq, Eq, Show, Clone)]
pub enum ColorType {
    ///Pixel is greyscale
    Grey(u8),

    ///Pixel contains R, G and B channels
    RGB(u8),

    ///Pixel is an index into a color palette
    Palette(u8),

    ///Pixel is greyscale with an alpha channel
    GreyA(u8),

    ///Pixel is RGB with an alpha channel
    RGBA(u8)
}

/// Trait that promises that the type implementing it
/// does not have any memory allocated on the heap or
/// needs any destructor to be run.
pub trait SafeToTransmute {}

/// Placeholder for primitives.
pub trait Primitive: Clone + Copy + Sub<Self, Self> + NumCast {
    /// The maximum value of primitive.
    fn max_value() -> Self;
}

impl Primitive for uint {
    fn max_value() -> uint { Int::max_value() }
}
impl Primitive for u8 {
    fn max_value() -> u8 { Int::max_value() }
}
impl Primitive for u16 {
    fn max_value() -> u16 { Int::max_value() }
}
impl Primitive for u32 {
    fn max_value() -> u32 { Int::max_value() }
}
impl Primitive for u64 {
    fn max_value() -> u64 { Int::max_value() }
}
impl Primitive for int {
    fn max_value() -> int { Int::max_value() }
}
impl Primitive for i8 {
    fn max_value() -> i8 { Int::max_value() }
}
impl Primitive for i16 {
    fn max_value() -> i16 { Int::max_value() }
}
impl Primitive for i32 {
    fn max_value() -> i32 { Int::max_value() }
}
impl Primitive for i64 {
    fn max_value() -> i64 { Int::max_value() }
}
impl Primitive for f32 {
    fn max_value() -> f32 { Float::max_value() }
}
impl Primitive for f64 {
    fn max_value() -> f64 { Float::max_value() }
}

///Returns the number of bits contained in a pixel of ColorType c
pub fn bits_per_pixel(c: ColorType) -> uint {
    match c {
        ColorType::Grey(n)    => n as uint,
        ColorType::RGB(n)     => 3 * n as uint,
        ColorType::Palette(n) => 3 * n as uint,
        ColorType::GreyA(n)   => 2 * n as uint,
        ColorType::RGBA(n)    => 4 * n as uint,
    }
}

///Returns the number of color channels that make up this pixel
pub fn num_components(c: ColorType) -> uint {
    match c {
        ColorType::Grey(_)    => 1,
        ColorType::RGB(_)     => 3,
        ColorType::Palette(_) => 3,
        ColorType::GreyA(_)   => 2,
        ColorType::RGBA(_)    => 4,
    }
}

///A type to hold a grayscale pixel
#[packed]
#[deriving(Default, PartialEq, Eq, Clone, Show, Copy)]
pub struct Luma<T>(pub T);

impl<T: Primitive> Luma<T> {
    /// Returns the channels of this pixel as a tuple
    pub fn channel(&self) -> T {
        match *self {
            Luma(l) => l
        }
    }
}

impl<T: Primitive> SafeToTransmute for Luma<T> {}

/// A type to hold a grayscale pixel with an alpha channel
#[packed]
#[deriving(Default, PartialEq, Eq, Clone, Show, Copy)]
pub struct LumaA<T>(pub T, pub T);

impl<T: Primitive> LumaA<T> {
    /// Returns the channels of this pixel as a tuple
    pub fn channels(&self) -> (T, T) {
        match *self {
            LumaA(l, a) => (l, a)
        }
    }

    /// Returns the alpha channel of this pixel
    pub fn alpha(&self) -> T {
        match *self {
            LumaA(_, a) => a
        }
    }
}

impl<T: Primitive> SafeToTransmute for LumaA<T> {}

/// A type to hold an RGB pixel
#[packed]
#[deriving(Default, PartialEq, Eq, Clone, Show, Copy)]
pub struct Rgb<T>(pub T, pub T, pub T);

impl<T: Primitive> Rgb<T> {
    /// Returns the channels of this pixel as a tuple
    pub fn channels(&self) -> (T, T, T) {
        match *self {
            Rgb(r, g, b) => (r, g, b)
        }
    }
}

impl<T: Primitive> SafeToTransmute for Rgb<T> {}

/// A type to hold an RGB pixel with an alpha channel
#[packed]
#[deriving(Default, PartialEq, Eq, Clone, Show, Copy)]
pub struct Rgba<T>(pub T, pub T, pub T, pub T);

impl<T: Primitive> Rgba<T> {
    /// Returns the channels of this pixel as a tuple
    pub fn channels(&self) -> (T, T, T, T) {
        match *self {
            Rgba(r, g, b, a) => (r, g, b, a)
        }
    }

    /// Returns the alpha channel of this pixel
    pub fn alpha(&self) -> T {
        match *self {
            Rgba(_, _, _, a) => a
        }
    }
}

impl<T: Primitive> SafeToTransmute for Rgba<T> {}

/// A trait that all pixels implement.
pub trait Pixel<T>: Copy + Clone + Default {
    /// Construct a pixel from the 4 channels a, b, c and d.
    /// If the pixel does not contain 4 channels the extra are ignored.
    fn from_channels(a: T, b: T, c: T, d: T) -> Self;

    /// Convert this pixel to RGB
    fn to_rgb(&self) -> Rgb<T>;

    /// Convert this pixel to RGB with an alpha channel
    fn to_rgba(&self) -> Rgba<T>;

    /// Convert this pixel to luma
    fn to_luma(&self) -> Luma<T>;

    /// Convert this pixel to luma with an alpha channel
    fn to_luma_alpha(&self) -> LumaA<T>;

    /// Invert this pixel
    fn invert(&mut self);

    /// Apply the function ```f``` to each channel of this pixel.
    fn map(&self, f: | T | -> T) -> Self;

    ///Apply the function f to each channel except the alpha channel.
    ///Apply the function g to the alpha channel.
    fn map_with_alpha(&self, f: |T| -> T, g: |T| -> T) -> Self;

    /// Apply the function ```f``` to each channel of this pixel and
    /// ```other``` pairwise.
    fn map2(&self, other: Self, f: | T, T | -> T) -> Self;

    /// Returns the channels of this pixel as a 4 tuple. If the pixel
    /// has less than 4 channels the remainder is filled with the maximum value
    fn channels4(&self) -> (T, T, T, T);
}

impl<T: Primitive + Default> Pixel<T> for Rgb<T> {
    fn from_channels(a: T, b: T, c: T, _: T) -> Rgb<T> {
        Rgb(a, b, c)
    }

    fn to_luma(&self) -> Luma<T> {
        let (r, g, b) = self.channels();

        let l = 0.2125f32 * r.to_f32().unwrap() +
                0.7154f32 * g.to_f32().unwrap() +
                0.0721f32 * b.to_f32().unwrap();

        Luma(num::cast::<f32, T>(l).unwrap())
    }

    fn to_luma_alpha(&self) -> LumaA<T> {
        let l = self.to_luma().channel();

        LumaA(l, Primitive::max_value())
    }

    fn to_rgb(&self) -> Rgb<T> {
        self.clone()
    }

    fn to_rgba(&self) -> Rgba<T> {
        let (r, g, b) = self.channels();

        Rgba(r, g, b, Primitive::max_value())
    }

    fn invert(&mut self) {
        let (r, g, b) = self.channels();

        let max: T = Primitive::max_value();

        let r1 = max - r;
        let g1 = max - g;
        let b1 = max - b;

        *self = Rgb(r1, g1, b1)
    }

    fn map(&self, f: | a: T | -> T) -> Rgb<T> {
        let (r, g, b) = self.channels();

        let r1 = f(r);
        let g1 = f(g);
        let b1 = f(b);

        Rgb(r1, g1, b1)
    }

    fn map_with_alpha(&self, f: |c: T| -> T, _: |a: T| -> T) -> Rgb<T> {
        self.map(f)
    }


    fn map2(&self, other: Rgb<T>, f: | a: T, b: T | -> T) -> Rgb<T> {
        let (r1, g1, b1) = self.channels();
        let (r2, g2, b2) = other.channels();

        let r3 = f(r1, r2);
        let g3 = f(g1, g2);
        let b3 = f(b1, b2);

        Rgb(r3, g3, b3)
    }

    fn channels4(&self) ->(T, T, T, T) {
        let (r, g, b) = self.channels();

        (r, g, b, Primitive::max_value())
    }
}

impl<T: Primitive + Default> Pixel<T> for Rgba<T> {
    fn from_channels(a: T, b: T, c: T, d: T) -> Rgba<T> {
        Rgba(a, b, c, d)
    }

    fn to_luma(&self) -> Luma<T> {
        self.to_rgb().to_luma()
    }

    fn to_luma_alpha(&self) -> LumaA<T> {
        let l = self.to_luma().channel();
        let a = self.alpha();

        LumaA(l, a)
    }

    fn to_rgb(&self) -> Rgb<T> {
        let (r, g, b, _) = self.channels();

        Rgb(r, g, b)
    }

    fn to_rgba(&self) -> Rgba<T> {
        self.clone()
    }

    fn invert(&mut self) {
        let (r, g, b) = self.to_rgb().channels();
        let a = self.alpha();

        let max: T = Primitive::max_value();

        *self = Rgba(max - r, max - g, max - b, a)
    }

    fn map(&self, f: | a: T | -> T) -> Rgba<T> {
        let (r, g, b, a) = self.channels();

        let r1 = f(r);
        let g1 = f(g);
        let b1 = f(b);
        let a1 = f(a);

        Rgba(r1, g1, b1, a1)
    }

    fn map_with_alpha(&self, f: |c: T| -> T, h: |b: T| -> T) -> Rgba<T> {
        let (r, g, b, a) = self.channels();

        let r1 = f(r);
        let g1 = f(g);
        let b1 = f(b);
        let a1 = h(a);

        Rgba(r1, g1, b1, a1)
    }

    fn map2(&self, other: Rgba<T>, f: | a: T, b: T | -> T) -> Rgba<T> {
        let (r1, g1, b1, a1) = self.channels();
        let (r2, g2, b2, _) = other.channels();

        let r3 = f(r1, r2);
        let g3 = f(g1, g2);
        let b3 = f(b1, b2);

        Rgba(r3, g3, b3, a1)
    }

    fn channels4(&self) ->(T, T, T, T) {
        let (r, g, b, a) = self.channels();

        (r, g, b, a)
    }
}

impl<T: Primitive + Default> Pixel<T> for Luma<T> {
    fn from_channels(a: T, _: T, _: T, _: T) -> Luma<T> {
        Luma(a)
    }

    fn to_luma(&self) -> Luma<T> {
        self.clone()
    }

    fn to_luma_alpha(&self) -> LumaA<T> {
        let l = self.channel();

        LumaA(l, Primitive::max_value())
    }

    fn to_rgb(&self) -> Rgb<T> {
        let l1 = self.channel();
        let l2 = self.channel();
        let l3 = self.channel();

        Rgb(l1, l2, l3)
    }

    fn to_rgba(&self) -> Rgba<T> {
        let (r, g, b) = self.to_rgb().channels();

        Rgba(r, g, b, Primitive::max_value())
    }

    fn invert(&mut self) {
        let max: T = Primitive::max_value();
        let l1 = max - self.channel();

        *self = Luma(l1)
    }

    fn map(&self, f: | a: T | -> T) -> Luma<T> {
        let l  = self.channel();
        let l1 = f(l);

        Luma(l1)
    }

    fn map_with_alpha(&self, f: |c: T| -> T, _: |a: T| -> T) -> Luma<T> {
        self.map(f)
    }

    fn map2(&self, other: Luma<T>, f: | a: T, b: T | -> T) -> Luma<T> {
        let l1 = self.channel();
        let l2 = other.channel();

        let l3 = f(l1, l2);

        Luma(l3)
    }

    fn channels4(&self) ->(T, T, T, T) {
        let l = self.channel();
        let max: T = Primitive::max_value();

        (l, max.clone(), max.clone(), max.clone())
    }
}

impl<T: Primitive + Default> Pixel<T> for LumaA<T> {
    fn from_channels(a: T, b: T, _: T, _: T) -> LumaA<T> {
        LumaA(a, b)
    }

    fn to_luma(&self) -> Luma<T> {
        let (l, _) = self.channels();
        Luma(l)
    }

    fn to_luma_alpha(&self) -> LumaA<T> {
        self.clone()
    }

    fn to_rgb(&self) -> Rgb<T> {
        let (l1, _) = self.channels();
        let (l2, _) = self.channels();
        let (l3, _) = self.channels();

        Rgb(l1, l2, l3)
    }

    fn to_rgba(&self) -> Rgba<T> {
        let (r, g, b) = self.to_rgb().channels();
        let a = self.alpha();

        Rgba(r, g, b, a)
    }

    fn invert(&mut self) {
        let l = self.to_luma().channel();
        let a  = self.alpha();
        let max: T = Primitive::max_value();

        *self = LumaA(max - l, a)
    }

    fn map(&self, f: | a: T | -> T) -> LumaA<T> {
        let (l, a) = self.channels();

        let l1 = f(l);
        let a1 = f(a);

        LumaA(l1, a1)
    }

    fn map_with_alpha(&self, f: |c: T| -> T, g: |b: T| -> T) -> LumaA<T> {
        let (l, a) = self.channels();

        let l1 = f(l);
        let a1 = g(a);

        LumaA(l1, a1)
    }

    fn map2(&self, other: LumaA<T>, f: | a: T, b: T | -> T) -> LumaA<T> {
        let (l1, a1) = self.channels();
        let (l2, _)  = other.channels();

        let l3 = f(l1, l2);

        LumaA(l3, a1)
    }

    fn channels4(&self) ->(T, T, T, T) {
        let (l, a) = self.channels();
        let max: T = Primitive::max_value();

        (l, a, max.clone(), max.clone())
    }
}