Home | History | Annotate | Download | only in filterfw
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 // Extract histogram from image.
     18 
     19 package androidx.media.filterpacks.colorspace;
     20 
     21 import androidx.media.filterfw.FrameValue;
     22 
     23 import androidx.media.filterfw.Filter;
     24 import androidx.media.filterfw.Frame;
     25 import androidx.media.filterfw.FrameBuffer2D;
     26 import androidx.media.filterfw.FrameType;
     27 import androidx.media.filterfw.MffContext;
     28 import androidx.media.filterfw.OutputPort;
     29 import androidx.media.filterfw.Signature;
     30 
     31 import java.nio.ByteBuffer;
     32 import java.nio.ByteOrder;
     33 import java.nio.FloatBuffer;
     34 
     35 /**
     36  * ColorfulnessFilter takes in a particular Chroma histogram generated by NewChromaHistogramFilter
     37  * and compute the colorfulness based on the entropy in Hue space.
     38  */
     39 public final class ColorfulnessFilter extends Filter {
     40 
     41     public ColorfulnessFilter(MffContext context, String name) {
     42         super(context, name);
     43     }
     44 
     45     @Override
     46     public Signature getSignature() {
     47         FrameType dataIn = FrameType.buffer2D(FrameType.ELEMENT_FLOAT32);
     48         return new Signature()
     49             .addInputPort("histogram", Signature.PORT_REQUIRED, dataIn)
     50             .addOutputPort("score", Signature.PORT_REQUIRED, FrameType.single(float.class))
     51             .disallowOtherPorts();
     52     }
     53 
     54     @Override
     55     protected void onProcess() {
     56         FrameBuffer2D histogramFrame =
     57                 getConnectedInputPort("histogram").pullFrame().asFrameBuffer2D();
     58         ByteBuffer byteBuffer = histogramFrame.lockBytes(Frame.MODE_READ);
     59         byteBuffer.order(ByteOrder.nativeOrder());
     60         FloatBuffer histogramBuffer = byteBuffer.asFloatBuffer();
     61         histogramBuffer.rewind();
     62 
     63         // Create a hue histogram from hue-saturation histogram
     64         int hueBins = histogramFrame.getWidth();
     65         int saturationBins = histogramFrame.getHeight() - 1;
     66         float[] hueHistogram = new float[hueBins];
     67         float total = 0;
     68         for (int r = 0; r < saturationBins; ++r) {
     69             float weight = (float) Math.pow(2, r);
     70             for (int c = 0; c < hueBins; c++) {
     71                 float value = histogramBuffer.get() * weight;
     72                 hueHistogram[c] += value;
     73                 total += value;
     74             }
     75         }
     76         float colorful = 0f;
     77         for (int c = 0; c < hueBins; ++c) {
     78             float value = hueHistogram[c] / total;
     79             if (value > 0f) {
     80                 colorful -= value * ((float) Math.log(value));
     81             }
     82         }
     83 
     84         colorful /= Math.log(2);
     85 
     86         histogramFrame.unlock();
     87         OutputPort outPort = getConnectedOutputPort("score");
     88         FrameValue frameValue = outPort.fetchAvailableFrame(null).asFrameValue();
     89         frameValue.setValue(colorful);
     90         outPort.pushFrame(frameValue);
     91     }
     92 
     93 }
     94