たなしょのメモ

日々勉強していることをつらつらと

OS作成奮闘記day47

今日は画像ビューワを作っていきます。

gview/Makefile

APP      = gview
STACK    = 4480k
MALLOC   = 0k

TOOLPATH = ..\..\z_tools\\

INCPATH  = ..\..\z_tools\haribote\\
APILIBPATH   = ../apilib/
HARIBOTEPATH = ../haribote/

MAKE     = $(TOOLPATH)make.exe -r
NASK     = $(TOOLPATH)nask.exe
CC1      = $(TOOLPATH)cc1.exe -I$(INCPATH) -Os -Wall -quiet
GAS2NASK = $(TOOLPATH)gas2nask.exe -a
OBJ2BIM  = $(TOOLPATH)obj2bim.exe
MAKEFONT = $(TOOLPATH)makefont.exe
BIN2OBJ  = $(TOOLPATH)bin2obj.exe
BIM2HRB  = $(TOOLPATH)bim2hrb.exe
RULEFILE = ..\haribote.rul
EDIMG    = $(TOOLPATH)edimg.exe
IMGTOL   = $(TOOLPATH)imgtol.com
GOLIB    = $(TOOLPATH)golib00.exe
COPY     = cmd.exe /C copy
DEL      = rm -f

# デフォルト動作

default :
    $(MAKE) $(APP).hrb

# ファイル生成規則

$(APP).bim : $(APP).obj bmp.obj jpeg.obj $(APILIBPATH)apilib.lib Makefile
    $(OBJ2BIM) @$(RULEFILE) out:$(APP).bim map:$(APP).map stack:$(STACK) \
        $(APP).obj jpeg.obj bmp.obj $(APILIBPATH)apilib.lib

haribote.img : ../haribote/ipl20.bin ../haribote/haribote.sys $(APP).hrb \
        Makefile
    $(EDIMG)   imgin:../../../z_tools/fdimg0at.tek \
        wbinimg src:../haribote/ipl20.bin len:512 from:0 to:0 \
        copy from:../haribote/haribote.sys to:@: \
        copy from:$(APP).hrb to:@: \
        copy from:../nihongo/nihongo.fnt to:@: \
        imgout:haribote.img

# 一般規則

%.gas : %.c ../apilib.h Makefile
    $(CC1) -o $*.gas $*.c

%.nas : %.gas Makefile
    $(GAS2NASK) $*.gas $*.nas

%.obj : %.nas Makefile
    $(NASK) $*.nas $*.obj $*.lst

%.org : %.bim Makefile
    $(BIM2HRB) $*.bim $*.org $(MALLOC)

%.hrb : %.org Makefile
    $(BIM2BIN) -osacmp in:$*.org out:$*.hrb

# コマンド

run :
    $(MAKE) haribote.img
    $(COPY) haribote.img ../../../z_tools/qemu/fdimage0.bin
    $(MAKE) -C ../../../z_tools/qemu

full :
    $(MAKE) -C $(APILIBPATH)
    $(MAKE) $(APP).hrb

run_full :
    $(MAKE) -C $(APILIBPATH)
    $(MAKE) -C ../haribote
    $(MAKE) run

clean :
    -$(DEL) *.lst
    -$(DEL) gview.obj
    -$(DEL) jpeg.obj
    -$(DEL) *.map
    -$(DEL) *.bim
    -$(DEL) *.org
    -$(DEL) haribote.img

src_only :
    $(MAKE) clean
    -$(DEL) $(APP).hrb

gview.c

#include "apilib.h"

struct DLL_STRPICENV { /* 64KB */
    int work[64 * 1024 / 4];
};

struct RGB {
    unsigned char b, g, r, t;
};

/* bmp.nasm */
int info_BMP(struct DLL_STRPICENV *env, int *info, int size, char *fp);
int decode0_BMP(struct DLL_STRPICENV *env, int size, char *fp, int b_type, char *buf, int skip);

/* jpeg.c */
int info_JPEG(struct DLL_STRPICENV *env, int *info, int size, char *fp);
int decode0_JPEG(struct DLL_STRPICENV *env, int size, char *fp, int b_type, char *buf, int skip);

