13 /* Known image types. */
14 #define TGA_TYPE_MAPPED 1
15 #define TGA_TYPE_COLOR 2
16 #define TGA_TYPE_GRAY 3
18 /* Only known compression is RLE */
19 #define TGA_COMP_NONE 0
20 #define TGA_COMP_RLE 1
23 FileTGA::FileTGA(Asset *asset, File *file)
24 : FileList(asset, file, "TGALIST", ".tga", FILE_TGA, FILE_TGA_LIST)
34 int FileTGA::check_sig(Asset *asset)
38 // Test file extension
40 char *ext = strrchr(asset->path, '.');
45 if(!strncasecmp(ext, ".tga", 4)) result = 1;
55 if(!(stream = fopen(asset->path, "rb")))
63 fread(test, 16, 1, stream);
65 if(test[0] == 'T' && test[1] == 'G' && test[2] == 'A' &&
66 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' &&
79 void FileTGA::get_parameters(BC_WindowBase *parent_window,
81 BC_WindowBase* &format_window,
87 TGAConfigVideo *window = new TGAConfigVideo(parent_window, asset);
88 format_window = window;
89 window->create_objects();
98 N_("RGB uncompressed")
99 N_("RGBA uncompressed")
102 #define TGA_RGB_RLE "rle "
103 #define TGA_RGBA_RLE "rlea"
104 #define TGA_RGB "raw "
105 #define TGA_RGBA "rawa"
107 #define TGA_RGB_RLE_NAME "RGB compressed"
108 #define TGA_RGBA_RLE_NAME "RGBA compressed"
109 #define TGA_RGB_NAME "RGB uncompressed"
110 #define TGA_RGBA_NAME "RGBA uncompressed"
112 char* FileTGA::compression_to_str(char *compression)
114 if(!strcasecmp(compression, TGA_RGB_RLE)) return _(TGA_RGB_RLE_NAME);
115 if(!strcasecmp(compression, TGA_RGBA_RLE)) return _(TGA_RGBA_RLE_NAME);
116 if(!strcasecmp(compression, TGA_RGB)) return _(TGA_RGB_NAME);
117 if(!strcasecmp(compression, TGA_RGBA)) return _(TGA_RGBA_NAME);
121 char* FileTGA::str_to_compression(char *string)
123 if(!strcasecmp(compression_to_str(TGA_RGB_RLE), string)) return TGA_RGB_RLE;
124 if(!strcasecmp(compression_to_str(TGA_RGBA_RLE), string)) return TGA_RGBA_RLE;
125 if(!strcasecmp(compression_to_str(TGA_RGB), string)) return TGA_RGB;
126 if(!strcasecmp(compression_to_str(TGA_RGBA), string)) return TGA_RGBA;
130 int FileTGA::can_copy_from(Edit *edit, int64_t position)
132 if(edit->asset->format == FILE_TGA_LIST ||
133 edit->asset->format == FILE_TGA)
140 int FileTGA::colormodel_supported(int colormodel)
145 int FileTGA::get_best_colormodel(Asset *asset, int driver)
147 if(!strcasecmp(asset->vcodec, TGA_RGB_RLE) ||
148 !strcasecmp(asset->vcodec, TGA_RGB)) return BC_RGB888;
149 if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE) ||
150 !strcasecmp(asset->vcodec, TGA_RGBA)) return BC_RGBA8888;
154 int FileTGA::read_frame(VFrame *frame, VFrame *data)
156 read_tga(asset, frame, data, temp);
160 int FileTGA::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
162 TGAUnit *tga_unit = (TGAUnit*)unit;
164 write_tga(asset, frame, data, tga_unit->temp);
168 FrameWriterUnit* FileTGA::new_writer_unit(FrameWriter *writer)
170 return new TGAUnit(this, writer);
173 int64_t FileTGA::get_memory_usage()
175 int64_t result = FileList::get_memory_usage();
176 if(temp) result += temp->get_data_size();
187 #define FOOTERSIZE 26
188 #define HEADERSIZE 18
189 int FileTGA::read_frame_header(char *path)
193 //printf("FileTGA::read_frame_header 1\n");
196 if(!(stream = fopen(path, "rb")))
198 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
202 unsigned char header[HEADERSIZE];
203 fread(header, HEADERSIZE, 1, stream);
206 asset->width = header[12] | (header[13] << 8);
207 asset->height = header[14] | (header[15] << 8);
208 int bpp = header[16];
209 int rle = header[2] & 0x8;
214 strcpy(asset->vcodec, TGA_RGBA_RLE);
216 strcpy(asset->vcodec, TGA_RGBA);
220 strcpy(asset->vcodec, TGA_RGB_RLE);
222 strcpy(asset->vcodec, TGA_RGB);
225 //printf("FileTGA::read_frame_header 2 %d %d\n", asset->width, asset->height);
230 void FileTGA::read_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
233 unsigned char *footer, *header;
235 int64_t file_offset = 0;
237 footer = data->get_data() +
238 data->get_compressed_size() -
240 header = data->get_data();
241 file_offset += HEADERSIZE;
244 int image_compression;
248 image_type = TGA_TYPE_MAPPED;
249 image_compression = TGA_COMP_NONE;
252 image_type = TGA_TYPE_COLOR;
253 image_compression = TGA_COMP_NONE;
256 image_type = TGA_TYPE_GRAY;
257 image_compression = TGA_COMP_NONE;
260 image_type = TGA_TYPE_MAPPED;
261 image_compression = TGA_COMP_RLE;
264 image_type = TGA_TYPE_COLOR;
265 image_compression = TGA_COMP_RLE;
268 image_type = TGA_TYPE_GRAY;
269 image_compression = TGA_COMP_RLE;
275 int idlength = header[0];
276 int colormaptype = header[1];
277 int colormapindex = header[3] + header[4] * 256;
278 int colormaplength = header[5] + header[6] * 256;
279 int colormapsize = header[7];
280 int xorigin = header[8] + header[9] * 256;
281 int yorigin = header[10] + header[11] * 256;
282 int width = header[12] + header[13] * 256;
283 int height = header[14] + header[15] * 256;
284 int bpp = header[16];
285 int bytes = (bpp + 7) / 8;
286 int alphabits = header[17] & 0x0f;
287 int fliphoriz = (header[17] & 0x10) ? 1 : 0;
288 int flipvert = (header[17] & 0x20) ? 0 : 1;
289 int data_size = data->get_compressed_size();
291 if(idlength) file_offset += idlength;
294 unsigned char *tga_cmap;
295 unsigned char colormap[4 * 256];
297 if(colormaptype == 1)
299 int cmap_bytes = (colormapsize + 7) / 8;
300 tga_cmap = data->get_data() + file_offset;
301 file_offset += colormaplength * cmap_bytes;
306 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 1);
309 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 0);
312 upsample(colormap, tga_cmap, colormaplength, cmap_bytes);
317 int source_cmodel = BC_RGB888;
321 source_cmodel = BC_RGBA8888;
324 source_cmodel = BC_RGB888;
329 VFrame *output_frame;
330 if(frame->get_color_model() == source_cmodel)
332 output_frame = frame;
336 if(temp && temp->get_color_model() != source_cmodel)
344 temp = new VFrame(0, width, height, source_cmodel);
351 for(int i = height - 1; i >= 0; i--)
353 read_line(output_frame->get_rows()[i],
368 for(int i = 0; i < height; i++)
370 read_line(output_frame->get_rows()[i],
384 if(output_frame != frame)
386 cmodel_transfer(frame->get_rows(),
387 output_frame->get_rows(),
391 output_frame->get_y(),
392 output_frame->get_u(),
393 output_frame->get_v(),
402 output_frame->get_color_model(),
403 frame->get_color_model(),
410 void FileTGA::write_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
412 unsigned char header[18];
413 unsigned char footer[26];
414 int64_t file_offset = 0;
417 int dest_cmodel = BC_RGB888;
419 //printf("FileTGA::write_tga 1\n");
423 if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE))
428 header[16] = 32; /* bpp */
429 header[17] = 0x28; /* alpha + orientation */
430 dest_cmodel = BC_RGBA8888;
433 if(!strcasecmp(asset->vcodec, TGA_RGBA))
438 header[16] = 32; /* bpp */
439 header[17] = 0x28; /* alpha + orientation */
440 dest_cmodel = BC_RGBA8888;
443 if(!strcasecmp(asset->vcodec, TGA_RGB_RLE))
448 header[16] = 24; /* bpp */
449 header[17] = 0x20; /* alpha + orientation */
450 dest_cmodel = BC_RGB888;
457 header[16] = 24; /* bpp */
458 header[17] = 0x20; /* alpha + orientation */
459 dest_cmodel = BC_RGB888;
461 header[3] = header[4] = header[5] = header[6] = header[7] = 0;
462 //printf("FileTGA::write_tga 1\n");
465 if(frame->get_color_model() == dest_cmodel)
471 if(temp && temp->get_color_model() != dest_cmodel)
479 temp = new VFrame(0, frame->get_w(), frame->get_h(), dest_cmodel);
483 cmodel_transfer(input_frame->get_rows(),
485 input_frame->get_y(),
486 input_frame->get_u(),
487 input_frame->get_v(),
499 frame->get_color_model(),
500 input_frame->get_color_model(),
505 //printf("FileTGA::write_tga 1\n");
509 header[8] = header[9] = 0;
510 header[10] = header[11] = 0;
512 header[12] = input_frame->get_w() % 256;
513 header[13] = input_frame->get_w() / 256;
515 header[14] = input_frame->get_h() % 256;
516 header[15] = input_frame->get_h() / 256;
517 //printf("FileTGA::write_tga 1\n");
519 write_data(header, data, file_offset, sizeof(header));
520 //printf("FileTGA::write_tga 1\n");
522 unsigned char *output = new unsigned char[out_bpp * input_frame->get_w()];
523 //printf("FileTGA::write_tga 1\n");
524 for(int i = 0; i < input_frame->get_h(); i++)
526 //printf("FileTGA::write_tga 2\n");
527 bgr2rgb(output, input_frame->get_rows()[i], input_frame->get_w(), out_bpp, (out_bpp == 4));
528 //printf("FileTGA::write_tga 3\n");
532 //printf("FileTGA::write_tga 4\n");
534 input_frame->get_w(),
538 //printf("FileTGA::write_tga 5\n");
542 //printf("FileTGA::write_tga 6\n");
546 input_frame->get_w() * out_bpp);
547 //printf("FileTGA::write_tga 7\n");
550 //printf("FileTGA::write_tga 8\n");
552 //printf("FileTGA::write_tga 9\n");
555 void FileTGA::write_data(unsigned char *buffer,
557 int64_t &file_offset,
560 //printf("FileTGA::write_data 1 %d\n", len);
561 if(data->get_compressed_allocated() <= data->get_compressed_size() + len)
563 data->allocate_compressed_data((data->get_compressed_size() + len) * 2);
565 //printf("FileTGA::write_data 1 %d\n", len);
567 bcopy(buffer, data->get_data() + file_offset, len);
568 //printf("FileTGA::write_data 1 %d\n", len);
570 //printf("FileTGA::write_data 1 %d\n", len);
571 data->set_compressed_size(file_offset);
572 //printf("FileTGA::write_data 2 %d\n", len);
575 void FileTGA::read_line(unsigned char *row,
577 int64_t &file_offset,
580 int image_compression,
587 if(file_offset >= data_size) return;
588 if(image_compression == TGA_COMP_RLE)
598 if(file_offset + bytes * width <= data_size)
599 bcopy(data + file_offset, row, bytes * width);
600 file_offset += bytes * width;
605 flip_line(row, bytes, width);
608 if(image_type == TGA_TYPE_COLOR)
612 upsample(row, row, width, bytes);
616 bgr2rgb(row, row, width, bytes, alphabits);
625 void FileTGA::flip_line(unsigned char *row, int bytes, int width)
631 alt = row + (bytes * (width - 1));
633 for (x = 0; x * 2 <= width; x++)
635 for(s = 0; s < bytes; ++s)
647 void FileTGA::rle_read(unsigned char *row,
649 int64_t &file_offset,
655 unsigned char sample[4];
658 for(int x = 0; x < width; x++)
660 if(repeat == 0 && direct == 0)
662 head = data[file_offset++];
671 bcopy(data + file_offset, sample, bytes);
672 file_offset += bytes;
682 for(int k = 0; k < bytes; k++)
691 bcopy(data + file_offset, row, bytes);
692 file_offset += bytes;
701 void FileTGA::rle_write(unsigned char *buffer,
705 int64_t &file_offset)
709 unsigned char *from = buffer;
710 unsigned char output;
713 for(x = 1; x < width; ++x)
715 /* next pixel is different */
716 if(memcmp(buffer, buffer + bytes, bytes))
720 output = 128 + repeat;
721 write_data(&output, frame, file_offset, 1);
722 write_data(from, frame, file_offset, bytes);
723 from = buffer + bytes;
733 /* next pixel is the same */
738 write_data(&output, frame, file_offset, 1);
739 write_data(from, frame, file_offset, bytes * direct);
753 write_data(&output, frame, file_offset, 1);
754 write_data(from, frame, file_offset, bytes);
755 from = buffer + bytes;
763 write_data(&output, frame, file_offset, 1);
764 write_data(from, frame, file_offset, direct * bytes);
765 from = buffer + bytes;
775 output = 128 + repeat;
776 write_data(&output, frame, file_offset, 1);
777 write_data(from, frame, file_offset, bytes);
782 write_data(&output, frame, file_offset, 1);
783 write_data(from, frame, file_offset, bytes * (direct + 1));
788 void FileTGA::bgr2rgb(unsigned char *dest,
795 unsigned char r, g, b;
799 for(x = 0; x < width; x++)
814 for(x = 0; x < width; x++)
828 void FileTGA::upsample(unsigned char *dest,
835 dest += (width - 1) * 3;
836 src += (width - 1) * bytes;
837 for(x = width - 1; x >= 0; x--)
839 dest[0] = ((src[1] << 1) & 0xf8);
840 dest[0] += (dest[0] >> 5);
842 dest[1] = ((src[0] & 0xe0) >> 2) + ((src[1] & 0x03) << 6);
843 dest[1] += (dest[1] >> 5);
845 dest[2] = ((src[0] << 3) & 0xf8);
846 dest[2] += (dest[2] >> 5);
861 TGAUnit::TGAUnit(FileTGA *file, FrameWriter *writer)
862 : FrameWriterUnit(writer)
870 if(temp) delete temp;
885 TGAConfigVideo::TGAConfigVideo(BC_WindowBase *gui, Asset *asset)
886 : BC_Window(PROGRAM_NAME ": Video Compression",
887 gui->get_abs_cursor_x(1),
888 gui->get_abs_cursor_y(1),
895 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB_RLE)));
896 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA_RLE)));
897 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB)));
898 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA)));
901 TGAConfigVideo::~TGAConfigVideo()
903 compression_items.remove_all_objects();
906 int TGAConfigVideo::create_objects()
910 add_subwindow(new BC_Title(x, y, _("Compression:")));
911 TGACompression *textbox = new TGACompression(this,
916 textbox->create_objects();
917 add_subwindow(new BC_OKButton(this));
921 int TGAConfigVideo::close_event()
928 TGACompression::TGACompression(TGAConfigVideo *gui,
932 ArrayList<BC_ListBoxItem*> *compression_items)
933 : BC_PopupTextBox(gui,
935 FileTGA::compression_to_str(gui->asset->vcodec),
943 int TGACompression::handle_event()
945 strcpy(asset->vcodec, FileTGA::str_to_compression(get_text()));