Subversion Repositories shark

Rev

Rev 693 | 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     :
 *   Mauro Marinoni      <mauro.marinoni@unipv.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
 */


/*
 * 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
 */


#include "kernel/kern.h"
#include "string.h"
#include "drivers/shark_fb26.h"
#include "snapshot.h"

struct SNAP_SLOT{
        char used;
        int x;
        int y;
        int** buff;
} sslot[16];

int** snapshot_getslot(int nbuff, int sx, int sy)
{
        int i;
        int** tmp;

        if ((sx<=0) || (sy<=0) || (nbuff<=0) || (nbuff>15)){
                return NULL;
        }
        if (sslot[nbuff].used)
                return NULL;

        /* Allocate pointers to rows. */
        tmp = (int **) kern_alloc((unsigned) (sy)*sizeof(int*));
        if (!tmp)
                return NULL;

        /* Allocate rows and set pointers to them. */
        for (i = 0; i < sy; i++){
                tmp[i] = (int *) kern_alloc((unsigned) (sx)*sizeof(int));
                if (!tmp[i])
                        return NULL;
        }

        sslot[nbuff].used = 1;
        sslot[nbuff].x = sx;
        sslot[nbuff].y = sy;
        sslot[nbuff].buff = tmp;
        return tmp;
}

void snapshot_freeslot(int nbuff)
{
        int i;

        if (sslot[nbuff].used){
                for (i = 0; i < sslot[nbuff].y; i++)
                        kern_free(sslot[nbuff].buff[i], (unsigned) (sslot[nbuff].x)*sizeof(int));
                kern_free(sslot[nbuff].buff, (unsigned) (sslot[nbuff].y)*sizeof(int*));
                sslot[nbuff].used = 0;
        }
}

void snapshot_grab(int nbuff)
{
        int i, j;

        for (i = 0; i < sslot[nbuff].x; i++)
                for (j = 0; j < sslot[nbuff].x; j++)
                        sslot[nbuff].buff[i][j] = grx_getpixel(i,j);
}

int snapshot_save_pgm(int nbuff, char *fname)
{
        char out_st[40];
        register int i, j;
        DOS_FILE *outf;

        if ((outf = DOS_fopen(fname, "w")) == NULL) {
                return -1;
        } else {
                sprintf(out_st, "P5\n%d %d\n%d\n", sslot[nbuff].x, sslot[nbuff].y, 255);
                DOS_fwrite(out_st, strlen(out_st), 1, outf);
                cprintf(out_st);

                for (j = 0; j < sslot[nbuff].y; j++) {
                        for (i = 0; i < sslot[nbuff].x; i++) {
                                DOS_fwrite(&(sslot[nbuff].buff[i][j]), 1, 1, outf);
                        }
                }
                DOS_fclose(outf);
        }
        return 0;
}

int snapshot_save_ppm(int nbuff, char *fname)
{
        char out_st[40];
        register int i, j;
        int col_r, col_g, col_b;
        DOS_FILE *outf;

        if ((outf = DOS_fopen(fname, "w")) == NULL) {
                return -1;
        } else {
                sprintf(out_st, "P3\n%d %d\n%d\n", sslot[nbuff].x, sslot[nbuff].y, 255);
                DOS_fwrite(out_st, strlen(out_st), 1, outf);
                cprintf(out_st);

                for (j = 0; j < sslot[nbuff].y; j++) {
                        for (i = 0; i < sslot[nbuff].x; i++) {
                                col_r = ((sslot[nbuff].buff[i][j] & 0xF800) >> 11) * 4;
                                col_g = ((sslot[nbuff].buff[i][j] & 0x07E0) >>  5) * 2;
                                col_b =  (sslot[nbuff].buff[i][j] & 0x001F) * 4;
                                sprintf(out_st, "%d %d %d  ", col_r, col_g, col_b);
                                DOS_fwrite(out_st, strlen(out_st), 1, outf);
                        }
                }
                DOS_fclose(outf);
        }
        return 0;
}