ggstd 0.1.0

Partial implementation of Go standard library
Documentation
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Copyright 2023 The rust-ggstd authors. All rights reserved.
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/// Color can convert itself to alpha-premultiplied 16-bits per channel RGBA.
/// The conversion may be lossy.
pub trait ColorTrait {
    /// rgba returns the alpha-premultiplied red, green, blue and alpha values
    /// for the color. Each value ranges within [0, 0xffff], but is represented
    /// by a u32 so that multiplying by a blend factor up to 0xffff will not
    /// overflow.
    ///
    /// An alpha-premultiplied color component c has been scaled by alpha (a),
    /// so has valid values 0 <= c <= a.
    fn rgba(&self) -> (u32, u32, u32, u32); // (r, g, b, a)
}

/// Color represents a color in different color models.
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Color {
    /// RGBA is a traditional 32-bit alpha-premultiplied color, having 8
    /// bits for each of red, green, blue and alpha.
    RGBA(RGBA),
    /// NRGBA represents a non-alpha-premultiplied 32-bit color.
    NRGBA(NRGBA),
    /// RGBA64 represents a 64-bit alpha-premultiplied color, having 16 bits for
    /// each of red, green, blue and alpha.
    RGBA64(RGBA64),
    /// NRGBA64 represents a non-alpha-premultiplied 64-bit color,
    /// having 16 bits for each of red, green, blue and alpha.
    NRGBA64(NRGBA64),
    /// Gray represents an 8-bit grayscale color.
    Gray(Gray),
    /// Gray16 represents a 16-bit grayscale color.
    Gray16(Gray16),
    /// Alpha represents an 8-bit alpha color.
    Alpha(Alpha),
    /// Alpha16 represents a 16-bit alpha color.
    Alpha16(Alpha16),
}

impl Color {
    /// new_gray creates a new Gray color.
    pub const fn new_gray(y: u8) -> Self {
        Color::Gray(Gray::new(y))
    }
    /// new_rgba creates a new RGBA color.
    pub const fn new_rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
        Color::RGBA(RGBA::new(r, g, b, a))
    }
    /// new_rgba64 creates a new RGBA64 color.
    pub const fn new_rgba64(r: u16, g: u16, b: u16, a: u16) -> Self {
        Color::RGBA64(RGBA64::new(r, g, b, a))
    }
    /// new_nrgba creates a new NRGBA color.
    pub const fn new_nrgba(r: u8, g: u8, b: u8, a: u8) -> Self {
        Color::NRGBA(NRGBA::new(r, g, b, a))
    }
    /// new_nrgba64 creates a new NRGBA64 color.
    pub const fn new_nrgba64(r: u16, g: u16, b: u16, a: u16) -> Self {
        Color::NRGBA64(NRGBA64::new(r, g, b, a))
    }
    /// to_gray16 converts color to Gray16
    pub fn to_gray16(&self) -> Gray16 {
        match self {
            Color::Gray16(c) => *c,
            _ => Gray16::new_from(self),
        }
    }
    /// to_nrgba converts color to NRGBA64
    pub fn to_nrgba(&self) -> NRGBA {
        match self {
            Color::NRGBA(c) => *c,
            _ => NRGBA::new_from(self),
        }
    }
    /// to_nrgba64 converts color to NRGBA64
    pub fn to_nrgba64(&self) -> NRGBA64 {
        match self {
            Color::NRGBA64(c) => *c,
            _ => NRGBA64::new_from(self),
        }
    }
}

impl ColorTrait for Color {
    fn rgba(&self) -> (u32, u32, u32, u32) {
        match self {
            Color::RGBA(c) => c.rgba(),
            Color::NRGBA(c) => c.rgba(),
            Color::Gray(c) => c.rgba(),
            Color::Gray16(c) => c.rgba(),
            Color::RGBA64(c) => c.rgba(),
            Color::NRGBA64(c) => c.rgba(),
            Color::Alpha(c) => c.rgba(),
            Color::Alpha16(c) => c.rgba(),
        }
    }
}

/// RGBA represents a traditional 32-bit alpha-premultiplied color, having 8
/// bits for each of red, green, blue and alpha.
///
/// An alpha-premultiplied color component C has been scaled by alpha (A), so
/// has valid values 0 <= C <= A.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct RGBA {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

impl RGBA {
    pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self { r, g, b, a }
    }
    pub fn new_from(c: &Color) -> Self {
        if let Color::RGBA(c) = c {
            *c
        } else {
            let (r, g, b, a) = c.rgba();
            RGBA::new(
                (r >> 8) as u8,
                (g >> 8) as u8,
                (b >> 8) as u8,
                (a >> 8) as u8,
            )
        }
    }
}