unsigned char rgb2pal(int r, int g, int b, int x, int y);
void error(char *s);

void HariMain(void)
{
    struct DLL_STRPICENV env;
    char filebuf[512 * 1024], winbuf[1040 * 805];
    char s[32], *p;
    int win, i, j, fsize, xsize, info[8];
    struct RGB picbuf[1024 * 768], *q;

    /* コマンドライン解析 */
    api_cmdline(s, 30);
    for (p = s; *p > ' '; p++) { }    /* スペースが来るまで読み飛ばす */
    for (; *p == ' '; p++) { }   /* スペースを読み飛ばす */

    /* ファイル読み込み */
    i = api_fopen(p); if (i == 0) { error("file not found.\n"); }
    fsize = api_fsize(i, 0);
    if (fsize > 512 * 1024) {
        error("file too large.\n");
    }
    api_fread(filebuf, fsize, i);
    api_fclose(i);

    /* ファイルタイプチェック */
    if (info_BMP(&env, info, fsize, filebuf) == 0) {
        /* BMPではなかった */
        if (info_JPEG(&env, info, fsize, filebuf) == 0) {
            /* JPEGでもなかった */
            api_putstr0("file type unknown.\n");
            api_end();
        }
    }
    /* どちらかのinfo関数が成功すると、以下の情報がinfoに入っている */
    /* info[0] : ファイルタイプ (1:BMP, 2:JPEG) */
    /* info[1] : カラー情報 */
    /* info[2] : xsize */
    /* info[3] : ysize */

    if (info[2] > 1024 || info[3] > 768) {
        error("picture too large.\n");
    }

    /* ウィンドウの準備 */
    xsize = info[2] + 16;
    if (xsize < 136) {
        xsize = 136;
    }
    win = api_openwin(winbuf, xsize, info[3] + 37, -1, "gview");

    /* ファイル内容を画像データに変換 */
    if (info[0] == 1) {
        i = decode0_BMP (&env, fsize, filebuf, 4, (char *) picbuf, 0);
    } else {
        i = decode0_JPEG(&env, fsize, filebuf, 4, (char *) picbuf, 0);
    }
    /* b_type = 4 は、 struct RGB 形式を意味する */
    /* skipは0にしておけばよい */
    if (i != 0) {
        error("decode error.\n");
    }

    /* 表示 */
    for (i = 0; i < info[3]; i++) {
        p = winbuf + (i + 29) * xsize + (xsize - info[2]) / 2;
        q = picbuf + i * info[2];
        for (j = 0; j < info[2]; j++) {
            p[j] = rgb2pal(q[j].r, q[j].g, q[j].b, j, i);
        }
    }
    api_refreshwin(win, (xsize - info[2]) / 2, 29, (xsize - info[2]) / 2 + info[2], 29 + info[3]);

    /* 終了待ち */
    for (;;) {
        i = api_getkey(1);
        if (i == 'Q' || i == 'q') {
            api_end();
        }
    }
}

unsigned char rgb2pal(int r, int g, int b, int x, int y)
{
    static int table[4] = { 3, 1, 0, 2 };
    int i;
    x &= 1; /* 偶数か奇数か */
    y &= 1;
    i = table[x + y * 2];  /* 中間色を作るための定数 */
    r = (r * 21) / 256;   /* これで 0~20 になる */
    g = (g * 21) / 256;
    b = (b * 21) / 256;
    r = (r + i) / 4;   /* これで 0~5 になる */
    g = (g + i) / 4;
    b = (b + i) / 4;
    return 16 + r + g * 6 + b * 36;
}

void error(char *s)
{
    api_putstr0(s);
    api_end();
}

Makefile

TOOLPATH = ..\..\z_tools\\

INCPATH  = ..\..\z_tools\haribote\\

MAKE     = $(TOOLPATH)make.exe -r
EDIMG    = $(TOOLPATH)edimg.exe
IMGTOL   = $(TOOLPATH)imgtol.com
COPY     = cmd.exe /C copy
DEL      = rm -f

