r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / filegif.C
blob93473025e91ed8278df6b3fa41fedfb59edbbd11
1 #include "assets.h"
2 #include "file.h"
3 #include "filetiff.h"
4 #include "vframe.h"
7 FileGIF::FileGIF(Asset *asset) : FileBase(asset)
9         reset_parameters();
10         asset->video_data = 1;
11         asset->format = FILE_GIF;
14 FileGIF::~FileGIF()
16         close_file();
19 int FileGIF::reset_parameters_derived()
21         data = 0;
24 int FileGIF::open_file(int rd, int wr)
26         this->rd = rd;
27         this->wr = wr;
29 // skip header for write
30         if(wr)
31         {
32         }
33         else
34         if(rd)
35         {
36                 return read_header();
37         }
38         return 0;
41 int64_t FileGIF::get_video_length()
43         return -1;    // infinity
44 // should be determined by whether the GIF is animated
47 int64_t FileGIF::get_memory_usage()
49 // give buffer length plus padding
50         if(data)
51                 return asset->width * asset->height * sizeof(VPixel);
52         else
53                 return 256;
56 int FileGIF::close_file_derived()
58         if(data) delete data;
59         reset_parameters();
62 int FileGIF::read_header()
64         GIF *stream;
66         if(!(stream = GIFOpen(asset->path, "r")))
67         {
68                 perror("FileGIF::read_header");
69                 return 1;
70         }
71         
72         GIFGetField(stream, GIFTAG_IMAGEWIDTH, &(asset->width));
73         GIFGetField(stream, GIFTAG_IMAGELENGTH, &(asset->height));
74         asset->layers = 1;
76         GIFClose(stream);
77         return 0;
81 VFrame* FileGIF::read_frame(int use_alpha, int use_float)
83         read_raw();
84         return data;
87 int FileGIF::read_raw()
89         if(!data)
90         {
91 // read the raw data
92                 GIF *stream;
93                 unsigned char *raw_data;
94                 int i;
96                 if(!(stream = GIFOpen(asset->path, "r")))
97                 {
98                         perror("FileGIF::read_raw");
99                         return 1;
100                 }
102                 raw_data = new unsigned char[asset->width * asset->height * 4];
103                 GIFReadRGBAImage(stream, asset->width, asset->height, (uint32*)raw_data, 0);
105                 GIFClose(stream);
107 // convert to a Bcast 2000 Frame
108                 data = new VFrame(asset->width, asset->height);
110                 for(i = 0; i < asset->height; i++)
111                 {
112                         import_row(data->rows[asset->height - i - 1], &raw_data[i * asset->width * 4]);
113                 }
115 // delete temporary buffers
116                 delete raw_data;
117         }
118         return 0;
121 int FileGIF::import_row(VPixel *output, unsigned char *row_pointer)
123         for(int i = 0, j = 0; j < asset->width; j++)
124         {
125 #if (VMAX == 65535)
126                 output[j].red =  ((VWORD)row_pointer[i++]) << 8;
127                 output[j].green =  ((VWORD)row_pointer[i++]) << 8;
128                 output[j].blue = ((VWORD)row_pointer[i++]) << 8;
129                 output[j].alpha = ((VWORD)row_pointer[i++]) << 8;
130 #else
131                 output[j].red =  (VWORD)row_pointer[i++];
132                 output[j].green =  (VWORD)row_pointer[i++];
133                 output[j].blue = (VWORD)row_pointer[i++];
134                 output[j].alpha = (VWORD)row_pointer[i++];
135 #endif
136         }