impl ColorTrait for RGBA {
    fn rgba(&self) -> (u32, u32, u32, u32) {
        let mut r = self.r as u32;
        r |= r << 8;
        let mut g = self.g as u32;
        g |= g << 8;
        let mut b = self.b as u32;
        b |= b << 8;
        let mut a = self.a as u32;
        a |= a << 8;
        (r, g, b, a)
    }
}

/// RGBA64 represents a 64-bit alpha-premultiplied color, having 16 bits for
/// each of red, green, blue and alpha.
///
/// An alpha-premultiplied color component C has been scaled by alpha (A), so
/// has valid values 0 <= C <= A.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct RGBA64 {
    pub r: u16,
    pub g: u16,
    pub b: u16,
    pub a: u16,
}

impl RGBA64 {
    pub const fn new(r: u16, g: u16, b: u16, a: u16) -> Self {
        Self { r, g, b, a }
    }
    pub fn new_from(c: &Color) -> Self {
        match c {
            Color::RGBA64(c) => Self::new(c.r, c.g, c.b, c.a),
            _ => {
                let (r, g, b, a) = c.rgba();
                RGBA64::new(r as u16, g as u16, b as u16, a as u16)
            }
        }
    }
}

impl ColorTrait for RGBA64 {
    fn rgba(&self) -> (u32, u32, u32, u32) {
        (self.r as u32, self.g as u32, self.b as u32, self.a as u32)
    }
}

/// NRGBA represents a non-alpha-premultiplied 32-bit color.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct NRGBA {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

impl NRGBA {
    pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self { r, g, b, a }
    }
    pub fn new_from(c: &Color) -> Self {
        match c {
            Color::NRGBA(c) => Self::new(c.r, c.g, c.b, c.a),
            _ => {
                let (r, g, b, a) = c.rgba();
                if a == 0xffff {
                    return Self::new((r >> 8) as u8, (g >> 8) as u8, (b >> 8) as u8, 0xff);
                }
                if a == 0 {
                    return Self::new(0, 0, 0, 0);
                }
                // Since Color.RGBA returns an alpha-premultiplied color, we should have r <= a && g <= a && b <= a.
                let r = (r * 0xffff) / a;
                let g = (g * 0xffff) / a;
                let b = (b * 0xffff) / a;
                Self::new(
                    (r >> 8) as u8,
                    (g >> 8) as u8,
                    (b >> 8) as u8,
                    (a >> 8) as u8,
                )
            }
        }
    }
}

impl ColorTrait for NRGBA {
    fn rgba(&self) -> (u32, u32, u32, u32) {
        let mut r = self.r as u32;
        r |= r << 8;
        r *= self.a as u32;
        r /= 0xff;
        let mut g = self.g as u32;
        g |= g << 8;
        g *= self.a as u32;
        g /= 0xff;
        let mut b = self.b as u32;
        b |= b << 8;
        b *= self.a as u32;
        b /= 0xff;
        let mut a = self.a as u32;
        a |= a << 8;
        (r, g, b, a)
    }
}

/// NRGBA64 represents a non-alpha-premultiplied 64-bit color,
/// having 16 bits for each of red, green, blue and alpha.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct NRGBA64 {
    pub r: u16,
    pub g: u16,
    pub b: u16,
    pub a: u16,
}

impl NRGBA64 {
    pub const fn new(r: u16, g: u16, b: u16, a: u16) -> Self {
        Self { r, g, b, a }
    }
    pub fn new_from(c: &Color) -> Self {
        match c {
            Color::NRGBA64(c) => Self::new(c.r, c.g, c.b, c.a),
            _ => {
                let (r, g, b, a) = c.rgba();
                if a == 0xffff {
                    return NRGBA64::new(r as u16, g as u16, b as u16, 0xffff);
                }
                if a == 0 {
                    return NRGBA64::new(0, 0, 0, 0);
                }
                // Since Color.RGBA returns an alpha-premultiplied color, we should have r <= a && g <= a && b <= a.
                let r = (r * 0xffff) / a;
                let g = (g * 0xffff) / a;
                let b = (b * 0xffff) / a;
                NRGBA64::new(r as u16, g as u16, b as u16, a as u16)
            }
        }
    }
}

