Subversion Repositories shark

Rev

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

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.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.c,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
#define COLORFG rgb16(255,255,255)
#define COLORBG rgb16(0,0,0)
#else
#define COLORFG 255
#define COLORBG 0
#endif

#ifdef FULLCOLOR

/* 16bits format 5:6:5 */
/* 24bits format 8:8:8 */
static void down24to16(int h ,
                       WORD *dst, BYTE *src,
                       int xs, int ys, int dx, int dy)
{
  WORD *pdst;
  BYTE *psrc;
  int x,y;
  int n;
 
  /* row must be multiple of 4!!! (there are 3 bytes per pixel!)*/
 
  for (y=0;y<dy;y++) {
    psrc=src;
    pdst=dst;

    n=read(h,src,dx*3);
    if (n!=dx*3) return;
   
    for (x=0;x<dx;x++,psrc+=3,pdst++)
      /* blue green red */
      *pdst=((psrc[0]&0xf8)>>3)|((psrc[1]&0xfc)<<3)|((psrc[2]&0xf8)<<8);

#ifndef NOGRX
    grxlock();
    grx_putimage(xs, ys+dy-y-1, xs+dx-1, ys+dy-y-1, dst);
    grxunlock();
#endif

  }
 
}

#define downscaleimage(h,x,y,a,b,c,d) down24to16(h,(WORD*)x,y,a,b,c,d)

#else

static void down24to8(int h,
                      BYTE *dst, BYTE *src,
                      int xs, int ys, int dx, int dy)
{
  BYTE *pdst;
  BYTE *psrc;
  int x,y;
  int n;
 
  /* row must be multiple of 4!!! (there are 3 bytes per pixel!)*/
 
  for (y=0;y<dy;y++) {
    psrc=src;
    pdst=dst;

    n=read(h,src,dx*3);
    if (n!=dx*3) return;
   
    for (x=0;x<dx;x++,psrc+=3,pdst++)
      *pdst=(((int)psrc[0])*11+((int)psrc[1])*59+((int)psrc[2])*30)/100;

#ifndef NOGRX
    grxlock();
    grx_putimage(xs, ys+dy-y-1, xs+dx-1, ys+dy-y-1, dst);
    grxunlock();
#endif

  }
}

#define downscaleimage(h,x,y,a,b,c,d) down24to8(h,x,y,a,b,c,d)

#endif

/*
 *
 */


struct taskinfo {
  int h;
  BYTE *dst,*src;
  int x,y;
  int dx,dy;
};

static TASK phototask(void *arg)
{
  struct taskinfo *p=(struct taskinfo *)arg;

  downscaleimage(p->h,p->dst,p->src,p->x,p->y,p->dx,p->dy);

  free(p->dst);
  free(p->src);
  close(p->h);

  free(arg);
 
  return NULL;
}


/*
 *
 */


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

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

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

  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*3);
  if (src==NULL) {
    close(h);
    free(bf);
    free(bi);
    return -6;
  }

  dst=(BYTE*)malloc(dx*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;
  }

#ifndef NOGRX
  draw_frame(x,y,dx,dy);
  grxlock();
  grx_text(title,x,y-14,COLORFG,COLORBG);
  grxunlock();  
#endif

  free(bi);
  free(bf);

  info=(struct taskinfo *)malloc(sizeof(struct taskinfo));
  if (info==NULL) {
    free(dst);
    free(src);
    close(h);
    return -8;
  }
  info->h=h;
  info->src=src;
  info->dst=dst;
  info->x=x;
  info->y=y;
  info->dx=dx;
  info->dy=dy;
 
  nrt_task_default_model(model);
  nrt_task_def_arg(model,info);
  pid=task_create("Photo",phototask,&model,NULL);
 
  return pid;
}