#define PREVIEW_PIXEL_THRESHOLD 100000
#define G_R_B_MASK16  0x07E0F81F
static inline unsigned short blend_2_pixel565 (unsigned short color1, unsigned short color2)
{
    unsigned int expanded_pixel1 = (color1 | (color1 << 16)) & G_R_B_MASK16;
    unsigned int expanded_pixel2 = (color2 | (color2 << 16)) & G_R_B_MASK16;
    unsigned int blended_pixel = (expanded_pixel1+expanded_pixel2)>>1;
    blended_pixel &= G_R_B_MASK16;
    return blended_pixel | (blended_pixel >>16);
}

static inline unsigned short blend_4_pixel565 (unsigned short color1, unsigned short color2, unsigned short color3, unsigned short color4)
{
    unsigned int expanded_pixel1 = (color1 | (color1 << 16)) & G_R_B_MASK16;
    unsigned int expanded_pixel2 = (color2 | (color2 << 16)) & G_R_B_MASK16;
    unsigned int expanded_pixel3 = (color3 | (color3 << 16)) & G_R_B_MASK16;
    unsigned int expanded_pixel4 = (color4 | (color4 << 16)) & G_R_B_MASK16;
    unsigned int blended_pixel = (expanded_pixel1+expanded_pixel2+expanded_pixel3+expanded_pixel4)>>2;
    blended_pixel &= G_R_B_MASK16;
    return blended_pixel | (blended_pixel >>16);
}


#define BLIT_4_TO_16_quarter(DEST,SRC,LEN)  \
{ \
    register int n = LEN; \
    register unsigned short *dest = DEST; \
    register unsigned char *src = SRC; \
    while (n) \
    { \
	*dest++ = blend_4_pixel565(dingoo_palette[(*src)&0xf], dingoo_palette[(*(src+1))&0xf],dingoo_palette[(*(src+p->width))>>4], dingoo_palette[(*(src+p->width+1))>>4]); \
	src+=2; \
	n--; \
    } \
}



#define BLIT_8_TO_16_quarter(DEST,SRC,LEN)  \
{ \
    register int n = LEN; \
    register unsigned short *dest = DEST; \
    register unsigned char *src = SRC; \
    while (n) \
    { \
	*dest++ = blend_4_pixel565(dingoo_palette[*src], dingoo_palette[*(src+2)],dingoo_palette[*(src+(p->width<<1)+1)], dingoo_palette[*(src+(p->width<<1)+3)]); \
	src+=4; \
	n--; \
    } \
}

#define BLIT_24_TO_16_quarter(DEST,SRC,LEN)  \
{ \
    register int n = LEN; \
    register unsigned short *dest = DEST; \
    register unsigned char *src = SRC; \
    while (n) \
    { \
	*dest++ = RGB888_TO_565(*src, *(src+1), *(src+2)); \
	src+=12; \
	n--; \
    } \
}


static int quartersized_preview(struct png_info *p, unsigned int percent)
{
    unsigned int x = (p->width >> 2);		    /* Half size dimensions */
    unsigned int y = (p->height >> 2);		    /* Half size dimensions */
    unsigned int display_width = (x * (percent/100.0f)); /* Amount of image to display */
    unsigned short *out = dingoo_screen15;	    /* from mimimal.c */
    unsigned char *in = p->image;		    /* pointer image data */
    int offset_y = ((DINGOO_SCREEN_HEIGHT-y)>>1) * DINGOO_SCREEN_WIDTH ;
    int offset_x = DINGOO_SCREEN_WIDTH-(display_width);
    unsigned int bits_per_pixel = p->color_type==2?24:p->bit_depth;

    if (offset_y < 0) offset_y = 0;
    if (offset_x < 0) offset_x = 0;
    if (y>DINGOO_SCREEN_HEIGHT) y=DINGOO_SCREEN_HEIGHT;

    out += offset_y + offset_x;

    if (!in)
    {
	return 0;
    }

    do 
    {
	    switch (bits_per_pixel)
	    {
#if 0
		case 2:
		    BLIT_2_TO_16_quarter(out,in,display_width);
		    in+=(p->width); /* Skip current line + 3 lines, 4 pixels per byte */
		    break;
#endif
		case 4:
		    BLIT_4_TO_16_quarter(out,in,display_width);
		    in+=(p->width * 2); /* Skip current line + 3 lines, 2 pixels per byte */
		    break;
		case 8:
		    BLIT_8_TO_16_quarter(out,in,display_width);
		    in+=(p->width * 4); /* Skip current line + 3 lines, 1 pixel per byte */
		    break;
		case 24:
		    BLIT_24_TO_16_quarter(out,in,display_width);
		    in+=(p->width * 12 ); /* Skip current line + 3 lines, 1 pixel per 2 byte */
		    break;
	    }
	    out+=DINGOO_SCREEN_WIDTH;
	    y--;
    } while (y);

    return 1;

}

