Subversion Repositories shark

Rev

Rev 1655 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1655 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/*
20
 * Copyright (C) 1999 Luca Abeni and Massimiliano Giorgi
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 *
36
 */
37
 
38
/*
39
 * CVS :        $Id: gphoto.c,v 1.1.1.1 2004-05-24 18:03:44 giacomo Exp $
40
 *
41
 * File:        $File$
42
 * Revision:    $Revision: 1.1.1.1 $
43
 * Last update: $Date: 2004-05-24 18:03:44 $
44
 */
45
 
46
#include "config.h"
47
 
48
#include <kernel/func.h>
49
#include <kernel/model.h>
50
#include <kernel/const.h>
51
 
52
#include <drivers/glib.h>
53
 
54
#include <unistd.h>
55
#include <stdlib.h>
56
#include <fcntl.h>
57
 
58
#include "mutex.h"
59
#include "gphoto.h"
60
 
61
#ifdef FULLCOLOR
62
#define COLORFG rgb16(255,255,255)
63
#define COLORBG rgb16(0,0,0)
64
#else
65
#define COLORFG 255
66
#define COLORBG 0
67
#endif
68
 
69
#ifdef FULLCOLOR
70
 
71
/* 16bits format 5:6:5 */
72
/* 24bits format 8:8:8 */
73
static void down24to16(int h ,
74
                       WORD *dst, BYTE *src,
75
                       int xs, int ys, int dx, int dy)
76
{
77
  WORD *pdst;
78
  BYTE *psrc;
79
  int x,y;
80
  int n;
81
 
82
  /* row must be multiple of 4!!! (there are 3 bytes per pixel!)*/
83
 
84
  for (y=0;y<dy;y++) {
85
    psrc=src;
86
    pdst=dst;
87
 
88
    n=read(h,src,dx*3);
89
    if (n!=dx*3) return;
90
 
91
    for (x=0;x<dx;x++,psrc+=3,pdst++)
92
      /* blue green red */
93
      *pdst=((psrc[0]&0xf8)>>3)|((psrc[1]&0xfc)<<3)|((psrc[2]&0xf8)<<8);
94
 
95
#ifndef NOGRX
96
    grxlock();
97
    grx_putimage(xs, ys+dy-y-1, xs+dx-1, ys+dy-y-1, dst);
98
    grxunlock();
99
#endif
100
 
101
  }
102
 
103
}
104
 
105
#define downscaleimage(h,x,y,a,b,c,d) down24to16(h,(WORD*)x,y,a,b,c,d)
106
 
107
#else
108
 
109
static void down24to8(int h,
110
                      BYTE *dst, BYTE *src,
111
                      int xs, int ys, int dx, int dy)
112
{
113
  BYTE *pdst;
114
  BYTE *psrc;
115
  int x,y;
116
  int n;
117
 
118
  /* row must be multiple of 4!!! (there are 3 bytes per pixel!)*/
119
 
120
  for (y=0;y<dy;y++) {
121
    psrc=src;
122
    pdst=dst;
123
 
124
    n=read(h,src,dx*3);
125
    if (n!=dx*3) return;
126
 
127
    for (x=0;x<dx;x++,psrc+=3,pdst++)
128
      *pdst=(((int)psrc[0])*11+((int)psrc[1])*59+((int)psrc[2])*30)/100;
129
 
130
#ifndef NOGRX
131
    grxlock();
132
    grx_putimage(xs, ys+dy-y-1, xs+dx-1, ys+dy-y-1, dst);
133
    grxunlock();
134
#endif
135
 
136
  }
137
}
138
 
139
#define downscaleimage(h,x,y,a,b,c,d) down24to8(h,x,y,a,b,c,d)
140
 
141
#endif
142
 
143
/*
144
 *
145
 */
146
 
147
struct taskinfo {
148
  int h;
149
  BYTE *dst,*src;
150
  int x,y;
151
  int dx,dy;
152
};
153
 
154
static TASK phototask(void *arg)
155
{
156
  struct taskinfo *p=(struct taskinfo *)arg;
157
 
158
  downscaleimage(p->h,p->dst,p->src,p->x,p->y,p->dx,p->dy);
159
 
160
  free(p->dst);
161
  free(p->src);
162
  close(p->h);
163
 
164
  free(arg);
165
 
166
  return NULL;
167
}
168
 
