
/*                                                                       */
/*              へらへらアニメ to TIFF コンバータ                        */
/*                     Fri Feb  3 03:43:45 JST 1995    a.higuchi         */
/*                                                                       */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "hel2tif.h"


static int read_block(int fd, unsigned char *buffer, int n)
{
    int a;
    do {
        a = read(fd, buffer, n);
        if (a == 0) return 0;
        buffer += a;
        n -= a;
    } while (n > 0);
    return 1;
}

static unsigned char buffer_prev[SIZE];

static void page_diff_table_init()
{
    int j;
    for (j=0; j<SIZE; j++) buffer_prev[j] = 0;
}

static int page_diff(unsigned char *buffer)
{
    int j;
    int ret = 0;
    for (j=0; j<SIZE; j++){
        ret |= buffer[j];
        buffer[j] = buffer[j] ^ buffer_prev[j];
        buffer_prev[j] = buffer[j];
    }
    return ret;
}

static int load_and_output(int fd, char *output_filename_header,
                           int colors, int omit)
{
    int page_count;
    unsigned char buffer[SIZE*32];
    int i=0, j, s;
    
    if (read_block(fd, buffer, 12) == 0) return -1;
    if (strncmp("he1", buffer, 3) != 0) return -2;

    page_count =  buffer[8];
    page_count += buffer[9]  <<  8;
    page_count += buffer[11] << 16;
    page_count += buffer[12] << 24;
    page_count++;

    page_diff_table_init();
    do {
        if (i+32 < page_count){
            if (read_block(fd, buffer, SIZE*32) == 0) return -7;
            s = 32;
        }
        else {
            if (read_block(fd, buffer, (page_count-i)*SIZE) == 0) return -7;
            s = page_count-i;
        }
        for (j=0; j<s; j++){
            if ((page_diff(buffer+j*SIZE)) || (omit == 0))
                output_a_helahela_page(output_filename_header, i+j,
                                       buffer+j*SIZE, colors);
        }
        i += s;
    } while (i < page_count);
    return 0;
}

int main(int argc, char *argv[])
{
    int fd;
    int arg_count = 1;
    char *a;
    int colors = 2;
    char file_name[1024] = "";                        /* 入力ファイル名 */
    char output_filename_header[1024] = ""; /* 出力ファイル名の頭の部分 */
    int filename_size_5 = 0;  /* 出力ファイル名を８文字に収めるかどうか */
    int omit = 0;             /* 前のフレームから変化していないフレー   */
                              /*  ムは出力しないとき１  */

#ifdef towns_os
    filename_size_5 = 1;
#endif
    
    while (arg_count < argc){
        if (strcmp(argv[arg_count], "-o") == 0){
            strcpy(output_filename_header, argv[++arg_count]);
        }
        else if (strcmp(argv[arg_count], "-2") == 0){
            colors = 2;
        }
        else if (strcmp(argv[arg_count], "-16") == 0){
            colors = 16;
        }
        else if (strcmp(argv[arg_count], "-256") == 0){
            colors = 256;
        }
        else if (strcmp(argv[arg_count], "-32768") == 0){
            colors = 32768;
        }
        else if (strcmp(argv[arg_count], "-omit") == 0){
            omit = 1;
        }
        else if (strcmp(argv[arg_count], "-filename-size-5") == 0){
            filename_size_5 = 1;
        }
        else {
            strcpy(file_name, argv[arg_count]);
        }
        arg_count++;
    }
    if (strlen(file_name) == 0){
        printf("Usage : hel2tif [options] filename.hel\n");
        printf("      -o output_filename\n");
        printf("      -2\n");
        printf("      -32768\n");
        printf("      -omit\n");
        return -1;
    }
    if (strlen(output_filename_header) == 0){
        strcpy(output_filename_header, file_name);
        a = strchr(output_filename_header, '.');
        if (a != NULL)
            (*a) = 0;                              /* サフィックスを取る */
        if (filename_size_5 && (strlen(output_filename_header) > 5))
            output_filename_header[5] = 0; /* ５文字以内になるように切る */
    }
    
    fd = open(file_name, O_RDONLY);
    if (fd == -1) exit(-1);
    
    load_and_output(fd, output_filename_header, colors, omit);
    
    if (fd != 0) close(fd);
    return 0;
}

