001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.imaging.formats.png.scanlinefilters; 018 019import java.io.IOException; 020 021import org.apache.commons.imaging.ImagingException; 022 023public class ScanlineFilterAverage implements ScanlineFilter { 024 private final int bytesPerPixel; 025 026 public ScanlineFilterAverage(final int bytesPerPixel) { 027 this.bytesPerPixel = bytesPerPixel; 028 } 029 030 @Override 031 public void unfilter(final byte[] src, final byte[] dst, final byte[] up) throws ImagingException, IOException { 032 for (int i = 0; i < src.length; i++) { 033 int raw = 0; 034 final int prevIndex = i - bytesPerPixel; 035 if (prevIndex >= 0) { 036 raw = dst[prevIndex]; 037 } 038 039 int prior = 0; 040 if (up != null) { 041 prior = up[i]; 042 } 043 044 final int average = ((0xff & raw) + (0xff & prior)) / 2; 045 046 dst[i] = (byte) ((src[i] + average) % 256); 047 // dst[i] = src[i]; 048 // dst[i] = (byte) 255; 049 } 050 } 051}