Skip to content

Commit 2907b27

Browse files
committed
[Tests] Add PixImage stream save / load tests
1 parent 266b308 commit 2907b27

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

src/Aardvark.PixImage.DevIL/PixImageDevil.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,17 @@ public void SaveToFile(string filename, PixImage image, PixSaveParams saveParams
274274
=> SaveImage(image, saveParams, "Save", imageType => I.Save(imageType, filename));
275275

276276
public void SaveToStream(Stream stream, PixImage image, PixSaveParams saveParams)
277-
=> SaveImage(image, saveParams, "SaveStream", imageType => I.SaveStream(imageType, stream));
277+
{
278+
if (saveParams.Format == PixFileFormat.Tiff)
279+
{
280+
// Not sure why...
281+
throw new NotSupportedException("DevIL does not support saving TIFF images to streams.");
282+
}
283+
else
284+
{
285+
SaveImage(image, saveParams, "SaveStream", imageType => I.SaveStream(imageType, stream));
286+
}
287+
}
278288

279289
private static PixImageInfo LoadPixImageInfo(Action load)
280290
=> LoadImage(load, (size, format, type, _) =>

src/Tests/Aardvark.Base.FSharp.Tests/PixLoaderTests.fs

+42-20
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ module PixLoaderTests =
142142
let loaders = PixImage.GetLoaders() |> Seq.filter (fun l -> l.Name <> "Aardvark PGM")
143143
Gen.elements loaders
144144

145-
let pixEncoder (format : PixFileFormat) =
145+
let pixEncoder (useStream : bool) (format : PixFileFormat) =
146146
pixLoader
147147
|> Gen.filter (fun loader ->
148-
loader.Name <> "DevIL" || format <> PixFileFormat.Gif // DevIL does not support saving GIFs
148+
not (loader.Name = "DevIL" && format = PixFileFormat.Gif) && // DevIL does not support saving GIFs
149+
not (loader.Name = "DevIL" && format = PixFileFormat.Tiff && useStream) // DevIL does not support saving TIFFs to streams
149150
)
150151

151152
let colorAndImageFileFormat =
@@ -165,9 +166,10 @@ module PixLoaderTests =
165166
type SaveLoadInput =
166167
{
167168
Image : PixImage<byte>
168-
FileFormat : PixFileFormat
169+
SaveParams : PixSaveParams
169170
Encoder : IPixLoader
170171
Decoder : IPixLoader
172+
UseStream : bool
171173
}
172174

173175
type Generator private () =
@@ -186,9 +188,22 @@ module PixLoaderTests =
186188
gen {
187189
let! cf, iff = Gen.colorAndImageFileFormat
188190
let! pix = Gen.checkerboardPix cf
189-
let! encoder = Gen.pixEncoder iff
191+
let! useStream = Gen.elements [false; true]
192+
let! encoder = Gen.pixEncoder useStream iff
190193
let! decoder = Gen.pixLoader
191-
return { Image = pix; FileFormat = iff; Encoder = encoder; Decoder = decoder }
194+
195+
let saveParams =
196+
match iff with
197+
| PixFileFormat.Jpeg -> PixJpegSaveParams(quality = 100) :> PixSaveParams
198+
| fmt -> PixSaveParams fmt
199+
200+
return {
201+
Image = pix
202+
SaveParams = saveParams
203+
Encoder = encoder
204+
Decoder = decoder
205+
UseStream = useStream
206+
}
192207
}
193208
|> Arb.fromGen
194209

@@ -197,30 +212,34 @@ module PixLoaderTests =
197212
Aardvark.Init()
198213
Report.Verbosity <- 3
199214

215+
200216
[<Property(Arbitrary = [| typeof<Generator> |])>]
201217
let ``[PixLoader] Save and load`` (input : SaveLoadInput) =
202-
printfn "encoder = %s, decoder = %s, size = %A, format = %A, file = %A" input.Encoder.Name input.Decoder.Name input.Image.Size input.Image.Format input.FileFormat
218+
printfn "encoder = %s, decoder = %s, size = %A, color format = %A, file format = %A, use stream = %b"
219+
input.Encoder.Name input.Decoder.Name input.Image.Size input.Image.Format input.SaveParams.Format input.UseStream
203220

204221
tempFile (fun file ->
205-
let saveParams =
206-
match input.FileFormat with
207-
| PixFileFormat.Jpeg -> PixJpegSaveParams(quality = 100) :> PixSaveParams
208-
| fmt -> PixSaveParams fmt
209-
210-
input.Image.Save(file, saveParams, false, input.Encoder)
211-
let result = PixImage<byte>(file, input.Decoder)
212-
213-
match input.FileFormat with
222+
let output =
223+
if input.UseStream then
224+
use stream = File.Open(file, FileMode.Create, FileAccess.ReadWrite)
225+
input.Image.Save(stream, input.SaveParams, input.Encoder)
226+
227+
stream.Position <- 0L
228+
PixImage<byte>(stream, input.Decoder)
229+
else
230+
input.Image.Save(file, input.SaveParams, false, input.Encoder)
231+
PixImage<byte>(file, input.Decoder)
232+
233+
match input.SaveParams.Format with
214234
| PixFileFormat.Jpeg | PixFileFormat.Gif ->
215-
let psnr = PixImage.peakSignalToNoiseRatio input.Image result
216-
let rmse = PixImage.rootMeanSquaredError input.Image result
217-
Expect.isGreaterThan psnr 25.0 "Bad peak-signal-to-noise ratio"
218-
Expect.isLessThan rmse 15.0 "Bad root-mean-square error"
235+
let psnr = PixImage.peakSignalToNoiseRatio input.Image output
236+
Expect.isGreaterThan psnr 20.0 "Bad peak-signal-to-noise ratio"
219237

220238
| _ ->
221-
PixImage.compare input.Image result
239+
PixImage.compare input.Image output
222240
)
223241

242+
224243
[<Property(Arbitrary = [| typeof<Generator> |])>]
225244
let ``[PixLoader] Aardvark PGM writer`` (pi : PixImage<byte>) =
226245
printfn "size = %A" pi.Size
@@ -235,6 +254,7 @@ module PixLoaderTests =
235254
PixImage.compare pi out
236255
)
237256

257+
238258
[<Property(Arbitrary = [| typeof<Generator> |])>]
239259
let ``[PixLoader] JPEG quality`` (loader : IPixLoader) (pi : PixImage<byte>) =
240260
printfn "loader = %s, size = %A, format = %A" loader.Name pi.Size pi.Format
@@ -252,6 +272,7 @@ module PixLoaderTests =
252272
)
253273
)
254274

275+
255276
[<Property(Arbitrary = [| typeof<Generator> |])>]
256277
let ``[PixLoader] PNG compression level`` (pi : PixImage<byte>) =
257278
printfn "size = %A, format = %A" pi.Size pi.Format
@@ -275,6 +296,7 @@ module PixLoaderTests =
275296
)
276297
)
277298

299+
278300
[<Test>]
279301
let ``[PixLoader] Add and remove loaders``() =
280302
let count = PixImage.GetLoaders() |> Seq.length

0 commit comments

Comments
 (0)