Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

filetga.C

Go to the documentation of this file.
00001 #include "asset.h"
00002 #include "bcsignals.h"
00003 #include "edit.h"
00004 #include "filetga.h"
00005 #include "language.h"
00006 #include "mwindow.inc"
00007 #include "vframe.h"
00008 
00009 #include <string.h>
00010 #include <unistd.h>
00011 
00012 /* Known image types. */
00013 #define TGA_TYPE_MAPPED      1
00014 #define TGA_TYPE_COLOR       2
00015 #define TGA_TYPE_GRAY        3
00016 
00017 /* Only known compression is RLE */
00018 #define TGA_COMP_NONE        0 
00019 #define TGA_COMP_RLE         1 
00020 
00021 
00022 FileTGA::FileTGA(Asset *asset, File *file)
00023  : FileList(asset, file, "TGALIST", ".tga", FILE_TGA, FILE_TGA_LIST)
00024 {
00025         temp = 0;
00026 }
00027 
00028 FileTGA::~FileTGA()
00029 {
00030         if(temp) delete temp;
00031 }
00032 
00033 int FileTGA::check_sig(Asset *asset)
00034 {
00035 
00036 SET_TRACE
00037 // Test file extension
00038         int result = 0;
00039         char *ext = strrchr(asset->path, '.');
00040 SET_TRACE
00041         if(ext)
00042         {
00043 SET_TRACE
00044                 if(!strncasecmp(ext, ".tga", 4)) result = 1;
00045 SET_TRACE
00046         }
00047 
00048 SET_TRACE
00049 
00050 // Test for list
00051         if(!result)
00052         {
00053                 FILE *stream;
00054                 if(!(stream = fopen(asset->path, "rb")))
00055                 {
00056 // file not found
00057                         result = 0;
00058                 }
00059                 else
00060                 {
00061                         char test[16];
00062                         fread(test, 16, 1, stream);
00063                         fclose(stream);
00064                         if(test[0] == 'T' && test[1] == 'G' && test[2] == 'A' && 
00065                                 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && 
00066                                 test[6] == 'T')
00067                         {
00068                                 result = 1;
00069                         }
00070                         
00071                 }
00072         }
00073 SET_TRACE
00074 
00075         return result;
00076 }
00077 
00078 void FileTGA::get_parameters(BC_WindowBase *parent_window, 
00079                 Asset *asset, 
00080                 BC_WindowBase* &format_window,
00081                 int audio_options,
00082                 int video_options)
00083 {
00084         if(video_options)
00085         {
00086                 TGAConfigVideo *window = new TGAConfigVideo(parent_window, asset);
00087                 format_window = window;
00088                 window->create_objects();
00089                 window->run_window();
00090                 delete window;
00091         }
00092 }
00093 
00094 #if 0
00095 N_("RGB compressed")
00096 N_("RGBA compressed")
00097 N_("RGB uncompressed")
00098 N_("RGBA uncompressed")
00099 #endif
00100 
00101 #define TGA_RGB_RLE "rle "
00102 #define TGA_RGBA_RLE "rlea"
00103 #define TGA_RGB "raw "
00104 #define TGA_RGBA "rawa"
00105 
00106 #define TGA_RGB_RLE_NAME "RGB compressed"
00107 #define TGA_RGBA_RLE_NAME "RGBA compressed"
00108 #define TGA_RGB_NAME "RGB uncompressed"
00109 #define TGA_RGBA_NAME "RGBA uncompressed"
00110 
00111 char* FileTGA::compression_to_str(char *compression)
00112 {
00113         if(!strcasecmp(compression, TGA_RGB_RLE)) return _(TGA_RGB_RLE_NAME);
00114         if(!strcasecmp(compression, TGA_RGBA_RLE)) return _(TGA_RGBA_RLE_NAME);
00115         if(!strcasecmp(compression, TGA_RGB)) return _(TGA_RGB_NAME);
00116         if(!strcasecmp(compression, TGA_RGBA)) return _(TGA_RGBA_NAME);
00117         return TGA_RGB_NAME;
00118 }
00119 
00120 char* FileTGA::str_to_compression(char *string)
00121 {
00122         if(!strcasecmp(compression_to_str(TGA_RGB_RLE), string)) return TGA_RGB_RLE;
00123         if(!strcasecmp(compression_to_str(TGA_RGBA_RLE), string)) return TGA_RGBA_RLE;
00124         if(!strcasecmp(compression_to_str(TGA_RGB), string)) return TGA_RGB;
00125         if(!strcasecmp(compression_to_str(TGA_RGBA), string)) return TGA_RGBA;
00126         return TGA_RGB;
00127 }
00128 
00129 int FileTGA::can_copy_from(Edit *edit, int64_t position)
00130 {
00131         if(edit->asset->format == FILE_TGA_LIST ||
00132                 edit->asset->format == FILE_TGA)
00133                 return 1;
00134         
00135         return 0;
00136 }
00137 
00138 
00139 int  FileTGA::colormodel_supported(int colormodel)
00140 {
00141         return colormodel;
00142 }
00143 
00144 int FileTGA::get_best_colormodel(Asset *asset, int driver)
00145 {
00146         if(!strcasecmp(asset->vcodec, TGA_RGB_RLE) || 
00147                 !strcasecmp(asset->vcodec, TGA_RGB)) return BC_RGB888;
00148         if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE) ||
00149                 !strcasecmp(asset->vcodec, TGA_RGBA)) return BC_RGBA8888;
00150         return BC_RGB888;
00151 }
00152 
00153 int FileTGA::read_frame(VFrame *frame, VFrame *data)
00154 {
00155         read_tga(asset, frame, data, temp);
00156         return 0;
00157 }
00158 
00159 int FileTGA::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
00160 {
00161         TGAUnit *tga_unit = (TGAUnit*)unit;
00162 
00163         write_tga(asset, frame, data, tga_unit->temp);
00164         return 0;
00165 }
00166 
00167 FrameWriterUnit* FileTGA::new_writer_unit(FrameWriter *writer)
00168 {
00169         return new TGAUnit(this, writer);
00170 }
00171 
00172 int FileTGA::get_memory_usage()
00173 {
00174         int result = FileList::get_memory_usage();
00175         if(temp) result += temp->get_data_size();
00176         return result;
00177 }
00178 
00179 
00180 
00181 
00182 
00183 
00184 
00185 
00186 #define FOOTERSIZE 26
00187 #define HEADERSIZE 18
00188 int FileTGA::read_frame_header(char *path)
00189 {
00190         int result = 0;
00191 
00192 //printf("FileTGA::read_frame_header 1\n");
00193         FILE *stream;
00194 
00195         if(!(stream = fopen(path, "rb")))
00196         {
00197                 perror("FileTGA::read_frame_header");
00198                 return 1;
00199         }
00200 
00201         unsigned char header[HEADERSIZE];
00202         fread(header, HEADERSIZE, 1, stream);
00203         fclose(stream);
00204 
00205         asset->width = header[12] | (header[13] << 8);
00206         asset->height = header[14] | (header[15] << 8);
00207         int bpp = header[16];
00208         int rle = header[2] & 0x8;
00209         switch(bpp)
00210         {
00211                 case 32:
00212                         if(rle) 
00213                                 strcpy(asset->vcodec, TGA_RGBA_RLE);
00214                         else
00215                                 strcpy(asset->vcodec, TGA_RGBA);
00216                         break;
00217                 case 24:
00218                         if(rle) 
00219                                 strcpy(asset->vcodec, TGA_RGB_RLE);
00220                         else
00221                                 strcpy(asset->vcodec, TGA_RGB);
00222                         break;
00223         }
00224 //printf("FileTGA::read_frame_header 2 %d %d\n", asset->width, asset->height);
00225 
00226         return result;
00227 }
00228 
00229 void FileTGA::read_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
00230 {
00231 // Read header
00232         unsigned char *footer, *header;
00233         int input_cmodel;
00234         int64_t file_offset = 0;
00235 
00236         footer = data->get_data() + 
00237                 data->get_compressed_size() - 
00238                 FOOTERSIZE;
00239         header = data->get_data();
00240         file_offset += HEADERSIZE;
00241 
00242         int image_type;
00243         int image_compression;
00244         switch(header[2])
00245         {
00246                 case 1:
00247                         image_type = TGA_TYPE_MAPPED;
00248                         image_compression = TGA_COMP_NONE;
00249                         break;
00250                 case 2:
00251                         image_type = TGA_TYPE_COLOR;
00252                         image_compression = TGA_COMP_NONE;
00253                         break;
00254                 case 3:
00255                         image_type = TGA_TYPE_GRAY;
00256                         image_compression = TGA_COMP_NONE;
00257                         break;
00258                 case 9:
00259                         image_type = TGA_TYPE_MAPPED;
00260                         image_compression = TGA_COMP_RLE;
00261                         break;
00262                 case 10:
00263                         image_type = TGA_TYPE_COLOR;
00264                         image_compression = TGA_COMP_RLE;
00265                         break;
00266                 case 11:
00267                         image_type = TGA_TYPE_GRAY;
00268                         image_compression = TGA_COMP_RLE;
00269                         break;
00270                 default:
00271                         image_type = 0;
00272         }
00273         
00274         int idlength = header[0];
00275         int colormaptype = header[1];
00276         int colormapindex = header[3] + header[4] * 256;
00277         int colormaplength = header[5] + header[6] * 256;
00278         int colormapsize = header[7];
00279         int xorigin = header[8] + header[9] * 256;
00280         int yorigin = header[10] + header[11] * 256;
00281         int width = header[12] + header[13] * 256;
00282         int height = header[14] + header[15] * 256;
00283         int bpp = header[16];
00284         int bytes = (bpp + 7) / 8;
00285         int alphabits = header[17] & 0x0f;
00286         int fliphoriz = (header[17] & 0x10) ? 1 : 0;
00287         int flipvert = (header[17] & 0x20) ? 0 : 1;
00288         int data_size = data->get_compressed_size();
00289 
00290         if(idlength) file_offset += idlength;
00291 
00292 // Get colormap
00293         unsigned char *tga_cmap;
00294         unsigned char colormap[4 * 256];
00295 
00296         if(colormaptype == 1)
00297         {
00298                 int cmap_bytes = (colormapsize + 7) / 8;
00299                 tga_cmap = data->get_data() + file_offset;
00300                 file_offset += colormaplength * cmap_bytes;
00301                 
00302                 switch(colormapsize)
00303                 {
00304                         case 32:
00305                                 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 1);
00306                                 break;
00307                         case 24:
00308                                 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 0);
00309                                 break;
00310                         case 16:
00311                                 upsample(colormap, tga_cmap, colormaplength, cmap_bytes);
00312                                 break;
00313                 }
00314         }
00315 
00316         int source_cmodel = BC_RGB888;
00317         switch(bpp)
00318         {
00319                 case 32:
00320                         source_cmodel = BC_RGBA8888;
00321                         break;
00322                 case 24:
00323                         source_cmodel = BC_RGB888;
00324                         break;
00325         }
00326 
00327 // Read image
00328         VFrame *output_frame;
00329         if(frame->get_color_model() == source_cmodel)
00330         {
00331                 output_frame = frame;
00332         }
00333         else
00334         {
00335                 if(temp && temp->get_color_model() != source_cmodel)
00336                 {
00337                         delete temp;
00338                         temp = 0;
00339                 }
00340                 
00341                 if(!temp)
00342                 {
00343                         temp = new VFrame(0, width, height, source_cmodel);
00344                 }
00345                 output_frame = temp;
00346         }
00347 
00348         if(flipvert)
00349         {
00350                 for(int i = height - 1; i >= 0; i--)
00351                 {
00352                         read_line(output_frame->get_rows()[i], 
00353                                 data->get_data(), 
00354                                 file_offset,
00355                                 image_type,
00356                                 bpp,
00357                                 image_compression,
00358                                 bytes,
00359                                 width,
00360                                 fliphoriz,
00361                                 alphabits,
00362                                 data_size);
00363                 }
00364         }
00365         else
00366         {
00367                 for(int i = 0; i < height; i++)
00368                 {
00369                         read_line(output_frame->get_rows()[i], 
00370                                 data->get_data(), 
00371                                 file_offset,
00372                                 image_type,
00373                                 bpp,
00374                                 image_compression,
00375                                 bytes,
00376                                 width,
00377                                 fliphoriz,
00378                                 alphabits,
00379                                 data_size);
00380                 }
00381         }
00382 
00383         if(output_frame != frame)
00384         {
00385                 cmodel_transfer(frame->get_rows(), 
00386                         output_frame->get_rows(),
00387                         frame->get_y(),
00388                         frame->get_u(),
00389                         frame->get_v(),
00390                         output_frame->get_y(),
00391                         output_frame->get_u(),
00392                         output_frame->get_v(),
00393                         0, 
00394                         0, 
00395                         width, 
00396                         height,
00397                         0, 
00398                         0, 
00399                         frame->get_w(), 
00400                         frame->get_h(),
00401                         output_frame->get_color_model(), 
00402                         frame->get_color_model(),
00403                         0,
00404                         width,
00405                         frame->get_w());
00406         }
00407 }
00408 
00409 void FileTGA::write_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
00410 {
00411         unsigned char header[18];
00412         unsigned char footer[26];
00413         int64_t file_offset = 0;
00414         int out_bpp = 0;
00415         int rle = 0;
00416         int dest_cmodel = BC_RGB888;
00417 
00418 //printf("FileTGA::write_tga 1\n");
00419 
00420         header[0] = 0;
00421         header[1] = 0;
00422         if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE))
00423         {
00424                 header[2] = 10;
00425         out_bpp = 4;
00426                 rle = 1;
00427         header[16] = 32; /* bpp */
00428         header[17] = 0x28; /* alpha + orientation */
00429                 dest_cmodel = BC_RGBA8888;
00430         }
00431         else
00432         if(!strcasecmp(asset->vcodec, TGA_RGBA))
00433         {
00434                 header[2] = 2;
00435         out_bpp = 4;
00436                 rle = 0;
00437         header[16] = 32; /* bpp */
00438         header[17] = 0x28; /* alpha + orientation */
00439                 dest_cmodel = BC_RGBA8888;
00440         }
00441         else
00442         if(!strcasecmp(asset->vcodec, TGA_RGB_RLE))
00443         {
00444                 header[2] = 10;
00445         out_bpp = 3;
00446                 rle = 1;
00447         header[16] = 24; /* bpp */
00448         header[17] = 0x20; /* alpha + orientation */
00449                 dest_cmodel = BC_RGB888;
00450         }
00451         else
00452         {
00453                 header[2] = 2;
00454         out_bpp = 3;
00455                 rle = 0;
00456         header[16] = 24; /* bpp */
00457         header[17] = 0x20; /* alpha + orientation */
00458                 dest_cmodel = BC_RGB888;
00459         }
00460     header[3] = header[4] = header[5] = header[6] = header[7] = 0;
00461 //printf("FileTGA::write_tga 1\n");
00462 
00463         VFrame *input_frame;
00464         if(frame->get_color_model() == dest_cmodel)
00465         {
00466                 input_frame = frame;
00467         }
00468         else
00469         {
00470                 if(temp && temp->get_color_model() != dest_cmodel)
00471                 {
00472                         delete temp;
00473                         temp = 0;
00474                 }
00475                 
00476                 if(!temp)
00477                 {
00478                         temp = new VFrame(0, frame->get_w(), frame->get_h(), dest_cmodel);
00479                 }
00480                 input_frame = temp;
00481 
00482                 cmodel_transfer(input_frame->get_rows(), 
00483                         frame->get_rows(),
00484                         input_frame->get_y(),
00485                         input_frame->get_u(),
00486                         input_frame->get_v(),
00487                         frame->get_y(),
00488                         frame->get_u(),
00489                         frame->get_v(),
00490                         0, 
00491                         0, 
00492                         frame->get_w(), 
00493                         frame->get_h(),
00494                         0, 
00495                         0, 
00496                         frame->get_w(), 
00497                         frame->get_h(),
00498                         frame->get_color_model(), 
00499                         input_frame->get_color_model(),
00500                         0,
00501                         frame->get_w(),
00502                         frame->get_w());
00503         }
00504 //printf("FileTGA::write_tga 1\n");
00505 
00506 // xorigin
00507 // yorigin
00508         header[8]  = header[9]  = 0;
00509         header[10] = header[11] = 0;
00510 
00511         header[12] = input_frame->get_w() % 256;
00512         header[13] = input_frame->get_w() / 256;
00513 
00514         header[14] = input_frame->get_h() % 256;
00515         header[15] = input_frame->get_h() / 256;
00516 //printf("FileTGA::write_tga 1\n");
00517         
00518         write_data(header, data, file_offset, sizeof(header));
00519 //printf("FileTGA::write_tga 1\n");
00520 
00521         unsigned char *output = new unsigned char[out_bpp * input_frame->get_w()];
00522 //printf("FileTGA::write_tga 1\n");
00523         for(int i = 0; i < input_frame->get_h(); i++)
00524         {
00525 //printf("FileTGA::write_tga 2\n");
00526                 bgr2rgb(output, input_frame->get_rows()[i], input_frame->get_w(), out_bpp, (out_bpp == 4));
00527 //printf("FileTGA::write_tga 3\n");
00528                 
00529                 if(rle)
00530                 {
00531 //printf("FileTGA::write_tga 4\n");
00532                         rle_write(output, 
00533                                 input_frame->get_w(), 
00534                                 out_bpp,
00535                                 data,
00536                                 file_offset);
00537 //printf("FileTGA::write_tga 5\n");
00538                 }
00539                 else
00540                 {
00541 //printf("FileTGA::write_tga 6\n");
00542                         write_data(output, 
00543                                 data, 
00544                                 file_offset, 
00545                                 input_frame->get_w() * out_bpp);
00546 //printf("FileTGA::write_tga 7\n");
00547                 }
00548         }
00549 //printf("FileTGA::write_tga 8\n");
00550         delete [] output;
00551 //printf("FileTGA::write_tga 9\n");
00552 }
00553 
00554 void FileTGA::write_data(unsigned char *buffer, 
00555         VFrame *data, 
00556         int64_t &file_offset,
00557         int64_t len)
00558 {
00559 //printf("FileTGA::write_data 1 %d\n", len);
00560         if(data->get_compressed_allocated() <= data->get_compressed_size() + len)
00561         {
00562                 data->allocate_compressed_data((data->get_compressed_size() + len) * 2);
00563         }
00564 //printf("FileTGA::write_data 1 %d\n", len);
00565 
00566         bcopy(buffer, data->get_data() + file_offset, len);
00567 //printf("FileTGA::write_data 1 %d\n", len);
00568         file_offset += len;
00569 //printf("FileTGA::write_data 1 %d\n", len);
00570         data->set_compressed_size(file_offset);
00571 //printf("FileTGA::write_data 2 %d\n", len);
00572 }
00573 
00574 void FileTGA::read_line(unsigned char *row,
00575         unsigned char *data,
00576         int64_t &file_offset,
00577         int image_type,
00578         int bpp,
00579         int image_compression,
00580         int bytes,
00581         int width,
00582         int fliphoriz,
00583         int alphabits,
00584         int data_size)
00585 {
00586         if(file_offset >= data_size) return;
00587         if(image_compression == TGA_COMP_RLE)
00588         {
00589                 rle_read(row,
00590                         data,
00591                         file_offset,
00592                         bytes,
00593                         width);
00594         }
00595         else
00596         {
00597                 if(file_offset + bytes * width <= data_size)
00598                         bcopy(data + file_offset, row, bytes * width);
00599                 file_offset += bytes * width;
00600         }
00601         
00602         if(fliphoriz)
00603         {
00604                 flip_line(row, bytes, width);
00605         }
00606         
00607         if(image_type == TGA_TYPE_COLOR)
00608         {
00609                 if(bpp == 16)
00610                 {
00611                         upsample(row, row, width, bytes);
00612                 }
00613                 else
00614                 {
00615                         bgr2rgb(row, row, width, bytes, alphabits);
00616                 }
00617         }
00618         else
00619         {
00620                 ;
00621         }
00622 }
00623 
00624 void FileTGA::flip_line(unsigned char *row, int bytes, int width)
00625 {
00626         unsigned char temp;
00627         unsigned char *alt;
00628         int x, s;
00629 
00630         alt = row + (bytes * (width - 1));
00631 
00632         for (x = 0; x * 2 <= width; x++)
00633     {
00634         for(s = 0; s < bytes; ++s)
00635                 {
00636                         temp = row[s];
00637                         row[s] = alt[s];
00638                         alt[s] = temp;
00639                 }
00640 
00641         row += bytes;
00642         alt -= bytes;
00643     }
00644 }
00645 
00646 void FileTGA::rle_read(unsigned char *row,
00647         unsigned char *data,
00648         int64_t &file_offset,
00649         int bytes,
00650         int width)
00651 {
00652         int repeat = 0;
00653         int direct = 0;
00654         unsigned char sample[4];
00655         int head;
00656 
00657         for(int x = 0; x < width; x++)
00658         {
00659                 if(repeat == 0 && direct == 0)
00660                 {
00661                         head = data[file_offset++];
00662                         if(head == EOF)
00663                         {
00664                                 return;
00665                         }
00666                         else
00667                         if(head >= 128)
00668                         {
00669                                 repeat = head - 127;
00670                                 bcopy(data + file_offset, sample, bytes);
00671                                 file_offset += bytes;
00672                         }
00673                         else
00674                         {
00675                                 direct = head + 1;
00676                         }
00677                 }
00678                 
00679                 if(repeat > 0)
00680                 {
00681                         for(int k = 0; k < bytes; k++)
00682                         {
00683                                 row[k] = sample[k];
00684                         }
00685                         
00686                         repeat--;
00687                 }
00688                 else
00689                 {
00690                         bcopy(data + file_offset, row, bytes);
00691                         file_offset += bytes;
00692                         
00693                         direct--;
00694                 }
00695                 
00696                 row += bytes;
00697         }
00698 }
00699 
00700 void FileTGA::rle_write(unsigned char *buffer, 
00701         int width, 
00702         int bytes, 
00703         VFrame *frame, 
00704         int64_t &file_offset)
00705 {
00706         int repeat = 0;
00707         int direct = 0;
00708         unsigned char *from = buffer;
00709         unsigned char output;
00710         int x;
00711         
00712         for(x = 1; x < width; ++x)
00713         {
00714 /* next pixel is different */
00715                 if(memcmp(buffer, buffer + bytes, bytes))
00716                 {
00717                         if(repeat)
00718                         {
00719                                 output = 128 + repeat;
00720                                 write_data(&output, frame, file_offset, 1);
00721                                 write_data(from, frame, file_offset, bytes);
00722                                 from = buffer + bytes;
00723                                 repeat = 0;
00724                                 direct = 0;
00725                         }
00726                         else
00727                         {
00728                                 direct++;
00729                         }
00730                 }
00731                 else
00732 /* next pixel is the same */
00733                 {
00734                         if(direct)
00735                         {
00736                                 output = direct - 1;
00737                                 write_data(&output, frame, file_offset, 1);
00738                                 write_data(from, frame, file_offset, bytes * direct);
00739                                 from = buffer;
00740                                 direct = 0;
00741                                 repeat = 1;
00742                         }
00743                         else
00744                         {
00745                                 repeat++;
00746                         }
00747                 }
00748                 
00749                 if(repeat == 128)
00750                 {
00751                         output = 255;
00752                         write_data(&output, frame, file_offset, 1);
00753                         write_data(from, frame, file_offset, bytes);
00754                         from = buffer + bytes;
00755                         direct = 0;
00756                         repeat = 0;
00757                 }
00758                 else
00759                 if(direct == 128)
00760                 {
00761                         output = 127;
00762                         write_data(&output, frame, file_offset, 1);
00763                         write_data(from, frame, file_offset, direct * bytes);
00764                         from = buffer + bytes;
00765                         direct = 0;
00766                         repeat = 0;
00767                 }
00768                 
00769                 buffer += bytes;
00770         }
00771         
00772         if(repeat > 0)
00773         {
00774                 output = 128 + repeat;
00775                 write_data(&output, frame, file_offset, 1);
00776                 write_data(from, frame, file_offset, bytes);
00777         }
00778         else
00779         {
00780                 output = direct;
00781                 write_data(&output, frame, file_offset, 1);
00782                 write_data(from, frame, file_offset, bytes * (direct + 1));
00783         }
00784 }
00785 
00786 
00787 void FileTGA::bgr2rgb(unsigned char *dest,
00788          unsigned char *src,
00789          int width,
00790          int bytes,
00791          int alpha)
00792 {
00793         int x;
00794         unsigned char r, g, b;
00795 
00796         if(alpha)
00797     {
00798         for(x = 0; x < width; x++)
00799                 {
00800                         r = src[2];
00801                         g = src[1];
00802                         b = src[0];
00803                         *(dest++) = r;
00804                         *(dest++) = g;
00805                         *(dest++) = b;
00806                         *(dest++) = src[3];
00807 
00808                         src += bytes;
00809                 }
00810     }
00811         else
00812     {
00813         for(x = 0; x < width; x++)
00814                 {
00815                         r = src[2];
00816                         g = src[1];
00817                         b = src[0];
00818                         *(dest++) = r;
00819                         *(dest++) = g;
00820                         *(dest++) = b;
00821 
00822                         src += bytes;
00823                 }
00824     }
00825 }
00826 
00827 void FileTGA::upsample(unsigned char *dest,
00828           unsigned char *src,
00829           int width,
00830           int bytes)
00831 {
00832         int x;
00833 
00834         dest += (width - 1) * 3;
00835         src += (width - 1) * bytes;
00836         for(x = width - 1; x >= 0; x--)
00837     {
00838         dest[0] =  ((src[1] << 1) & 0xf8);
00839         dest[0] += (dest[0] >> 5);
00840 
00841         dest[1] =  ((src[0] & 0xe0) >> 2) + ((src[1] & 0x03) << 6);
00842         dest[1] += (dest[1] >> 5);
00843 
00844         dest[2] =  ((src[0] << 3) & 0xf8);
00845         dest[2] += (dest[2] >> 5);
00846 
00847         dest -= 3;
00848         src -= bytes;
00849     }
00850 }
00851 
00852 
00853 
00854 
00855 
00856 
00857 
00858 
00859 
00860 TGAUnit::TGAUnit(FileTGA *file, FrameWriter *writer)
00861  : FrameWriterUnit(writer)
00862 {
00863         temp = 0;
00864         this->file = file;
00865 }
00866 
00867 TGAUnit::~TGAUnit()
00868 {
00869         if(temp) delete temp;
00870 }
00871 
00872 
00873 
00874 
00875 
00876 
00877 
00878 
00879 
00880 
00881 
00882 
00883 
00884 TGAConfigVideo::TGAConfigVideo(BC_WindowBase *gui, Asset *asset)
00885  : BC_Window(PROGRAM_NAME ": Video Compression",
00886         gui->get_abs_cursor_x(1),
00887         gui->get_abs_cursor_y(1),
00888         400,
00889         100)
00890 {
00891         this->gui = gui;
00892         this->asset = asset;
00893 
00894         compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB_RLE)));
00895         compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA_RLE)));
00896         compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB)));
00897         compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA)));
00898 }
00899 
00900 TGAConfigVideo::~TGAConfigVideo()
00901 {
00902         compression_items.remove_all_objects();
00903 }
00904 
00905 int TGAConfigVideo::create_objects()
00906 {
00907         int x = 10, y = 10;
00908 
00909         add_subwindow(new BC_Title(x, y, _("Compression:")));
00910         TGACompression *textbox = new TGACompression(this, 
00911                 x + 110, 
00912                 y, 
00913                 asset, 
00914                 &compression_items);
00915         textbox->create_objects();
00916         add_subwindow(new BC_OKButton(this));
00917         return 0;
00918 }
00919 
00920 int TGAConfigVideo::close_event()
00921 {
00922         set_done(0);
00923         return 1;
00924 }
00925 
00926 
00927 TGACompression::TGACompression(TGAConfigVideo *gui,
00928         int x, 
00929         int y, 
00930         Asset *asset, 
00931         ArrayList<BC_ListBoxItem*> *compression_items)
00932  : BC_PopupTextBox(gui,
00933         compression_items,
00934         FileTGA::compression_to_str(gui->asset->vcodec),
00935         x, 
00936         y, 
00937         200,
00938         200)
00939 {
00940         this->asset = asset;
00941 }
00942 int TGACompression::handle_event()
00943 {
00944         strcpy(asset->vcodec, FileTGA::str_to_compression(get_text()));
00945         return 1;
00946 }

Generated on Sun Jan 8 13:38:55 2006 for Cinelerra-svn by  doxygen 1.4.4