/* -*- Mode: C -*-
 *
 * $Header: /home/gene/library/website/docsrc/vwu/src/RCS/frame.c,v 395.1 2008/04/20 17:25:48 gene Exp $
 *
 * Copyright (c) 2006 Gene Michael Stover.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include "this.h"

/*
 */
Frame *
FRAME_Destroy (Frame *frame)
{
  if (frame != NULL) {
    if (frame->raw != NULL) frame->raw = xfree (frame->raw);
    frame = (Frame *) xfree (frame);
  }
  return NULL;
}

/*
 */
Frame *
FRAME_Load (char pathname[])
{
  Frame *frame = NULL;
  struct stat ss;
  FILE *fp;

  if (stat (pathname, &ss) == 0) {
    fp = fopen (pathname, "rb");
    if (fp != NULL) {
      frame = (Frame *) xmalloc (sizeof *frame);
      if (frame != NULL) {
        strncpy (frame->pathname, pathname, sizeof frame->pathname);
        frame->raw = xmalloc (ss.st_size);
        if (frame->raw != NULL) {
          if (fread (frame->raw, ss.st_size, 1, fp) == 1) {
            frame->bmfh = (BITMAPFILEHEADER *) &frame->raw[0];
            frame->bmi = (BITMAPINFO *) &frame->raw[sizeof *frame->bmfh];
            frame->bmih = &frame->bmi->bmiHeader;
            frame->rgba = frame->bmi->bmiColors;
            frame->pixels = &frame->raw[frame->bmfh->bfOffBits];
            LOG_DumpBitmapInfoHeader (frame->bmih);
          } else {
            /* fread failed */
            frame->raw = xfree (frame->raw);
            frame = (Frame *) xfree (frame);
          }
        } else {
          /* xmalloc failed */
          frame = (Frame *) xfree (frame);
        }
      } else {
        /* xmalloc failed */
      }
      fclose (fp);
    } else {
      LOG_LastError ("fopen", __FILE__, __LINE__);
      printf ("\n%s:%d: fopen: %s", __FILE__, __LINE__, strerror (errno));
      printf ("%s:%d: Can't open \"%s\" for binary input", __FILE__,
              __LINE__, pathname);
    }      
  } else {
    LOG_LastError ("stat", __FILE__, __LINE__);
    printf ("\n%s:%d: stat: %s", __FILE__, __LINE__, strerror (errno));
  }
  return frame;
}
/*
 */
static int
S_ReadPathname (char pathname[PATH_MAX + 1])
{
  int rc = 0;

  if (fgets (pathname, PATH_MAX + 1, stdin)) {
    while (strlen (pathname) > 0 &&
           (pathname[strlen(pathname)-1] == '\r' ||
            pathname[strlen(pathname)-1] == '\n'))
      pathname[strlen(pathname)-1] = '\0';
    if (strlen (pathname) > 0) {
      /* good */
    } else {
      rc = 44;
    }
  } else if (feof (stdin)) {
    /* Normal end of file.  Not an error. */
    rc = -1;
  } else {
    LOG_LastError ("fgets", __FILE__, __LINE__);
    printf ("\n%s:%d: fgets: %s", __FILE__, __LINE__, strerror (errno));
    rc = 43;
  }
  return rc;
}

/*
 */
Frame *
FRAME_Next ()
{
  Frame *frame = NULL;
  char pathname[PATH_MAX + 1];

  if (S_ReadPathname (pathname) == 0) {
    frame = FRAME_Load (pathname);
  } else {
    /*
     * Probably no more pathnames, so no more frames.
     */
  }
  return frame;
}

/* --- end of file --- */