impl ColorTrait for NRGBA64 {
    fn rgba(&self) -> (u32, u32, u32, u32) {
        let mut r = self.r as u32;
        r *= self.a as u32;
        r /= 0xffff;
        let mut g = self.g as u32;
        g *= self.a as u32;
        g /= 0xffff;
        let mut b = self.b as u32;
        b *= self.a as u32;
        b /= 0xffff;
        let a = self.a as u32;
        (r, g, b, a)
    }
}

/// Alpha represents an 8-bit alpha color.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Alpha {
    pub a: u8,
}

impl Alpha {
    pub const fn new(a: u8) -> Self {
        Self { a }
    }
    pub fn new_from(c: &Color) -> Self {
        match c {
            Color::Alpha(c) => Self::new(c.a),
            _ => {
                let (_, _, _, a) = c.rgba();
                Alpha::new((a >> 8) as u8)
            }
        }
    }
}

impl ColorTrait for Alpha {
    fn rgba(&self) -> (u32, u32, u32, u32) {
        let mut a = self.a as u32;
        a |= a << 8;
        (a, a, a, a)
    }
}

/// Alpha16 represents a 16-bit alpha color.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Alpha16 {
    pub a: u16,
}

impl Alpha16 {
    pub const fn new(a: u16) -> Self {
        Self { a }
    }
    pub fn new_from(c: &Color) -> Self {
        match c {
            Color::Alpha16(c) => Self::new(c.a),
            _ => {
                let (_, _, _, a) = c.rgba();
                Alpha16::new(a as u16)
            }
        }
    }
}

impl ColorTrait for Alpha16 {
    fn rgba(&self) -> (u32, u32, u32, u32) {
        let a = self.a as u32;
        (a, a, a, a)
    }
}

/// Gray represents an 8-bit grayscale color.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Gray {
    pub y: u8,
}

impl Gray {
    pub const fn new(y: u8) -> Self {
        Self { y }
    }
    pub fn new_from(c: &Color) -> Self {
        match c {
            Color::Gray(c) => Self::new(c.y),
            _ => {
                let (r, g, b, _) = c.rgba();

                // These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
                // as those given by the JFIF specification and used by fn RGBToYCbCr in
                // ycbcr.go.
                //
                // Note that 19595 + 38470 + 7471 equals 65536.
                //
                // The 24 is 16 + 8. The 16 is the same as used in RGBToYCbCr. The 8 is
                // because the return value is 8 bit color, not 16 bit color.
                let y = (19595 * r + 38470 * g + 7471 * b + (1 << 15)) >> 24;

                Gray::new(y as u8)
            }
        }
    }
}

impl ColorTrait for Gray {
    fn rgba(&self) -> (u32, u32, u32, u32) {
        let mut y = self.y as u32;
        y |= y << 8;
        (y, y, y, 0xffff)
    }
}

/// Gray16 represents a 16-bit grayscale color.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Gray16 {
    pub y: u16,
}

impl Gray16 {
    pub const fn new(y: u16) -> Self {
        Self { y }
    }
    pub fn new_from(c: &Color) -> Self {
        match c {
            Color::Gray16(c) => Self::new(c.y),
            _ => {
                let (r, g, b, _) = c.rgba();
                // These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
                // as those given by the JFIF specification and used by fn RGBToYCbCr in
                // ycbcr.go.
                //
                // Note that 19595 + 38470 + 7471 equals 65536.
                let y = (19595 * r + 38470 * g + 7471 * b + (1 << 15)) >> 16;
                Gray16::new(y as u16)
            }
        }
    }
}

impl ColorTrait for Gray16 {
    fn rgba(&self) -> (u32, u32, u32, u32) {
        let y = self.y as u32;
        (y, y, y, 0xffff)
    }
}

// /// Model can convert any Color to one from its own color model. The conversion
// /// may be lossy.
// pub trait Model {
//     fn Convert(c: dyn Color) -> dyn Color;
// }

// // ModelFunc returns a Model that invokes f to implement the conversion.
// fn ModelFunc(f fn(Color) Color) Model {
// 	// Note: using *modelFunc as the implementation
// 	// means that callers can still use comparisons
// 	// like m == RGBAModel. This is not possible if
// 	// we use the fn value directly, because funcs
// 	// are no longer comparable.
// 	return &modelFunc{f}
// }

// type modelFunc struct {
// 	f fn(Color) Color
// }

// fn (m *modelFunc) Convert(c Color) Color {
// 	return m.f(c)
// }

/// Models for the standard color types.
/// Model can convert any Color to one from its own color model. The conversion
/// may be lossy.
#[derive(Debug, PartialEq)]
pub enum Model {
    RGBAModel,
    RGBA64Model,
    NRGBAModel,
    NRGBA64Model,
    AlphaModel,
    Alpha16Model,
    GrayModel,
    Gray16Model,
    Paletted(Palette),
}

