#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned char uint8_t;

#include "glyphs.c"
#include "authimage3.c"


int main (void)
{
    int x, y;
    int bytes_per_row = (authimage3_width+7)>>3;
    int idx = 0;
    uint8_t *pix_buf = malloc(authimage3_width * authimage3_height);
    if (pix_buf == NULL)
    {
        return 1;
    }

    for (y = 0; y < authimage3_height; y++)
    {
        int bits_so_far = 0;
        for (x = 0; x < bytes_per_row; x++)
        {
            uint8_t byte = authimage3_bits[(y*bytes_per_row)+x];
            int bit;
            for (bit = 0; bit < 8; bit++)
            {
                if (bits_so_far == authimage3_width)
                {
                    break;
                }

                if (byte & (1<<bit))
                {
                    pix_buf[idx++] = 0xff;
                }
                else
                {
                    pix_buf[idx++] = 0;
                }
                bits_so_far++;
            }
        }
    }

    for (x = 1; x < authimage3_width-1; x++)
    {
        for (y = 1; y < authimage3_height-1; y++)
        {
            if (pix_buf[(y*authimage3_width)+x])
            {
                int g;
                for (g = 0; g < nglyphs; g++)
                {
                    int xx,yy;
                    int top;
                    int found = 1;

                    top = y-glyphs[g].y_offs;
                    if (top < 1)
                    {
                        break;
                    }


                    for (yy = 0; yy < glyphs[g].h; yy++)
                    {
                        for (xx = 0; xx < glyphs[g].w; xx++)
                        {
                            if (glyphs[g].glyph[(yy*glyphs[g].w)+xx] != pix_buf[((yy+top)*authimage3_width)+xx+x])
                            {
                                found = 0;
                                break;
                            }
                        }
                        if (found == 0)
                        {
                            break;
                        }
                    }
                    if (found)
                    {
                        printf("%c",glyphs[g].c);
                        y = 0;
                        x += glyphs[g].w;
                    }
                }
            }
        }
    }

    return 0;
}