View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.palette;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.awt.image.BufferedImage;
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  import org.apache.commons.imaging.ImagingException;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Unit tests for class {@link Dithering}.
30   **/
31  public class DitheringTest {
32  
33      @Test
34      public void testApplyFloydSteinbergDitheringWithNonNullOne() throws ImagingException {
35          final BufferedImage bufferedImage = new BufferedImage(8, 8, 8);
36          bufferedImage.setRGB(2, 2, 2);
37          final List<ColorSpaceSubset> linkedList = new LinkedList<>();
38          final ColorSpaceSubset colorSpaceSubset = new ColorSpaceSubset(-234, -352);
39          colorSpaceSubset.rgb = 8;
40          linkedList.add(colorSpaceSubset);
41          final QuantizedPalette quantizedPalette = new QuantizedPalette(linkedList, 8);
42          Dithering.applyFloydSteinbergDithering(bufferedImage, quantizedPalette);
43  
44          assertEquals(-16777208, bufferedImage.getRGB(0, 0));
45          assertEquals(-16777208, bufferedImage.getRGB(1, 1));
46          assertEquals(-16777208, bufferedImage.getRGB(2, 1));
47          assertEquals(-16777208, bufferedImage.getRGB(2, 2));
48      }
49  
50      @Test
51      public void testApplyFloydSteinbergDitheringWithNonNullTwo() throws ImagingException {
52          final BufferedImage bufferedImage = new BufferedImage(3, 3, 3);
53          bufferedImage.setRGB(1, 2, 4);
54          final List<ColorSpaceSubset> linkedList = new LinkedList<>();
55          final ColorSpaceSubset colorSpaceSubset = new ColorSpaceSubset(-234, -352);
56          linkedList.add(colorSpaceSubset);
57          final QuantizedPalette quantizedPalette = new QuantizedPalette(linkedList, 3);
58          Dithering.applyFloydSteinbergDithering(bufferedImage, quantizedPalette);
59  
60          assertEquals(-1, bufferedImage.getRGB(0, 0));
61          assertEquals(-1, bufferedImage.getRGB(1, 1));
62          assertEquals(-1, bufferedImage.getRGB(2, 1));
63          assertEquals(-1, bufferedImage.getRGB(2, 2));
64      }
65  
66  }