Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_png.lua « test - github.com/torch/image.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 376ef6f3ea90ce5100e30e049bc3a4fd61dc662c (plain)
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
require 'image'

-- Create an instance of the test framework
local mytester = torch.Tester()
local precision_mean = 1e-3
local test = {}

local function toBlob(filename)
  local f = torch.DiskFile(filename, 'r')
  f:binary()
  f:seekEnd()
  local size = f:position() - 1
  f:seek(1)
  local blob = torch.ByteTensor(size)
  f:readByte(blob:storage())
  f:close()
  return blob
end

local function checkPNG(imfile, depth, tensortype, want)
  local img = image.load(imfile, depth, tensortype)
  -- Tensors have to be converted to double, since assertTensorEq does not support ByteTensor
  print('img: ', img)
  print('want: ', want)
  mytester:assertTensorEq(img:double(), want:double(), precision_mean,
                          string.format('%s: pixel values are unexpected', imfile))
end

function test.LoadPNG()
  -- Gray 8-bit PNG image with width = 3, height = 1
  local gray8byte = torch.ByteTensor({{{0,127,255}}})
  checkPNG('gray3x1.png', 1, 'byte', gray8byte)

  local gray8double = torch.DoubleTensor({{{0, 127/255, 1}}})
  checkPNG('gray3x1.png', 1, 'double', gray8double)

  -- Gray 16-bit PNG image with width=1, height = 2
  local gray16byte = torch.ByteTensor({{{0, 255}}})
  checkPNG('gray16-1x2.png', 1, 'byte', gray16byte)

  local gray16float = torch.FloatTensor({{{0, 65534/65535}}})
  checkPNG('gray16-1x2.png', 1, 'float', gray16float)

  -- Color 8-bit PNG image with width = 2, height = 1
  local rgb8byte = torch.ByteTensor({{{255, 0, 0, 127, 63, 0}}})
  checkPNG('rgb2x1.png', 3, 'byte', rgb8byte)

  local rgb8float = torch.FloatTensor({{{1, 0, 0, 127/255, 63/255, 0}}})
  checkPNG('rgb2x1.png', 3, 'float', rgb8float)

  -- Color 16-bit PNG image with width = 2, height = 1
  local rgb16byte = torch.ByteTensor({{{255, 0, 0, 127, 63, 0}}})
  checkPNG('rgb16-2x1.png', 3, 'byte', rgb16byte)

  local rgb16float = torch.FloatTensor({{{1, 0, 0, 32767/65535, 16383/65535, 0}}})
  checkPNG('rgb16-2x1.png', 3, 'float', rgb16float)
end

function test.DecompressPNG()
  mytester:assertTensorEq(
    image.load('rgb2x1.png'),
    image.decompressPNG(toBlob('rgb2x1.png')),
    precision_mean,
    'decompressed and loaded images should be equal'
  )
end

function test.LoadCorruptedPNG()
  local ok, _ = pcall(image.load, 'corrupt-ihdr.png')
  mytester:assert(not ok, 'corrupted image should not be loaded')
end

-- Now run the test above
mytester:add(test)
mytester:run()