X-Git-Url: https://git.cinelerra-gg.org/git/?p=goodguy%2Fcinelerra.git;a=blobdiff_plain;f=cinelerra-5.1%2Fcinelerra%2Ffilegif.C;h=6462fdca8af1a9e093a299e8f00ca0f82951d497;hp=d694ff87e15b5a3145750b79063a2c9cdb945c99;hb=cb1a1530246ad67fb9be9aa2dbba5b88eb63e933;hpb=7fd85fb66168f6b518c5f2d73e04036e87faa0e1 diff --git a/cinelerra-5.1/cinelerra/filegif.C b/cinelerra-5.1/cinelerra/filegif.C index d694ff87..6462fdca 100644 --- a/cinelerra-5.1/cinelerra/filegif.C +++ b/cinelerra-5.1/cinelerra/filegif.C @@ -27,53 +27,46 @@ #include "mainerror.h" #include "interlacemodes.h" #include "vframe.h" -#include - - -static int gif_err = 0; -#define GIF_ERR ,&gif_err -#define GifErrorString(s) Gif##ErrorString(gif_err) -#define GifLastError(s) gif_err -const char *gifErrorString() -{ - static char msg[32]; - snprintf(msg, sizeof(msg), "Gif Error %d", GifLastError()); - return msg; -} +#include +#include +#include +#include FileGIF::FileGIF(Asset *asset, File *file) - : FileList(asset, file, "GIFLIST", ".gif", FILE_GIF, FILE_GIF_LIST) + : FileBase(asset, file) { + offset = 0; + err = 0; + gif_file = 0; + eof = 1; + row_size = 0; + rows = 0; + depth = 8; + fp = 0; + fd = -1; + writes = -1; + buffer = 0; + bg = 0; + output = 0; } FileGIF::~FileGIF() { + close_file(); } int FileGIF::check_sig(Asset *asset) { FILE *stream = fopen(asset->path, "rb"); - - if(stream) - { + if( stream ) { char test[8]; int ret = fread(test, 1, 6, stream); fclose(stream); - if( ret >= 6 && - test[0] == 'G' && test[1] == 'I' && test[2] == 'F' && - test[3] == '8' && test[4] == '7' && test[5] == 'A') - { - eprintf("FileGIFF: version error (87A): \"%s\".\n", asset->path); - return 1; - } - } - - if(strlen(asset->path) > 4) - { - int len = strlen(asset->path); - if(!strncasecmp(asset->path + len - 4, ".gif", 4)) return 1; + test[0] == 'G' && test[1] == 'I' && test[2] == 'F' && + test[3] == '8' && (test[4] == '7' || test[4] == '9') && + test[5] == 'a' ) return 1; } return 0; } @@ -92,16 +85,14 @@ int FileGIF::get_best_colormodel(Asset *asset, int driver) int FileGIF::read_frame_header(char *path) { FILE *stream = fopen(path, "rb"); - - if(stream) - { + if( stream ) { unsigned char test[16]; int ret = fread(test, 16, 1, stream); fclose(stream); if( ret < 1 ) return 1; + asset->format = FILE_GIF; asset->width = test[6] | (test[7] << 8); asset->height = test[8] | (test[9] << 8); -//printf("FileGIF::read_frame_header %d %d %d\n", __LINE__, asset->width, asset->height); return 0; } @@ -109,175 +100,408 @@ int FileGIF::read_frame_header(char *path) return 1; } - -static int input_func(GifFileType *gif_file, GifByteType *buffer, int bytes) +static int input_file(GifFileType *gif_file, GifByteType *buffer, int bytes) { FileGIF *file = (FileGIF*)gif_file->UserData; - if(file->offset + bytes > file->size) bytes = file->size - file->offset; - if(bytes > 0) - { - memcpy(buffer, file->data + file->offset, bytes); - file->offset += bytes; - } + fseek(file->fp, file->offset, SEEK_SET); + bytes = fread(buffer, 1, bytes, file->fp); + file->offset += bytes; return bytes; } -int FileGIF::read_frame(VFrame *output, VFrame *input) +int FileGIF::open_file(int rd, int wr) { - data = input->get_data(); - offset = 0; - size = input->get_compressed_size(); + return rd ? ropen_path(asset->path) : + wr ? wopen_path(asset->path) : + 0 ; +} + +int FileGIF::ropen_path(const char *path) +{ + fp = fopen(path, "r"); + int result = !fp ? 1 : 0; + if( !result ) { + offset = 0; eof = 0; + gif_file = DGifOpen(this, input_file, &err); + if( !gif_file ) { + eprintf("FileGIF::ropen_path %d: %s\n", __LINE__, GifErrorString(err)); + result = 1; + } + } + if( !result ) + result = open_gif(); + return result; +} - GifFileType *gif_file; - GifRowType *gif_buffer; - gif_file = DGifOpen(this, input_func GIF_ERR); +int FileGIF::wopen_path(const char *path) +{ + fd = open(path, O_CREAT+O_TRUNC+O_WRONLY, 0777); + int result = fd < 0 ? 1 : 0; + if( !result ) { + gif_file = EGifOpenFileHandle(fd, &err); + if( !gif_file ) { + eprintf("FileGIF::wopen_path %d: %s\n", __LINE__, GifErrorString(err)); + result = 1; + } + } + if( !result ) { + writes = 0; + } + return result; +} +int FileGIF::write_frames(VFrame ***frames, int len) +{ + int result = !gif_file ? 1 : 0; + for( int i=0; iwidth; + int height = asset->height; + int result = read_frame_header(asset->path); + if( !result ) { + asset->actual_width = asset->width; + if( width ) asset->width = width; + asset->actual_height = asset->height; + if( height ) asset->height = height; + asset->layers = 1; + if( !asset->frame_rate ) + asset->frame_rate = 10; + asset->video_data = 1; + row_size = gif_file->SWidth * sizeof(GifPixelType); + bg = (GifRowType)malloc(row_size); + for( int i=0; iSWidth; ++i ) + bg[i] = gif_file->SBackGroundColor; + rows = gif_file->SHeight; + buffer = (GifRowType*)malloc(sizeof(GifRowType) * rows); + for( int i=0; iSHeight; ++i ) { + buffer[i] = (GifRowType)malloc(row_size); + memcpy(buffer[i], bg, row_size); + } + result = scan_gif(); + asset->video_length = file_pos.size(); } - gif_buffer = (GifRowType*)malloc(sizeof(GifRowType) * gif_file->SHeight); - int row_size = gif_file->SWidth * sizeof(GifPixelType); - gif_buffer[0] = (GifRowType)malloc(row_size); + return result; +} - for(int i = 0; i < gif_file->SWidth; i++) - { - gif_buffer[0][i] = gif_file->SBackGroundColor; +int FileGIF::close_file() +{ + if( gif_file ) { + EGifCloseFile(gif_file, &err); + gif_file = 0; + } + if( fp ) { + fclose(fp); fp = 0; + } + if( fd >= 0 ) { + close(fd); fd = -1; + } + if( bg ) { free(bg); bg = 0; } + if( buffer ) { + for( int k=0; kSHeight; i++) - { - gif_buffer[i] = (GifRowType)malloc(row_size); - memcpy(gif_buffer[i], gif_buffer[0], row_size); +int FileGIF::scan_gif() +{ + int file_eof = eof; + int64_t file_offset = offset; + file_pos.remove_all(); + int image_pos = offset, ret; +// read all imgs, build file_pos index + while( (ret=read_next_image(0)) > 0 ) { + file_pos.append(image_pos); + image_pos = offset; } + eof = file_eof; + offset = file_offset; + return ret; +} + +int FileGIF::set_video_position(int64_t pos) +{ + if( !gif_file || !asset->video_length ) return 1; + int64_t sz = file_pos.size(); + eof = pos < 0 || pos >= sz ? 1 : 0; + offset = !eof ? file_pos[pos] : 0; + return 0; +} + +int FileGIF::read_frame(VFrame *output) +{ + if( !gif_file ) return 1; + for( int i=0; iSHeight; ++i ) + memcpy(buffer[i], bg, row_size); + int ret = read_next_image(output) > 0 ? 0 : 1; + return ret; +} +// ret = -1:err, 0:eof, 1:frame +int FileGIF::read_next_image(VFrame *output) +{ + int ret = 0; GifRecordType record_type; - do - { - if(DGifGetRecordType(gif_file, &record_type) == GIF_ERROR) - { - eprintf("FileGIF::read_frame %d: %s\n", __LINE__, GifErrorString()); + + while( !ret && !eof ) { + if( DGifGetRecordType(gif_file, &record_type) == GIF_ERROR ) { + err = gif_file->Error; + eprintf("FileGIF::read_frame %d: %s\n", __LINE__, GifErrorString(err)); + ret = -1; break; } - switch(record_type) - { - case IMAGE_DESC_RECORD_TYPE: - { - if(DGifGetImageDesc(gif_file) == GIF_ERROR) - { - eprintf("FileGIF::read_frame %d: %s\n", __LINE__, GifErrorString()); - break; - } - - int row = gif_file->Image.Top; - int col = gif_file->Image.Left; - int width = gif_file->Image.Width; - int height = gif_file->Image.Height; - if(gif_file->Image.Left + gif_file->Image.Width > gif_file->SWidth || - gif_file->Image.Top + gif_file->Image.Height > gif_file->SHeight) - { - DGifCloseFile(gif_file GIF_ERR); - for(int k = 0; k < gif_file->SHeight; k++) - { - free(gif_buffer[k]); - } - free(gif_buffer); - return 1; - } - - if (gif_file->Image.Interlace) - { - static int InterlacedOffset[] = { 0, 4, 2, 1 }; - static int InterlacedJumps[] = { 8, 8, 4, 2 }; + switch( record_type ) { + case IMAGE_DESC_RECORD_TYPE: { + if( DGifGetImageDesc(gif_file) == GIF_ERROR ) { + err = gif_file->Error; + eprintf("FileGIF::read_frame %d: %s\n", __LINE__, GifErrorString(err)); + break; + } + int row = gif_file->Image.Top; + int col = gif_file->Image.Left; + int width = gif_file->Image.Width; + int height = gif_file->Image.Height; + if( gif_file->Image.Left + gif_file->Image.Width > gif_file->SWidth || + gif_file->Image.Top + gif_file->Image.Height > gif_file->SHeight ) + ret = -1; + if( !ret && gif_file->Image.Interlace ) { + static int InterlacedOffset[] = { 0, 4, 2, 1 }; + static int InterlacedJumps[] = { 8, 8, 4, 2 }; /* Need to perform 4 passes on the images: */ - for (int i = 0; i < 4; i++) - { - for (int j = row + InterlacedOffset[i]; - j < row + height; - j += InterlacedJumps[i]) - { - if (DGifGetLine(gif_file, - &gif_buffer[j][col], - width) == GIF_ERROR) - { - DGifCloseFile(gif_file GIF_ERR); - for(int k = 0; k < gif_file->SHeight; k++) - { - free(gif_buffer[k]); - } - free(gif_buffer); - return 1; - } - } + for( int i=0; i<4; ++i ) { + int j = row + InterlacedOffset[i]; + for( ; !ret && jSHeight; k++) - { - free(gif_buffer[k]); - } - free(gif_buffer); - return 1; - } - } + } + else { + for( int i=0; !ret && iSBackGroundColor; - ColorMapObject *color_map = (gif_file->Image.ColorMap - ? gif_file->Image.ColorMap - : gif_file->SColorMap); - if(!color_map) - { - DGifCloseFile(gif_file GIF_ERR); - for(int k = 0; k < gif_file->SHeight; k++) - { - free(gif_buffer[k]); + ColorMapObject *color_map = 0; + if( ret > 0 ) { + color_map = gif_file->Image.ColorMap; + if( !color_map ) color_map = gif_file->SColorMap; + if( !color_map ) ret = -1; + } + if( ret > 0 && output ) { + int screen_width = gif_file->SWidth; + int screen_height = gif_file->SHeight; + for( int i=0; iget_rows()[i]; + for( int j=0; jColors[row[j]]; + *out_ptr++ = color_map_entry->Red; + *out_ptr++ = color_map_entry->Green; + *out_ptr++ = color_map_entry->Blue; + } } - free(gif_buffer); - return 1; } + return ret; +} - int screen_width = gif_file->SWidth; - int screen_height = gif_file->SHeight; - for(int i = 0; i < screen_height; i++) - { - GifRowType gif_row = gif_buffer[i]; - unsigned char *out_ptr = output->get_rows()[i]; - for(int j = 0; j < screen_width; j++) - { - GifColorType *color_map_entry = &color_map->Colors[gif_row[j]]; - *out_ptr++ = color_map_entry->Red; - *out_ptr++ = color_map_entry->Green; - *out_ptr++ = color_map_entry->Blue; +int FileGIF::write_frame(VFrame *frame) +{ + int w = frame->get_w(), h = frame->get_h(); + ColorMapObject *cmap = 0; + int cmap_sz = depth >= 0 ? 1 << depth : 0; + int64_t len = w * h * sizeof(GifByteType); + GifByteType *bfr = (GifByteType *) malloc(len); + int result = !bfr ? 1 : 0; + if( !result ) { + VFrame gbrp(w, h, BC_GBRP); + gbrp.transfer_from(frame); + if( !(cmap = GifMakeMapObject(cmap_sz, 0)) ) + result = 1; + if( !result ) { + GifByteType *gp = (GifByteType *)gbrp.get_r(); + GifByteType *bp = (GifByteType *)gbrp.get_g(); + GifByteType *rp = (GifByteType *)gbrp.get_b(); + if( GifQuantizeBuffer(w, h, &cmap_sz, rp, gp, bp, + bfr, cmap->Colors) == GIF_ERROR ) + result = 1; } } + if( !result && !writes && + EGifPutScreenDesc(gif_file, w, h, depth, 0, 0) == GIF_ERROR ) + result = 1; + if( !result && + EGifPutImageDesc(gif_file, 0, 0, w, h, 0, cmap) == GIF_ERROR ) + result = 1; + + GifByteType *bp = bfr; + for( int y=0; !result && yUserData; + VFrame *output = file->output; + long size = output->get_compressed_size(); + long alloc = output->get_compressed_allocated(); + long len = size + bytes; + if( len > alloc ) + output->allocate_compressed_data(2*size + bytes); + unsigned char *data = output->get_data() + size; + memcpy(data, bfr, bytes); + output->set_compressed_size(len); + return bytes; +} + +int FileGIF::wopen_data(VFrame *output) +{ + int result = 0; + gif_file = EGifOpen(this, write_data, &err); + if( !gif_file ) { + eprintf("FileGIF::wopen_data %d: %s\n", __LINE__, GifErrorString(err)); + result = 1; + } + if( !result ) { + output->set_compressed_size(0); + this->output = output; + writes = 0; + } + return result; +} + +FileGIFList::FileGIFList(Asset *asset, File *file) + : FileList(asset, file, "GIFLIST", ".gif", FILE_UNKNOWN, FILE_GIF_LIST) +{ +} + +FileGIFList::~FileGIFList() +{ +} - for(int k = 0; k < gif_file->SHeight; k++) - { - free(gif_buffer[k]); +int FileGIFList::check_sig(Asset *asset) +{ + FILE *stream = fopen(asset->path, "rb"); + if( stream ) { + unsigned char test[16]; + int ret = fread(test, 16, 1, stream); + fclose(stream); + if( ret < 1 ) return 1; + if( test[0] == 'G' && test[1] == 'I' && test[2] == 'F' && + test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && test[6] == 'T') + return 1; } - free(gif_buffer); - DGifCloseFile(gif_file GIF_ERR); return 0; } +int FileGIFList::colormodel_supported(int colormodel) { return BC_RGB888; } +int FileGIFList::get_best_colormodel(Asset *asset, int driver) { return BC_RGB888; } + +int FileGIFList::read_frame_header(char *path) +{ + FILE *stream = fopen(path, "rb"); + if( stream ) { + unsigned char test[16]; + int ret = fread(test, 16, 1, stream); + fclose(stream); + if( ret < 1 ) return 1; + asset->format = FILE_GIF_LIST; + asset->width = test[6] | (test[7] << 8); + asset->height = test[8] | (test[9] << 8); + return 0; + } + perror(path); + return 1; +} + +int FileGIFList::read_frame(VFrame *output, char *path) +{ + Asset *asset = new Asset(path); + FileGIF gif(asset, file); + int ret = gif.ropen_path(path); + if( !ret ) + ret = gif.read_frame(output); + asset->remove_user(); + return ret; +} + +int FileGIFList::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit) +{ + int native_cmodel = BC_RGB888; + if( frame->get_color_model() != native_cmodel ) { + GIFUnit *gif_unit = (GIFUnit *)unit; + if( !gif_unit->temp_frame ) gif_unit->temp_frame = + new VFrame(frame->get_w(), frame->get_h(), native_cmodel); + gif_unit->temp_frame->transfer_from(frame); + frame = gif_unit->temp_frame; + } + + FileGIF gif(asset, file); + int ret = gif.wopen_data(data); + if( !ret ) + ret = gif.write_frame(frame); + return ret; +} + +FrameWriterUnit* FileGIFList::new_writer_unit(FrameWriter *writer) +{ + return new GIFUnit(this, writer); +} + +GIFUnit::GIFUnit(FileGIFList *file, FrameWriter *writer) + : FrameWriterUnit(writer) +{ + this->file = file; + temp_frame = 0; +} + +GIFUnit::~GIFUnit() +{ + delete temp_frame; +} +