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
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/

//! This create gathers some functionality related to images and access to them.

use graphics::egl_tools::HwImage;
use graphics::attributes::{EglAttributes, DmabufAttributes};

use defs::Size;

// -------------------------------------------------------------------------------------------------

/// Format of a pixel.
#[derive(Clone, Copy, Debug)]
pub enum PixelFormat {
    XRGB8888,
    ARGB8888,
    XBGR8888,
    ABGR8888,
}

// -------------------------------------------------------------------------------------------------

impl PixelFormat {
    /// Returns size in bytes of pixel encoded in given format.
    pub fn get_size(&self) -> usize {
        match *self {
            PixelFormat::XBGR8888 => 3,
            PixelFormat::ABGR8888 => 4,
            PixelFormat::XRGB8888 => 3,
            PixelFormat::ARGB8888 => 4,
        }
    }
}

// -------------------------------------------------------------------------------------------------

/// Trait providing interface for image storing objects.
pub trait Image {
    /// Get width and height of the image.
    fn get_size(&self) -> Size;

    /// Return width of the image.
    fn get_width(&self) -> usize;

    /// Returns height of the image.
    fn get_height(&self) -> usize;
}

// -------------------------------------------------------------------------------------------------

/// Trait providing interface for pixmap storing objects.
pub trait Pixmap: Image {
    /// Returns pixel format of the pixmap.
    fn get_format(&self) -> PixelFormat;

    /// Return stride (width in bytes or one row) of the pixmap.
    fn get_stride(&self) -> usize;

    /// Returns data as slice.
    fn as_slice(&self) -> &[u8];

    /// Returns data as mutable slice.
    fn as_mut_slice(&mut self) -> &mut [u8];

    /// Returns data as pointer to `u8`.
    unsafe fn as_ptr(&self) -> *const u8;
}

// -------------------------------------------------------------------------------------------------

impl Image for HwImage {
    /// Returns width and height of the image.
    fn get_size(&self) -> Size {
        Size::new(self.get_width(), self.get_height())
    }

    /// Returns width of the image.
    fn get_width(&self) -> usize {
        self.get_width()
    }

    /// Returns height of the image.
    fn get_height(&self) -> usize {
        self.get_height()
    }
}

// -------------------------------------------------------------------------------------------------

impl Image for EglAttributes {
    /// Get width and height of the underlying hardware image.
    fn get_size(&self) -> Size {
        Size::new(self.width as usize, self.height as usize)
    }

    /// Return width of the underlying hardware image.
    fn get_width(&self) -> usize {
        self.width as usize
    }

    /// Returns height of the underlying hardware image.
    fn get_height(&self) -> usize {
        self.height as usize
    }
}

// -------------------------------------------------------------------------------------------------

impl Image for DmabufAttributes {
    /// Get width and height of the underlying hardware image.
    fn get_size(&self) -> Size {
        Size::new(self.width as usize, self.height as usize)
    }

    /// Return width of the underlying hardware image.
    fn get_width(&self) -> usize {
        self.width as usize
    }

    /// Returns height of the underlying hardware image.
    fn get_height(&self) -> usize {
        self.height as usize
    }
}

// -------------------------------------------------------------------------------------------------