impl Model {
    /// convert any Color to one from its own color model. The conversion
    /// may be lossy.
    pub fn convert(&self, c: &Color) -> Color {
        match self {
            Model::RGBAModel => Color::RGBA(RGBA::new_from(c)),
            Model::NRGBAModel => Color::NRGBA(NRGBA::new_from(c)),
            Model::RGBA64Model => Color::RGBA64(RGBA64::new_from(c)),
            Model::NRGBA64Model => Color::NRGBA64(NRGBA64::new_from(c)),
            Model::GrayModel => Color::Gray(Gray::new_from(c)),
            Model::Gray16Model => Color::Gray16(Gray16::new_from(c)),
            Model::AlphaModel => Color::Alpha(Alpha::new_from(c)),
            Model::Alpha16Model => Color::Alpha16(Alpha16::new_from(c)),
            Model::Paletted(pal) => pal.convert(c),
        }
    }
    pub fn bitdepth(&self) -> usize {
        match self {
            Model::RGBAModel => 8,
            Model::RGBA64Model => 16,
            Model::NRGBAModel => 8,
            Model::NRGBA64Model => 16,
            Model::AlphaModel => 8,
            Model::Alpha16Model => 16,
            Model::GrayModel => 8,
            Model::Gray16Model => 16,
            Model::Paletted(pal) => match pal.colors.len() {
                0..=2 => 1,
                3..=4 => 2,
                5..=16 => 4,
                _ => 8,
            },
        }
    }
}

/// Palette is a palette of colors.
/// The number of colors is usually in the range of 1..=256.
#[derive(Debug, PartialEq, Clone)]
pub struct Palette {
    pub colors: Vec<Color>,
}

impl Palette {
    pub fn new(num_colors: usize) -> Self {
        Self {
            colors: vec![OPAQUE_BLACK; num_colors],
        }
    }

    /// Convert returns the palette color closest to c in Euclidean R,G,B space.
    pub fn convert(&self, c: &Color) -> Color {
        if self.colors.is_empty() {
            return Color::new_gray(0);
        }
        self.colors[self.index(c)]
    }

    /// index returns the index of the palette color closest to c in Euclidean
    /// R,G,B,A space.
    pub fn index(&self, c: &Color) -> usize {
        // A batch version of this computation is in image/draw/draw.go.
        let (cr, cg, cb, ca) = c.rgba();
        let (mut ret, mut best_sum) = (0, 0xffffffff);
        for (i, v) in self.colors.iter().enumerate() {
            let (vr, vg, vb, va) = v.rgba();
            let sum = sq_diff(cr, vr) + sq_diff(cg, vg) + sq_diff(cb, vb) + sq_diff(ca, va);
            if sum < best_sum {
                if sum == 0 {
                    return i;
                }
                (ret, best_sum) = (i, sum);
            }
        }
        ret
    }
}

/// sq_diff returns the squared-difference of x and y, shifted by 2 so that
/// adding four of those won't overflow a u32.
///
/// x and y are both assumed to be in the range [0, 0xffff].
pub(super) fn sq_diff(x: u32, y: u32) -> u32 {
    // The canonical code of this function looks as follows:
    //
    //	var d u32
    //	if x > y {
    //		d = x - y
    //	} else {
    //		d = y - x
    //	}
    //	return (d * d) >> 2
    //
    // Language spec guarantees the following properties of unsigned integer
    // values operations with respect to overflow/wrap around:
    //
    // > For unsigned integer values, the operations +, -, *, and << are
    // > computed modulo 2n, where n is the bit width of the unsigned
    // > integer's type. Loosely speaking, these unsigned integer operations
    // > discard high bits upon overflow, and programs may rely on ``wrap
    // > around''.
    //
    // Considering these properties and the fact that this function is
    // called in the hot paths (x,y loops), it is reduced to the below code
    // which is slightly faster. See TestSqDiff for correctness check.
    let d = x.wrapping_sub(y);
    d.wrapping_mul(d) >> 2
}

// Standard colors.
pub const BLACK: Color = Color::Gray16(Gray16::new(0));
pub const WHITE: Color = Color::Gray16(Gray16::new(0xffff));
pub const TRANSPARENT: Color = Color::Alpha16(Alpha16::new(0));
pub const OPAQUE: Color = Color::Alpha16(Alpha16::new(0xffff));
pub const OPAQUE_BLACK: Color = Color::new_rgba(0x00, 0x00, 0x00, 0xff);