4 * Copyright (C) 2008 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 "mwindow.inc"
29 #include "mainerror.h"
34 /* Known image types. */
35 #define TGA_TYPE_MAPPED 1
36 #define TGA_TYPE_COLOR 2
37 #define TGA_TYPE_GRAY 3
39 /* Only known compression is RLE */
40 #define TGA_COMP_NONE 0
41 #define TGA_COMP_RLE 1
44 FileTGA::FileTGA(Asset *asset, File *file)
45 : FileList(asset, file, "TGALIST", ".tga", FILE_TGA, FILE_TGA_LIST)
55 int FileTGA::check_sig(Asset *asset)
59 // Test file extension
61 char *ext = strrchr(asset->path, '.');
66 if(!strncasecmp(ext, ".tga", 4)) result = 1;
76 if(!(stream = fopen(asset->path, "rb")))
84 (void)fread(test, 16, 1, stream);
86 if(test[0] == 'T' && test[1] == 'G' && test[2] == 'A' &&
87 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' &&
100 void FileTGA::get_parameters(BC_WindowBase *parent_window,
101 Asset *asset, BC_WindowBase* &format_window,
102 int audio_options, int video_options, EDL *edl)
106 TGAConfigVideo *window = new TGAConfigVideo(parent_window, asset);
107 format_window = window;
108 window->create_objects();
109 window->run_window();
116 N_("RGBA compressed")
117 N_("RGB uncompressed")
118 N_("RGBA uncompressed")
121 #define TGA_RGB_RLE "rle "
122 #define TGA_RGBA_RLE "rlea"
123 #define TGA_RGB "raw "
124 #define TGA_RGBA "rawa"
126 #define TGA_RGB_RLE_NAME "RGB compressed"
127 #define TGA_RGBA_RLE_NAME "RGBA compressed"
128 #define TGA_RGB_NAME "RGB uncompressed"
129 #define TGA_RGBA_NAME "RGBA uncompressed"
131 const char* FileTGA::compression_to_str(const char *compression)
133 if(!strcasecmp(compression, TGA_RGB_RLE)) return _(TGA_RGB_RLE_NAME);
134 if(!strcasecmp(compression, TGA_RGBA_RLE)) return _(TGA_RGBA_RLE_NAME);
135 if(!strcasecmp(compression, TGA_RGB)) return _(TGA_RGB_NAME);
136 if(!strcasecmp(compression, TGA_RGBA)) return _(TGA_RGBA_NAME);
140 const char* FileTGA::str_to_compression(const char *string)
142 if(!strcasecmp(compression_to_str(TGA_RGB_RLE), string)) return TGA_RGB_RLE;
143 if(!strcasecmp(compression_to_str(TGA_RGBA_RLE), string)) return TGA_RGBA_RLE;
144 if(!strcasecmp(compression_to_str(TGA_RGB), string)) return TGA_RGB;
145 if(!strcasecmp(compression_to_str(TGA_RGBA), string)) return TGA_RGBA;
149 int FileTGA::can_copy_from(Asset *asset, int64_t position)
151 if(asset->format == FILE_TGA_LIST ||
152 asset->format == FILE_TGA)
159 int FileTGA::colormodel_supported(int colormodel)
164 int FileTGA::get_best_colormodel(Asset *asset, int driver)
166 if(!strcasecmp(asset->vcodec, TGA_RGB_RLE) ||
167 !strcasecmp(asset->vcodec, TGA_RGB)) return BC_RGB888;
168 if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE) ||
169 !strcasecmp(asset->vcodec, TGA_RGBA)) return BC_RGBA8888;
173 int FileTGA::read_frame(VFrame *frame, VFrame *data)
175 read_tga(asset, frame, data, temp);
179 int FileTGA::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
181 TGAUnit *tga_unit = (TGAUnit*)unit;
183 write_tga(asset, frame, data, tga_unit->temp);
187 FrameWriterUnit* FileTGA::new_writer_unit(FrameWriter *writer)
189 return new TGAUnit(this, writer);
192 int64_t FileTGA::get_memory_usage()
194 int64_t result = FileList::get_memory_usage();
195 if(temp) result += temp->get_data_size();
206 #define FOOTERSIZE 26
207 #define HEADERSIZE 18
208 int FileTGA::read_frame_header(char *path)
212 //printf("FileTGA::read_frame_header 1\n");
215 if(!(stream = fopen(path, "rb")))
217 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
221 unsigned char header[HEADERSIZE];
222 (void)fread(header, HEADERSIZE, 1, stream);
225 asset->width = header[12] | (header[13] << 8);
226 asset->height = header[14] | (header[15] << 8);
227 int bpp = header[16];
228 int rle = header[2] & 0x8;
233 strcpy(asset->vcodec, TGA_RGBA_RLE);
235 strcpy(asset->vcodec, TGA_RGBA);
239 strcpy(asset->vcodec, TGA_RGB_RLE);
241 strcpy(asset->vcodec, TGA_RGB);
244 //printf("FileTGA::read_frame_header 2 %d %d\n", asset->width, asset->height);
249 void FileTGA::read_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
252 int64_t file_offset = 0;
254 // unsigned char *footer = data->get_data() +
255 // data->get_compressed_size() - FOOTERSIZE;
256 unsigned char *header = data->get_data();
257 file_offset += HEADERSIZE;
260 int image_compression = 0;
264 image_type = TGA_TYPE_MAPPED;
265 image_compression = TGA_COMP_NONE;
268 image_type = TGA_TYPE_COLOR;
269 image_compression = TGA_COMP_NONE;
272 image_type = TGA_TYPE_GRAY;
273 image_compression = TGA_COMP_NONE;
276 image_type = TGA_TYPE_MAPPED;
277 image_compression = TGA_COMP_RLE;
280 image_type = TGA_TYPE_COLOR;
281 image_compression = TGA_COMP_RLE;
284 image_type = TGA_TYPE_GRAY;
285 image_compression = TGA_COMP_RLE;
289 int idlength = header[0];
290 int colormaptype = header[1];
291 //int colormapindex = header[3] + header[4] * 256;
292 int colormaplength = header[5] + header[6] * 256;
293 int colormapsize = header[7];
294 //int xorigin = header[8] + header[9] * 256;
295 //int yorigin = header[10] + header[11] * 256;
296 int width = header[12] + header[13] * 256;
297 int height = header[14] + header[15] * 256;
298 int bpp = header[16];
299 int bytes = (bpp + 7) / 8;
300 int alphabits = header[17] & 0x0f;
301 int fliphoriz = (header[17] & 0x10) ? 1 : 0;
302 int flipvert = (header[17] & 0x20) ? 0 : 1;
303 int data_size = data->get_compressed_size();
305 if(idlength) file_offset += idlength;
308 unsigned char *tga_cmap;
309 unsigned char colormap[4 * 256];
311 if(colormaptype == 1)
313 int cmap_bytes = (colormapsize + 7) / 8;
314 tga_cmap = data->get_data() + file_offset;
315 file_offset += colormaplength * cmap_bytes;
320 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 1);
323 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 0);
326 upsample(colormap, tga_cmap, colormaplength, cmap_bytes);
331 int source_cmodel = BC_RGB888;
335 source_cmodel = BC_RGBA8888;
338 source_cmodel = BC_RGB888;
345 VFrame *output_frame;
346 if(frame->get_color_model() == source_cmodel)
348 output_frame = frame;
352 if(temp && temp->get_color_model() != source_cmodel)
360 temp = new VFrame(width, height, source_cmodel, 0);
367 for(int i = height - 1; i >= 0; i--)
369 read_line(output_frame->get_rows()[i],
384 for(int i = 0; i < height; i++)
386 read_line(output_frame->get_rows()[i],
400 if(output_frame != frame)
402 BC_CModels::transfer(frame->get_rows(), output_frame->get_rows(),
403 frame->get_y(), frame->get_u(), frame->get_v(),
404 output_frame->get_y(), output_frame->get_u(), output_frame->get_v(),
406 0, 0, frame->get_w(), frame->get_h(),
407 output_frame->get_color_model(), frame->get_color_model(),
408 0, width, frame->get_w());
412 void FileTGA::write_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
414 unsigned char header[18];
415 //unsigned char footer[26];
416 int64_t file_offset = 0;
419 int dest_cmodel = BC_RGB888;
421 //printf("FileTGA::write_tga 1\n");
425 if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE))
430 header[16] = 32; /* bpp */
431 header[17] = 0x28; /* alpha + orientation */
432 dest_cmodel = BC_RGBA8888;
435 if(!strcasecmp(asset->vcodec, TGA_RGBA))
440 header[16] = 32; /* bpp */
441 header[17] = 0x28; /* alpha + orientation */
442 dest_cmodel = BC_RGBA8888;
445 if(!strcasecmp(asset->vcodec, TGA_RGB_RLE))
450 header[16] = 24; /* bpp */
451 header[17] = 0x20; /* alpha + orientation */
452 dest_cmodel = BC_RGB888;
459 header[16] = 24; /* bpp */
460 header[17] = 0x20; /* alpha + orientation */
461 dest_cmodel = BC_RGB888;
463 header[3] = header[4] = header[5] = header[6] = header[7] = 0;
464 //printf("FileTGA::write_tga 1\n");
467 if(frame->get_color_model() == dest_cmodel)
473 if(temp && temp->get_color_model() != dest_cmodel)
481 temp = new VFrame(0, -1, frame->get_w(), frame->get_h(), dest_cmodel, -1);
485 BC_CModels::transfer(input_frame->get_rows(), frame->get_rows(),
486 input_frame->get_y(), input_frame->get_u(), input_frame->get_v(),
487 frame->get_y(), frame->get_u(), frame->get_v(),
488 0, 0, frame->get_w(), frame->get_h(),
489 0, 0, frame->get_w(), frame->get_h(),
490 frame->get_color_model(), input_frame->get_color_model(),
491 0, frame->get_w(), frame->get_w());
493 //printf("FileTGA::write_tga 1\n");
497 header[8] = header[9] = 0;
498 header[10] = header[11] = 0;
500 header[12] = input_frame->get_w() % 256;
501 header[13] = input_frame->get_w() / 256;
503 header[14] = input_frame->get_h() % 256;
504 header[15] = input_frame->get_h() / 256;
505 //printf("FileTGA::write_tga 1\n");
507 write_data(header, data, file_offset, sizeof(header));
508 //printf("FileTGA::write_tga 1\n");
510 unsigned char *output = new unsigned char[out_bpp * input_frame->get_w()];
511 //printf("FileTGA::write_tga 1\n");
512 for(int i = 0; i < input_frame->get_h(); i++)
514 //printf("FileTGA::write_tga 2\n");
515 bgr2rgb(output, input_frame->get_rows()[i], input_frame->get_w(), out_bpp, (out_bpp == 4));
516 //printf("FileTGA::write_tga 3\n");
520 //printf("FileTGA::write_tga 4\n");
522 input_frame->get_w(),
526 //printf("FileTGA::write_tga 5\n");
530 //printf("FileTGA::write_tga 6\n");
534 input_frame->get_w() * out_bpp);
535 //printf("FileTGA::write_tga 7\n");
538 //printf("FileTGA::write_tga 8\n");
540 //printf("FileTGA::write_tga 9\n");
543 void FileTGA::write_data(unsigned char *buffer,
545 int64_t &file_offset,
548 //printf("FileTGA::write_data 1 %d\n", len);
549 if(data->get_compressed_allocated() <= data->get_compressed_size() + len)
551 data->allocate_compressed_data((data->get_compressed_size() + len) * 2);
553 //printf("FileTGA::write_data 1 %d\n", len);
555 bcopy(buffer, data->get_data() + file_offset, len);
556 //printf("FileTGA::write_data 1 %d\n", len);
558 //printf("FileTGA::write_data 1 %d\n", len);
559 data->set_compressed_size(file_offset);
560 //printf("FileTGA::write_data 2 %d\n", len);
563 void FileTGA::read_line(unsigned char *row, unsigned char *data,
564 int64_t &file_offset, int image_type, int bpp, int image_compression,
565 int bytes, int width, int fliphoriz, int alphabits, int data_size)
567 if(file_offset >= data_size) return;
568 if(image_compression == TGA_COMP_RLE)
570 rle_read(row, data, file_offset, bytes, width);
574 if(file_offset + bytes * width <= data_size)
575 bcopy(data + file_offset, row, bytes * width);
576 file_offset += bytes * width;
581 flip_line(row, bytes, width);
584 if(image_type == TGA_TYPE_COLOR)
588 upsample(row, row, width, bytes);
592 bgr2rgb(row, row, width, bytes, alphabits);
601 void FileTGA::flip_line(unsigned char *row, int bytes, int width)
606 alt = row + (bytes * (width - 1));
608 for (x = 0; x * 2 <= width; x++) {
609 for(s = 0; s < bytes; ++s) {
620 void FileTGA::rle_read(unsigned char *row,
622 int64_t &file_offset,
628 unsigned char sample[4];
631 for(int x = 0; x < width; x++)
633 if(repeat == 0 && direct == 0)
635 head = data[file_offset++];
644 bcopy(data + file_offset, sample, bytes);
645 file_offset += bytes;
655 for(int k = 0; k < bytes; k++)
664 bcopy(data + file_offset, row, bytes);
665 file_offset += bytes;
674 void FileTGA::rle_write(unsigned char *buffer,
678 int64_t &file_offset)
682 unsigned char *from = buffer;
683 unsigned char output;
686 for(x = 1; x < width; ++x)
688 /* next pixel is different */
689 if(memcmp(buffer, buffer + bytes, bytes))
693 output = 128 + repeat;
694 write_data(&output, frame, file_offset, 1);
695 write_data(from, frame, file_offset, bytes);
696 from = buffer + bytes;
706 /* next pixel is the same */
711 write_data(&output, frame, file_offset, 1);
712 write_data(from, frame, file_offset, bytes * direct);
726 write_data(&output, frame, file_offset, 1);
727 write_data(from, frame, file_offset, bytes);
728 from = buffer + bytes;
736 write_data(&output, frame, file_offset, 1);
737 write_data(from, frame, file_offset, direct * bytes);
738 from = buffer + bytes;
748 output = 128 + repeat;
749 write_data(&output, frame, file_offset, 1);
750 write_data(from, frame, file_offset, bytes);
755 write_data(&output, frame, file_offset, 1);
756 write_data(from, frame, file_offset, bytes * (direct + 1));
761 void FileTGA::bgr2rgb(unsigned char *dest, unsigned char *src,
762 int width, int bytes, int alpha)
765 unsigned char r, g, b;
768 for(x = 0; x < width; x++) {
769 r = src[2]; g = src[1]; b = src[0];
770 *(dest++) = r; *(dest++) = g; *(dest++) = b;
771 *(dest++) = src[3]; src += bytes;
775 for(x = 0; x < width; x++) {
776 r = src[2]; g = src[1]; b = src[0];
777 *(dest++) = r; *(dest++) = g; *(dest++) = b;
783 void FileTGA::upsample(unsigned char *dest,
790 dest += (width - 1) * 3;
791 src += (width - 1) * bytes;
792 for(x = width - 1; x >= 0; x--) {
793 dest[0] = ((src[1] << 1) & 0xf8);
794 dest[0] += (dest[0] >> 5);
796 dest[1] = ((src[0] & 0xe0) >> 2) + ((src[1] & 0x03) << 6);
797 dest[1] += (dest[1] >> 5);
799 dest[2] = ((src[0] << 3) & 0xf8);
800 dest[2] += (dest[2] >> 5);
808 TGAUnit::TGAUnit(FileTGA *file, FrameWriter *writer)
809 : FrameWriterUnit(writer)
817 if(temp) delete temp;
821 TGAConfigVideo::TGAConfigVideo(BC_WindowBase *gui, Asset *asset)
822 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
823 gui->get_abs_cursor_x(1), gui->get_abs_cursor_y(1),
829 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB_RLE)));
830 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA_RLE)));
831 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB)));
832 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA)));
833 // *** CONTEXT_HELP ***
834 context_help_set_keyword("Single File Rendering");
837 TGAConfigVideo::~TGAConfigVideo()
839 compression_items.remove_all_objects();
842 void TGAConfigVideo::create_objects()
844 lock_window("TGAConfigVideo::create_objects");
845 int x = xS(10), y = yS(10);
847 add_subwindow(new BC_Title(x, y, _("Compression:")));
848 TGACompression *textbox = new TGACompression(this,
849 x + xS(110), y, asset, &compression_items);
850 textbox->create_objects();
851 add_subwindow(new BC_OKButton(this));
856 int TGAConfigVideo::close_event()
863 TGACompression::TGACompression(TGAConfigVideo *gui, int x, int y,
864 Asset *asset, ArrayList<BC_ListBoxItem*> *compression_items)
865 : BC_PopupTextBox(gui, compression_items,
866 FileTGA::compression_to_str(gui->asset->vcodec),
867 x, y, xS(200), yS(200))
871 int TGACompression::handle_event()
873 strcpy(asset->vcodec, FileTGA::str_to_compression(get_text()));