Subversion Repositories shark

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Project: HARTIK (HA-rd R-eal TI-me K-ernel)
 *
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
 *               Gerardo Lamastra <gerardo@sssup.it>
 *
 * Authors     : Luca Abeni <luca@hartik.sssup.it>
 *               Massimiliano Giorgi <massy@hartik.sssup.it>
 * (see authors.txt for full list of hartik's authors)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://hartik.sssup.it 
 */

/*
 * Copyright (C) 1999 Luca Abeni and Massimiliano Giorgi
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * CVS :        $Id: gphoto.ok,v 1.1.1.1 2004-05-24 18:03:44 giacomo Exp $
 *
 * File:        $File$
 * Revision:    $Revision: 1.1.1.1 $
 * Last update: $Date: 2004-05-24 18:03:44 $
 */

#include "config.h"

#include <kernel/func.h>
#include <kernel/model.h>
#include <kernel/const.h>

#include <drivers/glib.h>

#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

#include "mutex.h"
#include "gphoto.h"

#ifdef FULLCOLOR

/* 16bits format 5:6:5 */
/* 24bits format 8:8:8 */
static void down24to16(WORD *dst, BYTE *src, int dx, int dy)
{
  int x,y;
  /* row must be multiple of 4!!! (there are 3 bytes per pixel!)*/
  dst+=dx*(dy+1);
  for (y=0;y<dy;y++) {
    dst-=dx*2;
    for (x=0;x<dx;x++,src+=3,dst++)
      /* blue green red */
      *dst=((src[0]&0xf8)>>3)|((src[1]&0xfc)<<3)|((src[2]&0xf8)<<8);
  }
}

#define downscaleimage(x,y,z,k) down24to16((WORD*)x,y,z,k)

#else

static void down24to8(BYTE *dst, BYTE *src, int dx, int dy)
{
  int x,y;

  dst+=dx*(dy+1);
  for (y=0;y<dy;y++) {
    dst-=dx*2;
    for (x=0;x<dx;x++,src+=3,dst++)
      *dst=(((int)src[0])*11+((int)src[1])*59+((int)src[2])*30)/100;
  }
}

#define downscaleimage(x,y,z,k) down24to8(x,y,z,k)

#endif

/*
 *
 */

extern void draw_frame(int x, int y, int dx, int dy);

int gphoto_show(char *pathname, int x, int y)
{
  struct BITMAPFILEHEADER *bf;
  struct BITMAPINFOHEADER *bi;
  BYTE *src,*dst;
  long l;
  int h,n;
  int dx,dy;
  
  bf=(struct BITMAPFILEHEADER *)malloc(sizeof(struct BITMAPFILEHEADER));
  bi=(struct BITMAPINFOHEADER *)malloc(sizeof(struct BITMAPINFOHEADER));

  if (bf==NULL||bi==NULL) return -1;

  h=open(pathname,O_RDONLY);
  if (h==-1) {
    free(bf);
    free(bi);
    return -2;
  }

  n=read(h,bf,sizeof(struct BITMAPFILEHEADER));
  if (n!=sizeof(struct BITMAPFILEHEADER)) {
    close(h);
    free(bf);
    free(bi);
    return -4;
  }

  n=read(h,bi,sizeof(struct BITMAPINFOHEADER));
  if (n!=sizeof(struct BITMAPINFOHEADER)) {
    close(h);
    free(bf);
    free(bi);
    return -4;
  }
  
  //grx_close();

  /*
  cprintf("type: %c %c\n",bf->bfType&0xff,bf->bfType>>8);
  cprintf("size: %li\n",bf->bfSize);
  cprintf("tell: %li\n\n",bf->bfOffBits);

  cprintf("bitcoutn: %i\n",bi->biBitCount);
  cprintf("compress: %li\n",bi->biCompression);
  cprintf("dx: %li\n",bi->biWidth);
  cprintf("dy: %li\n",bi->biHeight);
  */
  
  //sys_end();
  //return 0;
  
  if (bf->bfType!='B'+256*'M'||
      bi->biBitCount!=24||
      bi->biCompression!=0||
      bi->biWidth%4!=0) {
    close(h);
    free(bf);
    free(bi);
    return -5;
  }
  
  dx=bi->biWidth;
  dy=bi->biHeight;
  
  src=(BYTE*)malloc(dx*dy*3);
  if (src==NULL) {
    close(h);
    free(bf);
    free(bi);
    return -6;
  }

  dst=(BYTE*)malloc(dx*dy*2);
  if (dst==NULL) {
    free(src);
    close(h);
    free(bf);
    free(bi);
    return -6;
  }

  l=lseek(h,bf->bfOffBits,SEEK_SET);
  if (l!=bf->bfOffBits) {
    free(dst);
    free(src);
    close(h);
    free(bf);
    free(bi);
    return -7;
  }

  n=read(h,src,dx*dy*3);
  if (n!=dx*dy*3) {
    free(dst);
    free(src);
    close(h);
    free(bf);
    free(bi);
    return -8;
  }

  downscaleimage(dst,src,dx,dy);
  
#ifndef NOGRX
  draw_frame(x,y,dx,dy);
  grxlock();
  grx_putimage(x, y, x+dx-1, y+dy-1, dst);
  grxunlock();
#endif

  free(dst);
  free(src);
  close(h);
  free(bi);
  free(bf);

  return 0;
}