|
6 | 6 |
|
7 | 7 | namespace Pinta.Core; |
8 | 8 |
|
9 | | -public sealed class NetpbmPortablePixmap : IImageExporter |
| 9 | +public sealed class NetpbmPortablePixmap : IImageExporter, IImageImporter |
10 | 10 | { |
| 11 | + private const string MAGIC_SEQUENCE = "P3"; // Magic sequence for text-based portable pixmap format |
| 12 | + |
| 13 | + public Document Import (Gio.File file) |
| 14 | + { |
| 15 | + using GioStream stream = new GioStream (file.Read (cancellable: null)); |
| 16 | + using StreamReader reader = new (stream, Encoding.ASCII); |
| 17 | + |
| 18 | + PpmTokenReader tokenizer = new (reader); |
| 19 | + |
| 20 | + string magic = tokenizer.ReadToken (); |
| 21 | + if (magic != MAGIC_SEQUENCE) |
| 22 | + throw new FormatException ($"Expected '{MAGIC_SEQUENCE}' magic sequence, got '{magic}'"); |
| 23 | + |
| 24 | + int width = tokenizer.ReadPositiveInt ("Image width"); |
| 25 | + int height = tokenizer.ReadPositiveInt ("Image height"); |
| 26 | + int maxValue = tokenizer.ReadPositiveInt ("Max color value"); |
| 27 | + |
| 28 | + Size imageSize = new (width, height); |
| 29 | + |
| 30 | + Document newDocument = new ( |
| 31 | + PintaCore.Actions, |
| 32 | + PintaCore.Tools, |
| 33 | + PintaCore.Workspace, |
| 34 | + imageSize, |
| 35 | + file, |
| 36 | + "ppm"); |
| 37 | + |
| 38 | + Layer layer = newDocument.Layers.AddNewLayer (file.GetDisplayName ()); |
| 39 | + Span<ColorBgra> pixelData = layer.Surface.GetPixelData (); |
| 40 | + |
| 41 | + layer.Surface.Flush (); |
| 42 | + |
| 43 | + int pixelCount = width * height; |
| 44 | + if (maxValue == 255) { |
| 45 | + for (int i = 0; i < pixelCount; i++) |
| 46 | + pixelData[i] = FastReadPixel (ref tokenizer); |
| 47 | + } else { |
| 48 | + for (int i = 0; i < pixelCount; i++) |
| 49 | + pixelData[i] = ReadPixel (ref tokenizer, maxValue); |
| 50 | + } |
| 51 | + |
| 52 | + layer.Surface.MarkDirty (); |
| 53 | + |
| 54 | + return newDocument; |
| 55 | + |
| 56 | + static ColorBgra FastReadPixel (ref PpmTokenReader tokens) |
| 57 | + { |
| 58 | + int r = tokens.ReadColorComponent (255); |
| 59 | + int g = tokens.ReadColorComponent (255); |
| 60 | + int b = tokens.ReadColorComponent (255); |
| 61 | + return ColorBgra.FromBgra ( |
| 62 | + b: (byte) b, |
| 63 | + g: (byte) g, |
| 64 | + r: (byte) r, |
| 65 | + a: byte.MaxValue); |
| 66 | + } |
| 67 | + |
| 68 | + static ColorBgra ReadPixel (ref PpmTokenReader tokens, int max) |
| 69 | + { |
| 70 | + int r = tokens.ReadColorComponent (max); |
| 71 | + int g = tokens.ReadColorComponent (max); |
| 72 | + int b = tokens.ReadColorComponent (max); |
| 73 | + return ColorBgra.FromBgra ( |
| 74 | + b: ScaleToByteRange (b, max), |
| 75 | + g: ScaleToByteRange (g, max), |
| 76 | + r: ScaleToByteRange (r, max), |
| 77 | + a: byte.MaxValue); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static byte ScaleToByteRange (int value, int maxValue) |
| 82 | + { |
| 83 | + int rounded = (int) (value * 255.0 / maxValue); |
| 84 | + int clamped = Math.Clamp (rounded, byte.MinValue, byte.MaxValue); |
| 85 | + return (byte) clamped; |
| 86 | + } |
| 87 | + |
11 | 88 | public void Export (ImageSurface flattenedImage, Stream outputStream) |
12 | 89 | { |
13 | 90 | using StreamWriter writer = new (outputStream, Encoding.ASCII) { |
14 | | - NewLine = "\n", // Always use LF endings to generate the same output on every platform and simplify unit tests |
| 91 | + NewLine = "\n", // Same output, including line endings, on every platform |
15 | 92 | }; |
| 93 | + |
16 | 94 | Size imageSize = flattenedImage.GetSize (); |
17 | 95 | ReadOnlySpan<ColorBgra> pixelData = flattenedImage.GetReadOnlyPixelData (); |
18 | | - writer.WriteLine ("P3"); // Magic number for text-based portable pixmap format |
| 96 | + |
| 97 | + writer.WriteLine (MAGIC_SEQUENCE); |
19 | 98 | writer.WriteLine ($"{imageSize.Width} {imageSize.Height}"); |
20 | 99 | writer.WriteLine (byte.MaxValue); |
| 100 | + |
21 | 101 | for (int row = 0; row < imageSize.Height; row++) { |
22 | 102 | int rowStart = row * imageSize.Width; |
23 | 103 | int rowEnd = rowStart + imageSize.Width; |
24 | 104 | for (int index = rowStart; index < rowEnd; index++) { |
25 | 105 | ColorBgra color = pixelData[index]; |
26 | | - string r = color.R.ToString ().PadLeft (3, ' '); |
27 | | - string g = color.G.ToString ().PadLeft (3, ' '); |
28 | | - string b = color.B.ToString ().PadLeft (3, ' '); |
29 | | - writer.Write ($"{r} {g} {b}"); |
| 106 | + WritePixel (color); |
30 | 107 | if (index != rowEnd - 1) |
31 | 108 | writer.Write (" "); |
32 | 109 | } |
33 | 110 | writer.WriteLine (); |
34 | 111 | } |
35 | | - writer.Close (); |
| 112 | + |
| 113 | + void WritePixel (ColorBgra color) |
| 114 | + { |
| 115 | + string r = RepresentColorComponent (color.R); |
| 116 | + string g = RepresentColorComponent (color.G); |
| 117 | + string b = RepresentColorComponent (color.B); |
| 118 | + writer.Write ($"{r} {g} {b}"); |
| 119 | + } |
| 120 | + |
| 121 | + static string RepresentColorComponent (byte component) |
| 122 | + => component.ToString ().PadLeft (3, ' '); |
36 | 123 | } |
37 | 124 |
|
38 | | - public void Export ( |
39 | | - Document document, |
40 | | - Gio.File file, |
41 | | - Window parent) |
| 125 | + public void Export (Document document, Gio.File file, Window parent) |
42 | 126 | { |
43 | 127 | using ImageSurface flattenedImage = document.GetFlattenedImage (); |
44 | 128 | using GioStream outputStream = new (file.Replace ()); |
45 | 129 | Export (flattenedImage, outputStream); |
46 | 130 | } |
| 131 | + |
| 132 | + private readonly ref struct PpmTokenReader |
| 133 | + { |
| 134 | + private readonly TextReader reader; |
| 135 | + internal PpmTokenReader (TextReader reader) |
| 136 | + { |
| 137 | + this.reader = reader; |
| 138 | + } |
| 139 | + |
| 140 | + public string ReadToken () |
| 141 | + { |
| 142 | + SkipWhitespaceAndComments (); |
| 143 | + StringBuilder token = new (); |
| 144 | + int c; |
| 145 | + while ((c = reader.Read ()) != -1) { |
| 146 | + char ch = (char) c; |
| 147 | + if (char.IsWhiteSpace (ch)) break; |
| 148 | + if (ch == '#') { // Comment |
| 149 | + reader.ReadLine (); // Skip rest of line |
| 150 | + break; |
| 151 | + } |
| 152 | + token.Append (ch); |
| 153 | + } |
| 154 | + return |
| 155 | + token.Length > 0 |
| 156 | + ? token.ToString () |
| 157 | + : throw new FormatException ("Unexpected end of data"); |
| 158 | + } |
| 159 | + |
| 160 | + public int ReadPositiveInt (string fieldName) |
| 161 | + { |
| 162 | + int value = ReadInt (); |
| 163 | + return |
| 164 | + value > 0 |
| 165 | + ? value |
| 166 | + : throw new FormatException ($"Invalid {fieldName}: {value}"); |
| 167 | + } |
| 168 | + |
| 169 | + public int ReadColorComponent (int maxValue) |
| 170 | + { |
| 171 | + int value = ReadInt (); |
| 172 | + return |
| 173 | + value >= 0 && value <= maxValue |
| 174 | + ? value |
| 175 | + : throw new FormatException ($"Color component {value} outside range 0..{maxValue}"); |
| 176 | + } |
| 177 | + |
| 178 | + private int ReadInt () |
| 179 | + { |
| 180 | + SkipWhitespaceAndComments (); |
| 181 | + int value = 0; |
| 182 | + bool found = false; |
| 183 | + int c; |
| 184 | + while ((c = reader.Peek ()) is >= '0' and <= '9') { |
| 185 | + reader.Read (); |
| 186 | + value = value * 10 + (c - '0'); |
| 187 | + found = true; |
| 188 | + } |
| 189 | + return |
| 190 | + found |
| 191 | + ? value |
| 192 | + : throw new FormatException ("Expected integer value"); |
| 193 | + } |
| 194 | + |
| 195 | + private void SkipWhitespaceAndComments () |
| 196 | + { |
| 197 | + while (reader.Peek () is int c and not -1) { |
| 198 | + char ch = (char) c; |
| 199 | + if (char.IsWhiteSpace (ch)) { |
| 200 | + reader.Read (); |
| 201 | + continue; |
| 202 | + } |
| 203 | + if (ch == '#') { // Comment |
| 204 | + reader.ReadLine (); // Skip rest of line |
| 205 | + continue; |
| 206 | + } |
| 207 | + break; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
47 | 211 | } |
0 commit comments