#define BLIT_1_TO_16_half(DEST,SRC,LEN)  \
{ \
    register int n = LEN/4; \
    register unsigned short *dest = DEST; \
    register unsigned char *src = SRC; \
    while (n) \
    { \
	*dest++ = blend_2_pixel565(dingoo_palette[((*src)&0x40)>>6], dingoo_palette[((*src)&0x80)>>7]); \
	*dest++ = blend_2_pixel565(dingoo_palette[((*src)&0x10)>>4], dingoo_palette[((*src)&0x20)>>5]); \
	*dest++ = blend_2_pixel565(dingoo_palette[((*src)&0x04)>>2], dingoo_palette[((*src)&0x08)>>3]); \
	*dest++ = blend_2_pixel565(dingoo_palette[(*src)&0x01], dingoo_palette[((*src)&0x02)>>1]); \
	src+=1; \
	n--; \
    } \
}

#define BLIT_2_TO_16_half(DEST,SRC,LEN)  \
{ \
    register int n = LEN/2; \
    register unsigned short *dest = DEST; \
    register unsigned char *src = SRC; \
    while (n) \
    { \
	*dest++ = blend_2_pixel565(dingoo_palette[((*src)&0x30)>>4], dingoo_palette[((*src)&0xC0)>>6]); \
	*dest++ = blend_2_pixel565(dingoo_palette[(*src)&0x03], dingoo_palette[((*src)&0x0C)>>2]); \
	src+=1; \
	n--; \
    } \
}


#define BLIT_4_TO_16_half(DEST,SRC,LEN)  \
{ \
    register int n = LEN; \
    register unsigned short *dest = DEST; \
    register unsigned char *src = SRC; \
    while (n) \
    { \
	*dest++ = blend_4_pixel565(dingoo_palette[(*src)&0xf], dingoo_palette[(*src)>>4],dingoo_palette[(*(src+(p->width>>1)))&0xf], dingoo_palette[(*(src+(p->width>>1)))>>4]); \
	src+=1; \
	n--; \
    } \
}


#define BLIT_8_TO_16_half(DEST,SRC,LEN)  \
{ \
    register int n = LEN; \
    register unsigned short *dest = DEST; \
    register unsigned char *src = SRC; \
    while (n) \
    { \
	*dest++ = blend_4_pixel565(dingoo_palette[*src], dingoo_palette[*(src+1)],dingoo_palette[*(src+p->width)], dingoo_palette[*(src+p->width+1)]); \
	src+=2; \
	n--; \
    } \
}

#define BLIT_24_TO_16_half(DEST,SRC,LEN)  \
{ \
    register int n = LEN; \
    register unsigned short *dest = DEST; \
    register unsigned char *src = SRC; \
    while (n) \
    { \
	*dest++ = RGB888_TO_565(*src, *(src+1), *(src+2)); \
	src+=6; \
	n--; \
    } \
}

static int halfsized_preview(struct png_info *p, unsigned int percent)
{
    unsigned int x = (p->width >> 1);		    /* Half size dimensions */
    unsigned int y = (p->height >> 1);		    /* Half size dimensions */
    unsigned int display_width = (x * (percent/100.0f)); /* Amount of image to display */
    unsigned short *out = dingoo_screen15;	    /* from mimimal.c */
    unsigned char *in = p->image;		    /* pointer image data */
    int offset_y = ((DINGOO_SCREEN_HEIGHT-y)>>1) * DINGOO_SCREEN_WIDTH ;
    int offset_x = DINGOO_SCREEN_WIDTH-(display_width);
    unsigned int bits_per_pixel = p->color_type==2?24:p->bit_depth;

    if (offset_y < 0) offset_y = 0;
    if (offset_x < 0) offset_x = 0;
    if (y>DINGOO_SCREEN_HEIGHT) y=DINGOO_SCREEN_HEIGHT;

    out += offset_y + offset_x;

    if (!in)
    {
	return 0;
    }

    //debug_printf("depth:%d\n", p->bit_depth);

    do 
    {
	    switch (bits_per_pixel)
	    {
		case 1:
		    BLIT_1_TO_16_half(out,in,display_width);
		    in+=(p->width/4); /* Skip a line, 8 pixels per byte */
		    break;
		case 2:
		    BLIT_2_TO_16_half(out,in,display_width);
		    in+=(p->width/2); /* Skip a line, 4 pixels per byte */
		    break;
		case 4:
		    BLIT_4_TO_16_half(out,in,display_width);
		    in+=(p->width); /* Skip a line, 2 pixels per byte */
		    break;
		case 8:
		    BLIT_8_TO_16_half(out,in,display_width);
		    in+=(p->width * 2); /* Skip a line, 1 pixel per byte */
		    break;
		case 24:
		    BLIT_24_TO_16_half(out,in,display_width);
		    in+=(p->width * 6); /* Skip a line, 1 pixel per 3 byte */
		    break;
	    }
	    out+=DINGOO_SCREEN_WIDTH;
	    y--;
    } while (y);

    return 1;

}

