int WangTilesProcessor::TileMismatch(const TilePack & tiles,
                                     const Pyramid & input)
{
    int result = -1;

    for(int lev = 0; lev < input.NumLevels(); lev++)
    {
        if(TileMismatch(tiles, input[lev]))
        {
            result = lev;
            break;
        }
    }

    return result;
}
int WangTilesProcessor::BoxSanitize(const TilePack & tiles,
                                    const Pyramid & input,
                                    Pyramid & output,
                                    const int do_corner)
{
    // initialize output size
    output = input;

    // sanitize each level, from high resolution to low resolution
    for(int level = 0; level < input.NumLevels(); level++)
    {
        Array3D<Pixel> & level_now = output[level];
            
        if(level > 0)
        {
            const Array3D<Pixel> & level_up = output[level-1];
        
            // perform initial box filtering + down-sampling from previous level
            for(int row = 0; row < level_now.Size(0); row++)
                for(int col = 0; col < level_now.Size(1); col++)
                    for(int cha = 0; cha < level_now.Size(2); cha++)
                    {
                        Pixel temp = 0;
                    
                        for(int i = 2*row; i <= 2*row+1; i++)
                            for(int j = 2*col; j <= 2*col+1; j++)
                            {
                                const int row_up = i%level_up.Size(0);
                                const int col_up = j%level_up.Size(1);
                                        
                                temp += level_up[row_up][col_up][cha];
                            }

                        level_now[row][col][cha] = temp/4.0;
                    }
        }

        if(!SanitizeEdges(tiles, level_now, level_now)) return 0;
        if(do_corner && !SanitizeCorners(tiles, level_now, level_now)) return 0;
    }
    
    // done
    return 1;
}