# default operation

default :
    $(MAKE) haribote.img

# file making rule

haribote.img : haribote/ipl20.bin haribote/haribote.sys Makefile \
        a/a.hrb hello3/hello3.hrb hello4/hello4.hrb hello5/hello5.hrb \
        winhelo/winhelo.hrb winhelo2/winhelo2.hrb winhelo3/winhelo3.hrb \
        star1/star1.hrb stars/stars.hrb stars2/stars2.hrb \
        lines/lines.hrb walk/walk.hrb noodle/noodle.hrb \
        beepdown/beepdown.hrb color/color.hrb color2/color2.hrb \
        sosu/sosu.hrb sosu2/sosu2.hrb sosu3/sosu3.hrb \
        type/type.hrb iroha/iroha.hrb chklang/chklang.hrb \
        notrec/notrec.hrb bball/bball.hrb invader/invader.hrb \
        calc/calc.hrb tview/tview.hrb mmlplay/mmlplay.hrb gview/gview.hrb
    $(EDIMG)   imgin:../../z_tools/fdimg0at.tek \
        wbinimg src:haribote/ipl20.bin len:512 from:0 to:0 \
        copy from:haribote/haribote.sys to:@: \
        copy from:haribote/ipl20.nas to:@: \
        copy from:make.bat to:@: \
        copy from:a/a.hrb to:@: \
        copy from:hello3/hello3.hrb to:@: \
        copy from:hello4/hello4.hrb to:@: \
        copy from:hello5/hello5.hrb to:@: \
        copy from:winhelo/winhelo.hrb to:@: \
        copy from:winhelo2/winhelo2.hrb to:@: \
        copy from:winhelo3/winhelo3.hrb to:@: \
        copy from:star1/star1.hrb to:@: \
        copy from:stars/stars.hrb to:@: \
        copy from:stars2/stars2.hrb to:@: \
        copy from:lines/lines.hrb to:@: \
        copy from:walk/walk.hrb to:@: \
        copy from:noodle/noodle.hrb to:@: \
        copy from:beepdown/beepdown.hrb to:@: \
        copy from:color/color.hrb to:@: \
        copy from:color2/color2.hrb to:@: \
        copy from:sosu/sosu.hrb to:@: \
        copy from:sosu2/sosu2.hrb to:@: \
        copy from:sosu3/sosu3.hrb to:@: \
        copy from:type/type.hrb to:@: \
        copy from:iroha/iroha.hrb to:@: \
        copy from:chklang/chklang.hrb to:@: \
        copy from:euc.txt to:@: \
        copy from:notrec/notrec.hrb to:@: \
        copy from:bball/bball.hrb to:@: \
        copy from:invader/invader.hrb to:@: \
        copy from:calc/calc.hrb to:@: \
        copy from:tview/tview.hrb to:@: \
        copy from:mmlplay/mmlplay.hrb to:@: \
        copy from:mmldata/kirakira.mml to:@: \
        copy from:mmldata/fujisan.mml to:@: \
        copy from:mmldata/daigo.mml to:@: \
        copy from:mmldata/daiku.mml to:@: \
        copy from:gview/gview.hrb to:@: \
        copy from:pictdata/fujisan.jpg to:@: \
        copy from:pictdata/night.bmp to:@: \
        copy from:nihongo/nihongo.fnt to:@: \
        imgout:haribote.img

# command

run :
    $(MAKE) haribote.img
    $(COPY) haribote.img ..\..\z_tools\qemu\fdimage0.bin
    $(MAKE) -C ..\..\z_tools\qemu

install :
    $(MAKE) haribote.img
    $(IMGTOL) w a: haribote.img