enum
{
    SNAP_NOT_INIT = 0,
    SNAP_INIT,
    SNAP_ALLOCATED
};

static void snapshot_preview(char *game, unsigned int percent)
{
    static char snapshot_name[10];
    static char snapshot_path[32];
    static char snapdir[6] = "snap\0";
    static int snapshot_status = SNAP_NOT_INIT;
    static struct png_info snapshot_png;
    int preview_it;
    void *f;

    // Assume we are not going to preview it
    preview_it = 0;
    sprintf(snapshot_path, "%s.png", game);

    if (snapshot_status == SNAP_NOT_INIT)
    {
	extern char *screenshotdir;
	memset(&snapshot_png, 0, sizeof(struct png_info));
	snapshot_status = SNAP_INIT;
	snapshot_name[0] = '\0';
	screenshotdir = snapdir; // Not set yet so point to here
    }

    if (strcmp(game, snapshot_name)==0 && snapshot_status == SNAP_ALLOCATED)
    {
	// already cached
	preview_it = 1;
#if 0
	do
	{
	    /* otherwise use "gamenameNN.png" */
	    sprintf(name,"%.8s%02d",Machine->gamedrv->name,snapno++);
	} while (osd_faccess(name, OSD_FILETYPE_SCREENSHOT));
#endif
    }
    else
    {
	f = osd_fopen(game, game, OSD_FILETYPE_SCREENSHOT, 0);
	if (f)
	{
	    // Free it up first
	    free_png(&snapshot_png);

	    if (png_read_file(f, &snapshot_png))
	    {
		// Success
		strcpy(snapshot_name,game);
		snapshot_status = SNAP_ALLOCATED;
		preview_it = 1;
	    }
	    else
	    {
		// Invalid PNG?
		free_png(&snapshot_png);
		snapshot_status = SNAP_NOT_INIT;
	    }
	    osd_fclose(f);
	}
    }

    /* Do the preview */
    if (preview_it)
    {
	//debug_printf("%s %ux%u %u-bit colors=%u\n", game, snapshot_png.width, snapshot_png.height, snapshot_png.bit_depth, snapshot_png.num_palette);
	// Convert 24-bit palette to 16-bit
	if (snapshot_png.bit_depth <= 8)
	{
	    unsigned int i = snapshot_png.num_palette;
	    for(i=0; i<snapshot_png.num_palette*3; i+=3)
	    {
		dingoo_video_color8(i/3,snapshot_png.palette[i],snapshot_png.palette[i+1],snapshot_png.palette[i+2]);
	    }
	}
	if (snapshot_png.width * snapshot_png.height > PREVIEW_PIXEL_THRESHOLD)
	{
	    if (!quartersized_preview(&snapshot_png, percent))
	    {
		debug_printf("ERROR: screenshot %s\n", snapshot_path);
	    }
	}
	else
	{
	    if (!halfsized_preview(&snapshot_png, percent))
	    {
		debug_printf("ERROR: screenshot %s\n", snapshot_path);
	    }
	}
    }
}

    /* load background */
    load_background(BITMAP_MENU);
  
    /* Wait until user selects a game */
    while(1)
    {
	unsigned long ExKey;
	unsigned int slide_index = 0;
	game_list_view(game,driver,desc,&flags);
	//snapshot_preview(game, 100);
	dingoo_flip_video();

	if( dingoo_joystick_read(1)) dingoo_timer_delay(DINGOO_DELAY); 
	while(!(ExKey=dingoo_joystick_read(1)))
	{
	    dingoo_timer_delay(1000/20);
	    if (slide_index < 13 /* see slide[] directly below */)
	    {
		unsigned int slide[]={2,4,8,16,24,32,40,56,72,80,88,96,98,100,100};
		slide_index++;
		snapshot_preview(game, slide[slide_index]);
		dingoo_flip_video();
	    }
	}
	/* Check for game selection movement */
	if (ExKey & DINGOO_UP) last_game_selected--;
	if (ExKey & DINGOO_DOWN) last_game_selected++;
	if (ExKey & DINGOO_LEFT) last_game_selected-=21;
	if (ExKey & DINGOO_RIGHT) last_game_selected+=21;
