//
//  OmImageTests.cpp
//  OmUtil
//
//  Created by David Van Brink on 1/8/17.
//  Copyright (c) 2017 omino.com. All rights reserved.
//

#include "OmImageTests.h"
#include "OmImageRgba8.h"
#include "OmAsserts.h"


OMTEST(doInterpolatedGetPixel)
{
    OmImageRgba8 im(2,2);
    im.clear();
    im.setPixel(0, 0, 0x00000101);
    im.setPixel(1, 0, 0x0000FF01);
    im.setPixel(0, 1, 0x000001FF);
    im.setPixel(1, 1, 0x0000FFFF);
    
    ASSERT_EQUALS_INT("pinned topleft", 0x00000101, im.getPixel(0.0, 0.0));
    ASSERT_EQUALS_INT("pixel", 0x00000101, im.getPixel(0.5, 0.5));
    ASSERT_EQUALS_INT("pixel", 0x00008080, im.getPixel(1.0, 1.0));
    ASSERT_EQUALS_INT("pixel", 0x00008001, im.getPixel(1.0, 0.5));
    ASSERT_EQUALS_INT("pixel", 0x0000ffff, im.getPixel(2.0, 2.0));
    
    printf("(0.0, 0.0) 0x%08x\n", im.getPixel(0.0, 0.0));
    printf("(0.5, 0.5) 0x%08x\n", im.getPixel(0.5, 0.5));
    printf("(1.0, 0.5) 0x%08x\n", im.getPixel(1.0, 0.5));
    printf("(1.0, 1.0) 0x%08x\n", im.getPixel(1.0, 1.0));
    printf("(2.0, 2.0) 0x%08x\n", im.getPixel(2.0, 2.0));
}

OMTEST(do8x8)
{
    OmImageRgba8 im(32, 8);
    im.clear();
    im.drawChar(0xffffff41, 0, 0, 'A');
    im.drawChar(0xffffff42, 8, 0, 'B');
    im.drawChar(0xffffff43, 16, 4, 'C');
    im.drawChar(0xffffff44, 24, -4, 'D');
    
    printf("\n");
    for(int y = 0; y < im.height; y++)
    {
        for(int x = 0; x < im.width; x++)
        {
            uint32_t p = im.getPixel(x, y);
            char c = p & 0x000000ff;
            if(c < 32) c = '.';
            printf("%c", c);
        }
        printf("\n");
    }
}

OMTEST(doReadWriteFile)
{
    OmImageRgba8 im(20,33);
    im.clear();
    for(int x = 0; x < 10; x++)
        for(int y = 0; y < 10; y++)
        {
            uint32_t p = 0xff000000 + (x << 4) + (y << 12);
            im.setPixel(x, y, p);
        }
    im.writeFile("OmImageTest.png");
    im.writeFile("OmImageTest.jpg");
    
    OmImageRgba8 *read1 = OmImageRgba8::readFile("OmImageTest.png");
    OmImageRgba8 *read2 = OmImageRgba8::readFile("OmImageTest.jpg");
    
    ASSERT_EQUALS_INT("read back the file", 20, read1->width);
    ASSERT_EQUALS_INT("read back the file", 33, read1->height);
    ASSERT_EQUALS_INT("read back the file", 20, read2->width);
    ASSERT_EQUALS_INT("read back the file", 33, read2->height);
    OMDELETE(read1);
    OMDELETE(read2);
}



//static void copyRect(OmImageRgba8 *src, OmImageRgba8 *dst, int sourceX, int sourceY, int destX, int destY, int w, int h)
//{
//    for(int x = 0; x < w; x++)
//        for(int y = 0; y < h; y++)
//        {
//            auto pixel = src->getPixel(sourceX + x, sourceY + y);
//            dst->setPixel(destX + x, destY + y, pixel);
//        }
//}



void allOmImageTests()
{
    doInterpolatedGetPixel();
    do8x8();
    doReadWriteFile();
}
