/*---------------------------------------------------------------------------
 * BMPSAVER_Color 画面情報
 * bscscr.c (2000/12/12 - 2000/12/14)
 * Copyright (c) 2000, Kazuhide Hatsuyama, All rights reserved.
 *   mail    : hat@lares.dti.ne.jp
 *   HomePage: http://www.lares.dti.ne.jp/~hat/
 *---------------------------------------------------------------------------*/

#define BIOS_INLINE

#include <sys/bios.h>
#include <sys/libwwc.h>
#include <stdio.h>

#include "bsbmp_u.h"
#include "bscbmp_u.h"
#include "bscscr_u.h"
#include "bmpsaver.h"

/* Bits Per Pixel */
#define KH_BS_BPP_MONO	4
#define KH_BS_BPP_COLOR	8

/*--------------------------------------------------------------------*/
/* BMPファイルに保存 （前バージョンとの互換性）*/
int bsc_save_bmp(const char far *fn, int x, int y, int w, int h)
{
	return bsc_save_screen(fn, x, y, w, h);
}

/*--------------------------------------------------------------------*/
/* BMPファイルに保存 */
int bsc_save_screen(const char far *fn, int x, int y, int w, int h)
{
	static BMPFILEINFO bfi;
	int src_x, src_y;
	unsigned short lcd_indicator = 0x0020;
	unsigned char color_mode;

	/* スワンカラーか？ */
	if(wwc_get_hardarch()!=HARDARCH_WSC) return KH_BS_ERROR;
	color_mode = wwc_get_color_mode();

	/* インジケータ点灯 */
	lcd_set_segments(lcd_indicator);

	/* BMPファイルヘッダー初期化 */
	if(color_mode==COLOR_MODE_GRAYSCALE) {
		bs_bmp_init_header(w, h);
		bfi.bpp = KH_BS_BPP_MONO;
	} else {
		bsc_bmp_init_header(w, h);
		bfi.bpp = KH_BS_BPP_COLOR;
	}

	/* 開く */
	bfi.fn = fn;
	bfi.w = w;
	bfi.h = h;
	if(!bs_open(&bfi))
	{
		return KH_BS_ERROR;
	}

	/* 画面情報取得 */
	bsc_screen_init_info();

	/* 画像データ書き込み */
	for(src_y=y+h-1;src_y>=y;src_y--)
	{
		/* インジケータ点滅 */
		lcd_indicator = (lcd_indicator+0x0020)&0x0020;
		lcd_set_segments(lcd_indicator);
		for(src_x=x;src_x<x+w;src_x++)
		{
			bs_putcolor(bsc_screen_get_color(src_x, src_y));
		}
	}

	/* 終了 */
	lcd_set_segments(0);
	bs_close();
	return KH_BS_OK;
}
