/*
 * Copyright (C) 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.googlecode.leptonica.android.test;

import junit.framework.TestCase;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.test.suitebuilder.annotation.SmallTest;

import com.googlecode.leptonica.android.GrayQuant;
import com.googlecode.leptonica.android.Pix;
import com.googlecode.leptonica.android.ReadFile;
import com.googlecode.leptonica.android.Skew;

public class SkewTest extends TestCase {

    private static final String SENTENCE = "The quick brown fox jumps over the lazy dog.";

    @SmallTest
    public void testFindSkew() {
        testFindSkew(SENTENCE, 640, 480, -15.0f);
        testFindSkew(SENTENCE, 640, 480, 0.0f);
        testFindSkew(SENTENCE, 640, 480, 15.0f);
    }

    private void testFindSkew(String text, int width, int height, float skew) {
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        Canvas canvas = new Canvas(bmp);

        paint.setColor(Color.BLACK);
        paint.setStyle(Style.FILL);
        paint.setAntiAlias(true);
        paint.setTextAlign(Align.CENTER);
        paint.setTextSize(32.0f);

        canvas.drawColor(Color.WHITE);
        canvas.rotate(skew, width / 2, height / 2);
        canvas.drawText(SENTENCE, width / 2, height / 2 , paint);

        Pix pixs = ReadFile.readBitmap(bmp);
        Pix pixd = GrayQuant.pixThresholdToBinary(pixs, 1);
        pixs.recycle();
        float measuredSkew = -Skew.findSkew(pixd);
        pixd.recycle();
        bmp.recycle();

        boolean isInRange = skew - 1 < measuredSkew && measuredSkew < skew + 1;
        assertTrue("Skew has incorrect value.", isInRange);
    }
}

@@ -925,24 +925,73 @@
 SmallTest;%0A%0A
+import com.googlecode.leptonica.android.Convert;%0A
 import com.g
@@ -2133,16 +2133,17 @@
 p(bmp);%0A
+%0A
         
@@ -2146,16 +2146,143 @@
     Pix 
+pixd;%0A        if (pixs.getDepth() != 4 %7C%7C pixs.getDepth() != 8) %7B%0A            Pix pix8 = Convert.convertTo8(pixs);%0A            
 pixd = G
@@ -2314,17 +2314,17 @@
 nary(pix
-s
+8
 , 1);%0A  
@@ -2329,20 +2329,24 @@
         
+ 
+   
 pix
-s
+8
 .recycle
@@ -2341,32 +2341,120 @@
 pix8.recycle();%0A
+        %7D else %7B%0A            pixd = GrayQuant.pixThresholdToBinary(pixs, 1);%0A        %7D%0A%0A
         float me
@@ -2500,47 +2500,22 @@
     
-pixd.recycle();%0A        bmp.recycle();%0A
+float tol = 1;
 %0A   
@@ -2546,17 +2546,19 @@
  skew - 
-1
+tol
  %3C measu
@@ -2590,17 +2590,19 @@
  skew + 
-1
+tol
 ;%0A      
@@ -2654,16 +2654,88 @@
 nRange);
+%0A%0A        pixs.recycle();%0A        pixd.recycle();%0A        bmp.recycle();
 %0A    %7D%0A%7D

Change SkewTest