github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/model/image_test.go (about) 1 /* 2 * This file is subject to the terms and conditions defined in 3 * file 'LICENSE.md', which is part of this source code package. 4 */ 5 6 package model 7 8 import ( 9 "testing" 10 ) 11 12 func TestImageResampling(t *testing.T) { 13 img := Image{} 14 15 // Case 1: 16 // Data: 17 // 4x8bit: 00000001 11101000 01101110 00001010 18 // Resample as 1bit: 19 // 20 // 4x8bit: 00000001 11101000 01101110 00001010 21 // Downsample to 1bit 22 // 4x8bit: 00000000 00000001 00000000 00000000 23 // 4x1bit: 0100 24 // Padding with 4x00 25 // -> 01000000 = 64 decimal 26 // 27 img.BitsPerComponent = 8 28 img.Data = []byte{1, 232, 110, 10} 29 //int(this.Width) * int(this.Height) * this.ColorComponents 30 img.Width = 4 31 img.ColorComponents = 1 32 img.Height = 1 33 img.Resample(1) 34 if len(img.Data) != 1 { 35 t.Errorf("Incorrect length != 1 (%d)", len(img.Data)) 36 return 37 } 38 if img.Data[0] != 64 { 39 t.Errorf("Value != 4 (%d)", img.Data[0]) 40 } 41 42 // Case 2: 43 // Data: 44 // 4x8bit: 00000001 11101000 01101110 00001010 00000001 11101000 01101110 00001010 00000001 11101000 01101110 00001010 45 // 0 1 0 0 0 1 0 0 0 1 0 0 46 // 010001000100 47 // -> 01000100 0100(0000) 48 // -> 68 64 49 img.BitsPerComponent = 8 50 img.Data = []byte{1, 232, 110, 10, 1, 232, 110, 10, 1, 232, 110, 10} 51 img.Width = 12 52 img.ColorComponents = 1 53 img.Height = 1 54 img.Resample(1) 55 56 if len(img.Data) != 2 { 57 t.Errorf("Incorrect length != 2 (%d)", len(img.Data)) 58 return 59 } 60 if img.Data[0] != 68 { 61 t.Errorf("Value != 68 (%d)", img.Data[0]) 62 } 63 if img.Data[1] != 64 { 64 t.Errorf("Value != 64 (%d)", img.Data[1]) 65 } 66 }