169
 
170
/*
171
 *
172
 */
173
 
174
extern void draw_frame(int x, int y, int dx, int dy);
175
 
176
int gphoto_show(char *title, char *pathname, int x, int y)
177
{
178
  NRT_TASK_MODEL model;
179
  struct BITMAPFILEHEADER *bf;
180
  struct BITMAPINFOHEADER *bi;
181
  struct taskinfo *info;
182
  BYTE *src,*dst;
183
  long l;
184
  int h,n;
185
  int dx,dy;
186
  PID pid;
187
 
188
  bf=(struct BITMAPFILEHEADER *)malloc(sizeof(struct BITMAPFILEHEADER));
189
  bi=(struct BITMAPINFOHEADER *)malloc(sizeof(struct BITMAPINFOHEADER));
190
 
191
  if (bf==NULL||bi==NULL) return -11;
192
 
193
  h=open(pathname,O_RDONLY);
194
  if (h==-1) {
195
    free(bf);
196
    free(bi);
197
    return -2;
198
  }
199
 
200
  n=read(h,bf,sizeof(struct BITMAPFILEHEADER));
201
  if (n!=sizeof(struct BITMAPFILEHEADER)) {
202
    close(h);
203
    free(bf);
204
    free(bi);
205
    return -4;
206
  }
207
 
208
  n=read(h,bi,sizeof(struct BITMAPINFOHEADER));
209
  if (n!=sizeof(struct BITMAPINFOHEADER)) {
210
    close(h);
211
    free(bf);
212
    free(bi);
213
    return -4;
214
  }
215
 
216
  //grx_close();
217
 
218
  /*
219
    cprintf("type: %c %c\n",bf->bfType&0xff,bf->bfType>>8);
220
    cprintf("size: %li\n",bf->bfSize);
221
    cprintf("tell: %li\n\n",bf->bfOffBits);
222
 
223
    cprintf("bitcoutn: %i\n",bi->biBitCount);
224
    cprintf("compress: %li\n",bi->biCompression);
225
    cprintf("dx: %li\n",bi->biWidth);
226
    cprintf("dy: %li\n",bi->biHeight);
227
    */
228
 
229
  //sys_end();
230
  //return 0;
231
 
232
  if (bf->bfType!='B'+256*'M'||
233
      bi->biBitCount!=24||
234
      bi->biCompression!=0||
235
      bi->biWidth%4!=0) {
236
    close(h);
237
    free(bf);
238
    free(bi);
239
    return -5;
240
  }
241
 
242
  dx=bi->biWidth;
243
  dy=bi->biHeight;
244
 
245
  src=(BYTE*)malloc(dx*3);
246
  if (src==NULL) {
247
    close(h);
248
    free(bf);
249
    free(bi);
250
    return -6;
251
  }
252
 
253
  dst=(BYTE*)malloc(dx*2);
254
  if (dst==NULL) {
255
    free(src);
256
    close(h);
257
    free(bf);
258
    free(bi);
259
    return -6;
260
  }
261
 
262
  l=lseek(h,bf->bfOffBits,SEEK_SET);
263
  if (l!=bf->bfOffBits) {
264
    free(dst);
265
    free(src);
266
    close(h);
267
    free(bf);
268
    free(bi);
269
    return -7;
270
  }
271
 
272
#ifndef NOGRX
273
  draw_frame(x,y,dx,dy);
274
  grxlock();
275
  grx_text(title,x,y-14,COLORFG,COLORBG);
276
  grxunlock();  
277
#endif
278
 
279
  free(bi);
280
  free(bf);
281
 
282
  info=(struct taskinfo *)malloc(sizeof(struct taskinfo));
283
  if (info==NULL) {
284
    free(dst);
285
    free(src);
286
    close(h);
287
    return -8;
288
  }
289
  info->h=h;
290
  info->src=src;
291
  info->dst=dst;
292
  info->x=x;
293
  info->y=y;
294
  info->dx=dx;
295
  info->dy=dy;
296
 
297
  nrt_task_default_model(model);
298
  nrt_task_def_arg(model,info);
299
  pid=task_create("Photo",phototask,&model,NULL);
300
 
301
  return pid;
302
}