4 * Copyright (C) 2014 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcsignals.h"
27 #include "mainerror.h"
28 #include "interlacemodes.h"
36 FileGIF::FileGIF(Asset *asset, File *file)
37 : FileBase(asset, file)
59 int FileGIF::check_sig(Asset *asset)
61 FILE *stream = fopen(asset->path, "rb");
64 int ret = fread(test, 1, 6, stream);
67 test[0] == 'G' && test[1] == 'I' && test[2] == 'F' &&
68 test[3] == '8' && (test[4] == '7' || test[4] == '9') &&
69 test[5] == 'a' ) return 1;
74 int FileGIF::colormodel_supported(int colormodel)
79 int FileGIF::get_best_colormodel(Asset *asset, int driver)
85 int FileGIF::read_frame_header(char *path)
87 FILE *stream = fopen(path, "rb");
89 unsigned char test[16];
90 int ret = fread(test, 16, 1, stream);
92 if( ret < 1 ) return 1;
93 asset->format = FILE_GIF;
94 asset->width = test[6] | (test[7] << 8);
95 asset->height = test[8] | (test[9] << 8);
103 static int input_file(GifFileType *gif_file, GifByteType *buffer, int bytes)
105 FileGIF *file = (FileGIF*)gif_file->UserData;
106 fseek(file->fp, file->offset, SEEK_SET);
107 bytes = fread(buffer, 1, bytes, file->fp);
108 file->offset += bytes;
112 int FileGIF::open_file(int rd, int wr)
114 return rd ? ropen_path(asset->path) :
115 wr ? wopen_path(asset->path) :
119 int FileGIF::ropen_path(const char *path)
121 fp = fopen(path, "r");
122 int result = !fp ? 1 : 0;
125 gif_file = DGifOpen(this, input_file, &err);
127 eprintf("FileGIF::ropen_path %d: %s\n", __LINE__, GifErrorString(err));
136 int FileGIF::wopen_path(const char *path)
138 fd = open(path, O_CREAT+O_TRUNC+O_WRONLY, 0777);
139 int result = fd < 0 ? 1 : 0;
141 gif_file = EGifOpenFileHandle(fd, &err);
143 eprintf("FileGIF::wopen_path %d: %s\n", __LINE__, GifErrorString(err));
153 int FileGIF::write_frames(VFrame ***frames, int len)
155 int result = !gif_file ? 1 : 0;
156 for( int i=0; i<len && !result; ++i )
157 result = write_frame(frames[0][i]);
161 int FileGIF::open_gif()
163 file_pos.remove_all();
164 int width = asset->width;
165 int height = asset->height;
166 int result = read_frame_header(asset->path);
168 asset->actual_width = asset->width;
169 if( width ) asset->width = width;
170 asset->actual_height = asset->height;
171 if( height ) asset->height = height;
173 if( !asset->frame_rate )
174 asset->frame_rate = 10;
175 asset->video_data = 1;
176 row_size = gif_file->SWidth * sizeof(GifPixelType);
177 bg = (GifRowType)malloc(row_size);
178 for( int i=0; i<gif_file->SWidth; ++i )
179 bg[i] = gif_file->SBackGroundColor;
180 rows = gif_file->SHeight;
181 buffer = (GifRowType*)malloc(sizeof(GifRowType) * rows);
182 for( int i=0; i<gif_file->SHeight; ++i ) {
183 buffer[i] = (GifRowType)malloc(row_size);
184 memcpy(buffer[i], bg, row_size);
187 asset->video_length = file_pos.size();
192 int FileGIF::close_file()
195 EGifCloseFile(gif_file, &err);
204 if( bg ) { free(bg); bg = 0; }
206 for( int k=0; k<rows; ++k )
208 free(buffer); buffer = 0;
217 FileBase::close_file();
221 int FileGIF::scan_gif()
224 int64_t file_offset = offset;
225 file_pos.remove_all();
226 int image_pos = offset, ret;
227 // read all imgs, build file_pos index
228 while( (ret=read_next_image(0)) > 0 ) {
229 file_pos.append(image_pos);
233 offset = file_offset;
237 int FileGIF::set_video_position(int64_t pos)
239 if( !gif_file || !asset->video_length ) return 1;
240 int64_t sz = file_pos.size();
241 eof = pos < 0 || pos >= sz ? 1 : 0;
242 offset = !eof ? file_pos[pos] : 0;
246 int FileGIF::read_frame(VFrame *output)
248 if( !gif_file ) return 1;
249 for( int i=0; i<gif_file->SHeight; ++i )
250 memcpy(buffer[i], bg, row_size);
251 int ret = read_next_image(output) > 0 ? 0 : 1;
255 // ret = -1:err, 0:eof, 1:frame
256 int FileGIF::read_next_image(VFrame *output)
259 GifRecordType record_type;
261 while( !ret && !eof ) {
262 if( DGifGetRecordType(gif_file, &record_type) == GIF_ERROR ) {
263 err = gif_file->Error;
264 eprintf("FileGIF::read_frame %d: %s\n", __LINE__, GifErrorString(err));
269 switch( record_type ) {
270 case IMAGE_DESC_RECORD_TYPE: {
271 if( DGifGetImageDesc(gif_file) == GIF_ERROR ) {
272 err = gif_file->Error;
273 eprintf("FileGIF::read_frame %d: %s\n", __LINE__, GifErrorString(err));
276 int row = gif_file->Image.Top;
277 int col = gif_file->Image.Left;
278 int width = gif_file->Image.Width;
279 int height = gif_file->Image.Height;
280 if( gif_file->Image.Left + gif_file->Image.Width > gif_file->SWidth ||
281 gif_file->Image.Top + gif_file->Image.Height > gif_file->SHeight )
283 if( !ret && gif_file->Image.Interlace ) {
284 static int InterlacedOffset[] = { 0, 4, 2, 1 };
285 static int InterlacedJumps[] = { 8, 8, 4, 2 };
286 /* Need to perform 4 passes on the images: */
287 for( int i=0; i<4; ++i ) {
288 int j = row + InterlacedOffset[i];
289 for( ; !ret && j<row + height; j+=InterlacedJumps[i] ) {
290 if( DGifGetLine(gif_file, &buffer[j][col], width) == GIF_ERROR )
296 for( int i=0; !ret && i<height; ++i ) {
297 if (DGifGetLine(gif_file, &buffer[row++][col], width) == GIF_ERROR)
303 case EXTENSION_RECORD_TYPE: {
305 GifByteType *ExtData = 0;
306 if( DGifGetExtension(gif_file, &ExtFunction, &ExtData) == GIF_ERROR )
308 while( !ret && ExtData ) {
309 if( DGifGetExtensionNext(gif_file, &ExtData) == GIF_ERROR )
313 case TERMINATE_RECORD_TYPE:
322 ColorMapObject *color_map = 0;
324 color_map = gif_file->Image.ColorMap;
325 if( !color_map ) color_map = gif_file->SColorMap;
326 if( !color_map ) ret = -1;
328 if( ret > 0 && output ) {
329 int screen_width = gif_file->SWidth;
330 int screen_height = gif_file->SHeight;
331 for( int i=0; i<screen_height; ++i ) {
332 GifRowType row = buffer[i];
333 unsigned char *out_ptr = output->get_rows()[i];
334 for( int j=0; j<screen_width; ++j ) {
335 GifColorType *color_map_entry = &color_map->Colors[row[j]];
336 *out_ptr++ = color_map_entry->Red;
337 *out_ptr++ = color_map_entry->Green;
338 *out_ptr++ = color_map_entry->Blue;
345 int FileGIF::write_frame(VFrame *frame)
347 int w = frame->get_w(), h = frame->get_h();
348 ColorMapObject *cmap = 0;
349 int cmap_sz = depth >= 0 ? 1 << depth : 0;
350 int64_t len = w * h * sizeof(GifByteType);
351 GifByteType *bfr = (GifByteType *) malloc(len);
352 int result = !bfr ? 1 : 0;
354 VFrame gbrp(w, h, BC_GBRP);
355 gbrp.transfer_from(frame);
356 if( !(cmap = GifMakeMapObject(cmap_sz, 0)) )
359 GifByteType *gp = (GifByteType *)gbrp.get_r();
360 GifByteType *bp = (GifByteType *)gbrp.get_g();
361 GifByteType *rp = (GifByteType *)gbrp.get_b();
362 if( GifQuantizeBuffer(w, h, &cmap_sz, rp, gp, bp,
363 bfr, cmap->Colors) == GIF_ERROR )
367 if( !result && !writes &&
368 EGifPutScreenDesc(gif_file, w, h, depth, 0, 0) == GIF_ERROR )
371 EGifPutImageDesc(gif_file, 0, 0, w, h, 0, cmap) == GIF_ERROR )
374 GifByteType *bp = bfr;
375 for( int y=0; !result && y<h; ++y ) {
376 if( EGifPutLine(gif_file, bp, w) == GIF_ERROR )
380 GifFreeMapObject(cmap);
386 static int write_data(GifFileType *gif_file, const GifByteType *bfr, int bytes)
388 FileGIF *file = (FileGIF*)gif_file->UserData;
389 VFrame *output = file->output;
390 long size = output->get_compressed_size();
391 long alloc = output->get_compressed_allocated();
392 long len = size + bytes;
394 output->allocate_compressed_data(2*size + bytes);
395 unsigned char *data = output->get_data() + size;
396 memcpy(data, bfr, bytes);
397 output->set_compressed_size(len);
401 int FileGIF::wopen_data(VFrame *output)
404 gif_file = EGifOpen(this, write_data, &err);
406 eprintf("FileGIF::wopen_data %d: %s\n", __LINE__, GifErrorString(err));
410 output->set_compressed_size(0);
411 this->output = output;
418 FileGIFList::FileGIFList(Asset *asset, File *file)
419 : FileList(asset, file, "GIFLIST", ".gif", FILE_UNKNOWN, FILE_GIF_LIST)
423 FileGIFList::~FileGIFList()
427 int FileGIFList::check_sig(Asset *asset)
429 FILE *stream = fopen(asset->path, "rb");
431 unsigned char test[16];
432 int ret = fread(test, 16, 1, stream);
434 if( ret < 1 ) return 1;
435 if( test[0] == 'G' && test[1] == 'I' && test[2] == 'F' &&
436 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && test[6] == 'T')
442 int FileGIFList::colormodel_supported(int colormodel) { return BC_RGB888; }
443 int FileGIFList::get_best_colormodel(Asset *asset, int driver) { return BC_RGB888; }
445 int FileGIFList::read_frame_header(char *path)
447 FILE *stream = fopen(path, "rb");
449 unsigned char test[16];
450 int ret = fread(test, 16, 1, stream);
452 if( ret < 1 ) return 1;
453 asset->format = FILE_GIF_LIST;
454 asset->width = test[6] | (test[7] << 8);
455 asset->height = test[8] | (test[9] << 8);
462 int FileGIFList::read_frame(VFrame *output, char *path)
464 Asset *asset = new Asset(path);
465 FileGIF gif(asset, file);
466 int ret = gif.ropen_path(path);
468 ret = gif.read_frame(output);
469 asset->remove_user();
473 int FileGIFList::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
475 int native_cmodel = BC_RGB888;
476 if( frame->get_color_model() != native_cmodel ) {
477 GIFUnit *gif_unit = (GIFUnit *)unit;
478 if( !gif_unit->temp_frame ) gif_unit->temp_frame =
479 new VFrame(frame->get_w(), frame->get_h(), native_cmodel);
480 gif_unit->temp_frame->transfer_from(frame);
481 frame = gif_unit->temp_frame;
484 FileGIF gif(asset, file);
485 int ret = gif.wopen_data(data);
487 ret = gif.write_frame(frame);
491 FrameWriterUnit* FileGIFList::new_writer_unit(FrameWriter *writer)
493 return new GIFUnit(this, writer);
496 GIFUnit::GIFUnit(FileGIFList *file, FrameWriter *writer)
497 : FrameWriterUnit(writer)