full :
    $(MAKE) -C haribote
    $(MAKE) -C apilib
    $(MAKE) -C a
    $(MAKE) -C hello3
    $(MAKE) -C hello4
    $(MAKE) -C hello5
    $(MAKE) -C winhelo
    $(MAKE) -C winhelo2
    $(MAKE) -C winhelo3
    $(MAKE) -C star1
    $(MAKE) -C stars
    $(MAKE) -C stars2
    $(MAKE) -C lines
    $(MAKE) -C walk
    $(MAKE) -C noodle
    $(MAKE) -C beepdown
    $(MAKE) -C color
    $(MAKE) -C color2
    $(MAKE) -C sosu
    $(MAKE) -C sosu2
    $(MAKE) -C sosu3
    $(MAKE) -C type
    $(MAKE) -C iroha
    $(MAKE) -C chklang
    $(MAKE) -C notrec
    $(MAKE) -C bball
    $(MAKE) -C invader
    $(MAKE) -C calc
    $(MAKE) -C tview
    $(MAKE) -C mmlplay
    $(MAKE) -C gview
    $(MAKE) haribote.img

run_full :
    $(MAKE) full
    $(COPY) haribote.img ..\..\z_tools\qemu\fdimage0.bin
    $(MAKE) -C ..\..\z_tools\qemu

install_full :
    $(MAKE) full
    $(IMGTOL) w a: haribote.img

run_os :
    $(MAKE) -C haribote
    $(MAKE) run

clean :
# 何もしない

src_only :
    $(MAKE) clean
    -$(DEL) haribote.img
    
clean_full :
    $(MAKE) -C haribote     clean
    $(MAKE) -C apilib       clean
    $(MAKE) -C a            clean
    $(MAKE) -C hello3       clean
    $(MAKE) -C hello4       clean
    $(MAKE) -C hello5       clean
    $(MAKE) -C winhelo      clean
    $(MAKE) -C winhelo2     clean
    $(MAKE) -C winhelo3     clean
    $(MAKE) -C star1        clean
    $(MAKE) -C stars        clean
    $(MAKE) -C stars2       clean
    $(MAKE) -C lines        clean
    $(MAKE) -C walk         clean   
    $(MAKE) -C noodle       clean
    $(MAKE) -C beepdown     clean
    $(MAKE) -C color        clean
    $(MAKE) -C color2       clean
    $(MAKE) -C sosu         clean
    $(MAKE) -C sosu2        clean
    $(MAKE) -C sosu3        clean
    $(MAKE) -C type         clean
    $(MAKE) -C iroha        clean
    $(MAKE) -C chklang      clean
    $(MAKE) -C notrec       clean
    $(MAKE) -C bball        clean
    $(MAKE) -C invader      clean
    $(MAKE) -C calc         clean
    $(MAKE) -C tview        clean
    $(MAKE) -C mmlplay      clean
    $(MAKE) -C gview        clean

src_only_full :
    $(MAKE) -C haribote     src_only
    $(MAKE) -C apilib       src_only
    $(MAKE) -C a            src_only
    $(MAKE) -C hello3       src_only
    $(MAKE) -C hello4       src_only
    $(MAKE) -C hello5       src_only
    $(MAKE) -C winhelo      src_only
    $(MAKE) -C winhelo2     src_only
    $(MAKE) -C winhelo3     src_only
    $(MAKE) -C star1        src_only
    $(MAKE) -C stars        src_only
    $(MAKE) -C stars2       src_only
    $(MAKE) -C lines        src_only
    $(MAKE) -C walk         src_only    
    $(MAKE) -C noodle       src_only
    $(MAKE) -C beepdown     src_only
    $(MAKE) -C color        src_only
    $(MAKE) -C color2       src_only
    $(MAKE) -C sosu         src_only
    $(MAKE) -C sosu2        src_only
    $(MAKE) -C sosu3        src_only
    $(MAKE) -C type         src_only
    $(MAKE) -C iroha        src_only
    $(MAKE) -C chklang      src_only
    $(MAKE) -C notrec       src_only
    $(MAKE) -C bball        src_only
    $(MAKE) -C invader      src_only
    $(MAKE) -C calc         src_only
    $(MAKE) -C tview        src_only
    $(MAKE) -C mmlplay      src_only
    $(MAKE) -C gview        src_only
    -$(DEL) haribote.img

refresh :
    $(MAKE) full
    $(MAKE) clean_full
    -$(DEL) haribote.img

実装過程
・ 9511499fa63c8d419d2ab85490516a65efb270b7