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
//! Library for reading and writing PCX image format.
//!
//! PCX is quite old format, it is not recommended to use it for new applications.
//!
//! PCX does not contain any color space information. Today one will usually interpret it as containing colors in [sRGB](https://en.wikipedia.org/wiki/sRGB) color space.
//!
//! Example for reading PCX image:
//!
//!     let mut reader = pcx::Reader::from_file("test-data/marbles.pcx").unwrap();
//!     println!("width = {}, height = {}, paletted = {}", reader.width(), reader.height(), reader.is_paletted());
//!     for y in 0..reader.height() {
//!         if reader.is_paletted() {
//!             // call reader.next_row_paletted(...) to read next row
//!         } else {
//!             // call reader.next_row_rgb(...) or reader.next_row_rgb_separate(...) to read next row
//!         }
//!     }
//!
//! Example for writing PCX image:
//!
//!     // Create 5x5 RGB file.
//!     let mut writer = pcx::WriterRgb::create_file("test.pcx", (5, 5), (300, 300)).unwrap();
//!     for y in 0..5 {
//!         // Write 5 green pixels.
//!         writer.write_row(&[0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0]);
//!     }
//!     writer.finish().unwrap();
//!
//! This library does not implement its own error type, instead it uses `std::io::Error`. In the case of invalid
//! PCX file it will return error with `.kind() == ErrorKind::InvalidData`.

// References:
// https://github.com/FFmpeg/FFmpeg/blob/415f907ce8dcca87c9e7cfdc954b92df399d3d80/libavcodec/pcx.c
// http://www.fileformat.info/format/pcx/egff.htm
// http://www.fileformat.info/format/pcx/spec/index.htm

extern crate byteorder;
#[cfg(test)]
extern crate walkdir;
#[cfg(test)]
extern crate image;

use std::io;

pub use reader::Reader;
pub use writer::{WriterRgb, WriterPaletted};

pub mod low_level;
mod reader;
mod writer;

#[cfg(test)]
mod test_samples;

// Error caused by incorrect use of the API.
fn user_error<T>(error: &str) -> io::Result<T> {
    Err(io::Error::new(io::ErrorKind::InvalidInput, error))
}

#[cfg(test)]
mod tests {
    use std::iter;
    use {Reader, WriterRgb, WriterPaletted};

    fn round_trip_rgb_separate(width: u16, height: u16) {
        let mut pcx = Vec::new();

        {
            let mut writer = WriterRgb::new(&mut pcx, (width, height), (300, 300)).unwrap();

            let r: Vec<u8> = iter::repeat(88).take(width as usize).collect();
            let g: Vec<u8> = (0..width).map(|v| (v & 0xFF) as u8).collect();
            let mut b: Vec<u8> = iter::repeat(88).take(width as usize).collect();
            for y in 0..height {
                for x in 0..width {
                    b[x as usize] = (y & 0xFF) as u8;
                }

                writer.write_row_from_separate(&r, &g, &b).unwrap();
            }
            writer.finish().unwrap();
        }

        let mut reader = Reader::new(&pcx[..]).unwrap();
        assert_eq!(reader.dimensions(), (width, height));
        assert_eq!(reader.is_paletted(), false);
        assert_eq!(reader.palette_length(), None);

        let mut r: Vec<u8> = iter::repeat(0).take(width as usize).collect();
        let mut g: Vec<u8> = iter::repeat(0).take(width as usize).collect();
        let mut b: Vec<u8> = iter::repeat(0).take(width as usize).collect();

        for y in 0..height {
            reader.next_row_rgb_separate(&mut r, &mut g, &mut b).unwrap();

            for x in 0..width {
                assert_eq!(r[x as usize], 88);
                assert_eq!(g[x as usize], (x & 0xFF) as u8);
                assert_eq!(b[x as usize], (y & 0xFF) as u8);
            }
        }
    }

    fn round_trip_rgb_interleaved(width: u16, height: u16) {
        let mut pcx = Vec::new();

        let written_rgb: Vec<u8> = (0..(width as usize) * 3).map(|v| (v & 0xFF) as u8).collect();
        {
            let mut writer = WriterRgb::new(&mut pcx, (width, height), (300, 300)).unwrap();

            for _ in 0..height {
                writer.write_row(&written_rgb).unwrap();
            }
            writer.finish().unwrap();
        }

        let mut reader = Reader::new(&pcx[..]).unwrap();
        assert_eq!(reader.dimensions(), (width, height));
        assert_eq!(reader.is_paletted(), false);
        assert_eq!(reader.palette_length(), None);

        let mut read_rgb: Vec<u8> = iter::repeat(0).take((width as usize) * 3).collect();

        for _ in 0..height {
            reader.next_row_rgb(&mut read_rgb).unwrap();
            assert_eq!(written_rgb, read_rgb);
        }
    }

    fn round_trip_paletted(width: u16, height: u16) {
        let mut pcx = Vec::new();

        let palette: Vec<u8> = (0..256 * 3).map(|v| (v % 0xFF) as u8).collect();
        {
            let mut writer = WriterPaletted::new(&mut pcx, (width, height), (300, 300)).unwrap();

            let mut p: Vec<u8> = iter::repeat(88).take(width as usize).collect();
            for y in 0..height {
                for x in 0..width {
                    p[x as usize] = (y & 0xFF) as u8;
                }

                writer.write_row(&p).unwrap();
            }

            writer.write_palette(&palette).unwrap();
        }

        let mut reader = Reader::new(&pcx[..]).unwrap();
        assert_eq!(reader.dimensions(), (width, height));
        assert!(reader.is_paletted());
        assert_eq!(reader.palette_length(), Some(256));

        let mut p: Vec<u8> = iter::repeat(0).take(width as usize).collect();

        for y in 0..height {
            reader.next_row_paletted(&mut p).unwrap();

            for x in 0..width {
                assert_eq!(p[x as usize], (y & 0xFF) as u8);
            }
        }

        let mut palette_read = [0; 3 * 256];
        reader.read_palette(&mut palette_read).unwrap();
        assert_eq!(&palette[..], &palette_read[..]);
    }

    #[test]
    fn small_round_trip() {
        for width in 1..40 {
            for height in 1..40 {
                round_trip_rgb_separate(width, height);
                round_trip_rgb_interleaved(width, height);
                round_trip_paletted(width, height);
            }
        }
    }

    #[test]
    fn large_round_trip_rgb() {
        round_trip_rgb_separate(0xFFFF - 1, 1);
        round_trip_rgb_separate(1, 0xFFFF);
        round_trip_rgb_interleaved(0xFFFF - 1, 1);
        round_trip_rgb_interleaved(1, 0xFFFF);
    }

    #[test]
    fn large_round_trip_paletted() {
        round_trip_paletted(0xFFFF - 1, 1);
        round_trip_paletted(1, 0xFFFF);
    }
}