~ubuntu-branches/ubuntu/quantal/vice/quantal

« back to all changes in this revision

Viewing changes to src/gfxoutputdrv/iffdrv.c

  • Committer: Bazaar Package Importer
  • Author(s): Zed Pobre
  • Date: 2006-07-30 19:15:59 UTC
  • mto: (9.1.1 lenny) (1.1.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20060730191559-g31ymd2mk102kzff
Tags: upstream-1.19
ImportĀ upstreamĀ versionĀ 1.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * iffdrv.c - Create an Amiga IFF file.
 
3
 *
 
4
 * Written by
 
5
 *  Marco van den Heuvel <blackystardust68@yahoo.com>
 
6
 *
 
7
 * This file is part of VICE, the Versatile Commodore Emulator.
 
8
 * See README for copyright notice.
 
9
 *
 
10
 *  This program is free software; you can redistribute it and/or modify
 
11
 *  it under the terms of the GNU General Public License as published by
 
12
 *  the Free Software Foundation; either version 2 of the License, or
 
13
 *  (at your option) any later version.
 
14
 *
 
15
 *  This program is distributed in the hope that it will be useful,
 
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 *  GNU General Public License for more details.
 
19
 *
 
20
 *  You should have received a copy of the GNU General Public License
 
21
 *  along with this program; if not, write to the Free Software
 
22
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 
23
 *  02111-1307  USA.
 
24
 *
 
25
 */
 
26
 
 
27
#include "vice.h"
 
28
 
 
29
#include <stdio.h>
 
30
#include <string.h>
 
31
#include <stdlib.h>
 
32
 
 
33
#include "archdep.h"
 
34
#include "iffdrv.h"
 
35
#include "lib.h"
 
36
#include "log.h"
 
37
#include "gfxoutput.h"
 
38
#include "palette.h"
 
39
#include "screenshot.h"
 
40
#include "types.h"
 
41
#include "util.h"
 
42
 
 
43
 
 
44
typedef struct gfxoutputdrv_data_s
 
45
{
 
46
  FILE *fd;
 
47
  char *ext_filename;
 
48
  BYTE *data;
 
49
  BYTE *iff_data;
 
50
  unsigned int line;
 
51
  int iff_rowbytes;
 
52
} gfxoutputdrv_data_t;
 
53
 
 
54
static gfxoutputdrv_t iff_drv;
 
55
 
 
56
static BYTE powers[8]= { 1,2,4,8,16,32,64,128 };
 
57
 
 
58
static int iffdrv_write_file_header(screenshot_t *screenshot)
 
59
{
 
60
  gfxoutputdrv_data_t *sdata;
 
61
  BYTE header[836];
 
62
  int i;
 
63
  int totalsize;
 
64
 
 
65
  memset(header, 0, sizeof(header));
 
66
 
 
67
  sdata = screenshot->gfxoutputdrv_data;
 
68
 
 
69
  sdata->iff_rowbytes=((screenshot->width+15)>>4)<<1;
 
70
 
 
71
  totalsize=828+(sdata->iff_rowbytes*screenshot->height*8);
 
72
 
 
73
  header[0]='F';
 
74
  header[1]='O';
 
75
  header[2]='R';
 
76
  header[3]='M';
 
77
  util_dword_to_be_buf(&header[4], totalsize);
 
78
  header[8]='I';
 
79
  header[9]='L';
 
80
  header[10]='B';
 
81
  header[11]='M';
 
82
  header[12]='B';
 
83
  header[13]='M';
 
84
  header[14]='H';
 
85
  header[15]='D';
 
86
  util_dword_to_be_buf(&header[16], 20);
 
87
  util_word_to_be_buf(&header[20],screenshot->width);
 
88
  util_word_to_be_buf(&header[22],screenshot->height);
 
89
  header[28]=8;
 
90
  util_word_to_be_buf(&header[36],screenshot->width);
 
91
  util_word_to_be_buf(&header[38],screenshot->height);
 
92
  header[40]='C';
 
93
  header[41]='M';
 
94
  header[42]='A';
 
95
  header[43]='P';
 
96
  util_dword_to_be_buf(&header[44],3*256);
 
97
 
 
98
  for (i = 0; i < 256; i++)
 
99
  {
 
100
    header[48+(i*3)]=screenshot->palette->entries[i].red;
 
101
    header[49+(i*3)]=screenshot->palette->entries[i].green;
 
102
    header[50+(i*3)]=screenshot->palette->entries[i].blue;
 
103
  }
 
104
  header[816]='C';
 
105
  header[817]='A';
 
106
  header[818]='M';
 
107
  header[819]='G';
 
108
  util_dword_to_be_buf(&header[820],4);
 
109
  header[828]='B';
 
110
  header[829]='O';
 
111
  header[830]='D';
 
112
  header[831]='Y';
 
113
  
 
114
util_dword_to_be_buf(&header[832],sdata->iff_rowbytes*screenshot->height*8);
 
115
 
 
116
  if (fwrite(header,836,1,sdata->fd)<1)
 
117
    return -1;
 
118
 
 
119
  return 0;
 
120
}
 
121
 
 
122
static int iffdrv_open(screenshot_t *screenshot, const char *filename)
 
123
{
 
124
  gfxoutputdrv_data_t *sdata;
 
125
 
 
126
  if (screenshot->palette->num_entries > 256)
 
127
  {
 
128
    log_error(LOG_DEFAULT, "Max 256 colors supported.");
 
129
    return -1;
 
130
  }
 
131
 
 
132
  sdata = (gfxoutputdrv_data_t *)lib_malloc(sizeof(gfxoutputdrv_data_t));
 
133
  screenshot->gfxoutputdrv_data = sdata;
 
134
  sdata->line = 0;
 
135
  sdata->ext_filename=util_add_extension_const(filename, iff_drv.default_extension);
 
136
  sdata->fd = fopen(filename, "wb");
 
137
 
 
138
  if (sdata->fd==NULL)
 
139
  {
 
140
    lib_free(sdata->ext_filename);
 
141
    lib_free(sdata);
 
142
    return -1;
 
143
  }
 
144
 
 
145
  if (iffdrv_write_file_header(screenshot)<0)
 
146
  {
 
147
    fclose(sdata->fd);
 
148
    lib_free(sdata->ext_filename);
 
149
    lib_free(sdata);
 
150
    return -1;
 
151
  }
 
152
 
 
153
  sdata->data = (BYTE *)lib_malloc(sdata->iff_rowbytes*8);
 
154
  sdata->iff_data = (BYTE *)lib_malloc(sdata->iff_rowbytes);
 
155
 
 
156
  return 0;
 
157
}
 
158
 
 
159
static void iff_c2p(BYTE *chunky, BYTE *planar, int amount, int plane)
 
160
{
 
161
  int i;
 
162
 
 
163
  for (i=0; i<amount; i++)
 
164
  {
 
165
    planar[i]=((chunky[i*8]&powers[plane])/powers[plane]*128)+
 
166
              ((chunky[(i*8)+1]&powers[plane])/powers[plane]*64)+
 
167
              ((chunky[(i*8)+2]&powers[plane])/powers[plane]*32)+
 
168
              ((chunky[(i*8)+3]&powers[plane])/powers[plane]*16)+
 
169
              ((chunky[(i*8)+4]&powers[plane])/powers[plane]*8)+
 
170
              ((chunky[(i*8)+5]&powers[plane])/powers[plane]*4)+
 
171
              ((chunky[(i*8)+6]&powers[plane])/powers[plane]*2)+
 
172
              (chunky[(i*8)+7]&powers[plane])/powers[plane];
 
173
  }
 
174
}
 
175
 
 
176
static int iffdrv_write(screenshot_t *screenshot)
 
177
{
 
178
  gfxoutputdrv_data_t *sdata;
 
179
  int j;
 
180
 
 
181
  sdata = screenshot->gfxoutputdrv_data;
 
182
  (screenshot->convert_line)(screenshot, sdata->data, sdata->line, SCREENSHOT_MODE_PALETTE);
 
183
  for (j = 0; j<8; j++)
 
184
  {
 
185
    iff_c2p(sdata->data,sdata->iff_data,sdata->iff_rowbytes,j);
 
186
    if (fwrite(sdata->iff_data, sdata->iff_rowbytes, 1, sdata->fd)<1)
 
187
      return -1;
 
188
  }
 
189
  return 0;
 
190
}
 
191
 
 
192
static int iffdrv_close(screenshot_t *screenshot)
 
193
{
 
194
  gfxoutputdrv_data_t *sdata;
 
195
 
 
196
  sdata = screenshot->gfxoutputdrv_data;
 
197
 
 
198
  fclose(sdata->fd);
 
199
  lib_free(sdata->data);
 
200
  lib_free(sdata->iff_data);
 
201
  lib_free(sdata->ext_filename);
 
202
  lib_free(sdata);
 
203
 
 
204
  return 0;
 
205
}
 
206
 
 
207
static int iffdrv_save(screenshot_t *screenshot, const char *filename)
 
208
{
 
209
  if (iffdrv_open(screenshot, filename) < 0)
 
210
    return -1;
 
211
 
 
212
  for (screenshot->gfxoutputdrv_data->line = 0; 
 
213
       screenshot->gfxoutputdrv_data->line < screenshot->height;
 
214
       (screenshot->gfxoutputdrv_data->line)++)
 
215
  {
 
216
    iffdrv_write(screenshot);
 
217
  }
 
218
 
 
219
  if (iffdrv_close(screenshot) < 0)
 
220
    return -1;
 
221
 
 
222
  return 0;
 
223
}
 
224
 
 
225
static gfxoutputdrv_t iff_drv =
 
226
{
 
227
    "IFF",
 
228
    "IFF screenshot",
 
229
    "iff",
 
230
    iffdrv_open,
 
231
    iffdrv_close,
 
232
    iffdrv_write,
 
233
    iffdrv_save,
 
234
    NULL
 
235
};
 
236
 
 
237
void gfxoutput_init_iff(void)
 
238
{
 
239
  gfxoutput_register(&iff_drv);
 
240
}