12 /* Known image types. */
13 #define TGA_TYPE_MAPPED 1
14 #define TGA_TYPE_COLOR 2
15 #define TGA_TYPE_GRAY 3
17 /* Only known compression is RLE */
18 #define TGA_COMP_NONE 0
19 #define TGA_COMP_RLE 1
22 FileTGA::FileTGA(Asset *asset, File *file)
23 : FileList(asset, file, "TGALIST", ".tga", FILE_TGA, FILE_TGA_LIST)
33 int FileTGA::check_sig(Asset *asset)
37 // Test file extension
39 char *ext = strrchr(asset->path, '.');
44 if(!strncasecmp(ext, ".tga", 4)) result = 1;
54 if(!(stream = fopen(asset->path, "rb")))
62 fread(test, 16, 1, stream);
64 if(test[0] == 'T' && test[1] == 'G' && test[2] == 'A' &&
65 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' &&
78 void FileTGA::get_parameters(BC_WindowBase *parent_window,
80 BC_WindowBase* &format_window,
86 TGAConfigVideo *window = new TGAConfigVideo(parent_window, asset);
87 format_window = window;
88 window->create_objects();
97 N_("RGB uncompressed")
98 N_("RGBA uncompressed")
101 #define TGA_RGB_RLE "rle "
102 #define TGA_RGBA_RLE "rlea"
103 #define TGA_RGB "raw "
104 #define TGA_RGBA "rawa"
106 #define TGA_RGB_RLE_NAME "RGB compressed"
107 #define TGA_RGBA_RLE_NAME "RGBA compressed"
108 #define TGA_RGB_NAME "RGB uncompressed"
109 #define TGA_RGBA_NAME "RGBA uncompressed"
111 char* FileTGA::compression_to_str(char *compression)
113 if(!strcasecmp(compression, TGA_RGB_RLE)) return _(TGA_RGB_RLE_NAME);
114 if(!strcasecmp(compression, TGA_RGBA_RLE)) return _(TGA_RGBA_RLE_NAME);
115 if(!strcasecmp(compression, TGA_RGB)) return _(TGA_RGB_NAME);
116 if(!strcasecmp(compression, TGA_RGBA)) return _(TGA_RGBA_NAME);
120 char* FileTGA::str_to_compression(char *string)
122 if(!strcasecmp(compression_to_str(TGA_RGB_RLE), string)) return TGA_RGB_RLE;
123 if(!strcasecmp(compression_to_str(TGA_RGBA_RLE), string)) return TGA_RGBA_RLE;
124 if(!strcasecmp(compression_to_str(TGA_RGB), string)) return TGA_RGB;
125 if(!strcasecmp(compression_to_str(TGA_RGBA), string)) return TGA_RGBA;
129 int FileTGA::can_copy_from(Edit *edit, int64_t position)
131 if(edit->asset->format == FILE_TGA_LIST ||
132 edit->asset->format == FILE_TGA)
139 int FileTGA::colormodel_supported(int colormodel)
144 int FileTGA::get_best_colormodel(Asset *asset, int driver)
146 if(!strcasecmp(asset->vcodec, TGA_RGB_RLE) ||
147 !strcasecmp(asset->vcodec, TGA_RGB)) return BC_RGB888;
148 if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE) ||
149 !strcasecmp(asset->vcodec, TGA_RGBA)) return BC_RGBA8888;
153 int FileTGA::read_frame(VFrame *frame, VFrame *data)
155 read_tga(asset, frame, data, temp);
159 int FileTGA::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
161 TGAUnit *tga_unit = (TGAUnit*)unit;
163 write_tga(asset, frame, data, tga_unit->temp);
167 FrameWriterUnit* FileTGA::new_writer_unit(FrameWriter *writer)
169 return new TGAUnit(this, writer);
172 int FileTGA::get_memory_usage()
174 int result = FileList::get_memory_usage();
175 if(temp) result += temp->get_data_size();
186 #define FOOTERSIZE 26
187 #define HEADERSIZE 18
188 int FileTGA::read_frame_header(char *path)
192 //printf("FileTGA::read_frame_header 1\n");
195 if(!(stream = fopen(path, "rb")))
197 perror("FileTGA::read_frame_header");
201 unsigned char header[HEADERSIZE];
202 fread(header, HEADERSIZE, 1, stream);
205 asset->width = header[12] | (header[13] << 8);
206 asset->height = header[14] | (header[15] << 8);
207 int bpp = header[16];
208 int rle = header[2] & 0x8;
213 strcpy(asset->vcodec, TGA_RGBA_RLE);
215 strcpy(asset->vcodec, TGA_RGBA);
219 strcpy(asset->vcodec, TGA_RGB_RLE);
221 strcpy(asset->vcodec, TGA_RGB);
224 //printf("FileTGA::read_frame_header 2 %d %d\n", asset->width, asset->height);
229 void FileTGA::read_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
232 unsigned char *footer, *header;
234 int64_t file_offset = 0;
236 footer = data->get_data() +
237 data->get_compressed_size() -
239 header = data->get_data();
240 file_offset += HEADERSIZE;
243 int image_compression;
247 image_type = TGA_TYPE_MAPPED;
248 image_compression = TGA_COMP_NONE;
251 image_type = TGA_TYPE_COLOR;
252 image_compression = TGA_COMP_NONE;
255 image_type = TGA_TYPE_GRAY;
256 image_compression = TGA_COMP_NONE;
259 image_type = TGA_TYPE_MAPPED;
260 image_compression = TGA_COMP_RLE;
263 image_type = TGA_TYPE_COLOR;
264 image_compression = TGA_COMP_RLE;
267 image_type = TGA_TYPE_GRAY;
268 image_compression = TGA_COMP_RLE;
274 int idlength = header[0];
275 int colormaptype = header[1];
276 int colormapindex = header[3] + header[4] * 256;
277 int colormaplength = header[5] + header[6] * 256;
278 int colormapsize = header[7];
279 int xorigin = header[8] + header[9] * 256;
280 int yorigin = header[10] + header[11] * 256;
281 int width = header[12] + header[13] * 256;
282 int height = header[14] + header[15] * 256;
283 int bpp = header[16];
284 int bytes = (bpp + 7) / 8;
285 int alphabits = header[17] & 0x0f;
286 int fliphoriz = (header[17] & 0x10) ? 1 : 0;
287 int flipvert = (header[17] & 0x20) ? 0 : 1;
288 int data_size = data->get_compressed_size();
290 if(idlength) file_offset += idlength;
293 unsigned char *tga_cmap;
294 unsigned char colormap[4 * 256];
296 if(colormaptype == 1)
298 int cmap_bytes = (colormapsize + 7) / 8;
299 tga_cmap = data->get_data() + file_offset;
300 file_offset += colormaplength * cmap_bytes;
305 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 1);
308 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 0);
311 upsample(colormap, tga_cmap, colormaplength, cmap_bytes);
316 int source_cmodel = BC_RGB888;
320 source_cmodel = BC_RGBA8888;
323 source_cmodel = BC_RGB888;
328 VFrame *output_frame;
329 if(frame->get_color_model() == source_cmodel)
331 output_frame = frame;
335 if(temp && temp->get_color_model() != source_cmodel)
343 temp = new VFrame(0, width, height, source_cmodel);
350 for(int i = height - 1; i >= 0; i--)
352 read_line(output_frame->get_rows()[i],
367 for(int i = 0; i < height; i++)
369 read_line(output_frame->get_rows()[i],
383 if(output_frame != frame)
385 cmodel_transfer(frame->get_rows(),
386 output_frame->get_rows(),
390 output_frame->get_y(),
391 output_frame->get_u(),
392 output_frame->get_v(),
401 output_frame->get_color_model(),
402 frame->get_color_model(),
409 void FileTGA::write_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
411 unsigned char header[18];
412 unsigned char footer[26];
413 int64_t file_offset = 0;
416 int dest_cmodel = BC_RGB888;
418 //printf("FileTGA::write_tga 1\n");
422 if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE))
427 header[16] = 32; /* bpp */
428 header[17] = 0x28; /* alpha + orientation */
429 dest_cmodel = BC_RGBA8888;
432 if(!strcasecmp(asset->vcodec, TGA_RGBA))
437 header[16] = 32; /* bpp */
438 header[17] = 0x28; /* alpha + orientation */
439 dest_cmodel = BC_RGBA8888;
442 if(!strcasecmp(asset->vcodec, TGA_RGB_RLE))
447 header[16] = 24; /* bpp */
448 header[17] = 0x20; /* alpha + orientation */
449 dest_cmodel = BC_RGB888;
456 header[16] = 24; /* bpp */
457 header[17] = 0x20; /* alpha + orientation */
458 dest_cmodel = BC_RGB888;
460 header[3] = header[4] = header[5] = header[6] = header[7] = 0;
461 //printf("FileTGA::write_tga 1\n");
464 if(frame->get_color_model() == dest_cmodel)
470 if(temp && temp->get_color_model() != dest_cmodel)
478 temp = new VFrame(0, frame->get_w(), frame->get_h(), dest_cmodel);
482 cmodel_transfer(input_frame->get_rows(),
484 input_frame->get_y(),
485 input_frame->get_u(),
486 input_frame->get_v(),
498 frame->get_color_model(),
499 input_frame->get_color_model(),
504 //printf("FileTGA::write_tga 1\n");
508 header[8] = header[9] = 0;
509 header[10] = header[11] = 0;
511 header[12] = input_frame->get_w() % 256;
512 header[13] = input_frame->get_w() / 256;
514 header[14] = input_frame->get_h() % 256;
515 header[15] = input_frame->get_h() / 256;
516 //printf("FileTGA::write_tga 1\n");
518 write_data(header, data, file_offset, sizeof(header));
519 //printf("FileTGA::write_tga 1\n");
521 unsigned char *output = new unsigned char[out_bpp * input_frame->get_w()];
522 //printf("FileTGA::write_tga 1\n");
523 for(int i = 0; i < input_frame->get_h(); i++)
525 //printf("FileTGA::write_tga 2\n");
526 bgr2rgb(output, input_frame->get_rows()[i], input_frame->get_w(), out_bpp, (out_bpp == 4));
527 //printf("FileTGA::write_tga 3\n");
531 //printf("FileTGA::write_tga 4\n");
533 input_frame->get_w(),
537 //printf("FileTGA::write_tga 5\n");
541 //printf("FileTGA::write_tga 6\n");
545 input_frame->get_w() * out_bpp);
546 //printf("FileTGA::write_tga 7\n");
549 //printf("FileTGA::write_tga 8\n");
551 //printf("FileTGA::write_tga 9\n");
554 void FileTGA::write_data(unsigned char *buffer,
556 int64_t &file_offset,
559 //printf("FileTGA::write_data 1 %d\n", len);
560 if(data->get_compressed_allocated() <= data->get_compressed_size() + len)
562 data->allocate_compressed_data((data->get_compressed_size() + len) * 2);
564 //printf("FileTGA::write_data 1 %d\n", len);
566 bcopy(buffer, data->get_data() + file_offset, len);
567 //printf("FileTGA::write_data 1 %d\n", len);
569 //printf("FileTGA::write_data 1 %d\n", len);
570 data->set_compressed_size(file_offset);
571 //printf("FileTGA::write_data 2 %d\n", len);
574 void FileTGA::read_line(unsigned char *row,
576 int64_t &file_offset,
579 int image_compression,
586 if(file_offset >= data_size) return;
587 if(image_compression == TGA_COMP_RLE)
597 if(file_offset + bytes * width <= data_size)
598 bcopy(data + file_offset, row, bytes * width);
599 file_offset += bytes * width;
604 flip_line(row, bytes, width);
607 if(image_type == TGA_TYPE_COLOR)
611 upsample(row, row, width, bytes);
615 bgr2rgb(row, row, width, bytes, alphabits);
624 void FileTGA::flip_line(unsigned char *row, int bytes, int width)
630 alt = row + (bytes * (width - 1));
632 for (x = 0; x * 2 <= width; x++)
634 for(s = 0; s < bytes; ++s)
646 void FileTGA::rle_read(unsigned char *row,
648 int64_t &file_offset,
654 unsigned char sample[4];
657 for(int x = 0; x < width; x++)
659 if(repeat == 0 && direct == 0)
661 head = data[file_offset++];
670 bcopy(data + file_offset, sample, bytes);
671 file_offset += bytes;
681 for(int k = 0; k < bytes; k++)
690 bcopy(data + file_offset, row, bytes);
691 file_offset += bytes;
700 void FileTGA::rle_write(unsigned char *buffer,
704 int64_t &file_offset)
708 unsigned char *from = buffer;
709 unsigned char output;
712 for(x = 1; x < width; ++x)
714 /* next pixel is different */
715 if(memcmp(buffer, buffer + bytes, bytes))
719 output = 128 + repeat;
720 write_data(&output, frame, file_offset, 1);
721 write_data(from, frame, file_offset, bytes);
722 from = buffer + bytes;
732 /* next pixel is the same */
737 write_data(&output, frame, file_offset, 1);
738 write_data(from, frame, file_offset, bytes * direct);
752 write_data(&output, frame, file_offset, 1);
753 write_data(from, frame, file_offset, bytes);
754 from = buffer + bytes;
762 write_data(&output, frame, file_offset, 1);
763 write_data(from, frame, file_offset, direct * bytes);
764 from = buffer + bytes;
774 output = 128 + repeat;
775 write_data(&output, frame, file_offset, 1);
776 write_data(from, frame, file_offset, bytes);
781 write_data(&output, frame, file_offset, 1);
782 write_data(from, frame, file_offset, bytes * (direct + 1));
787 void FileTGA::bgr2rgb(unsigned char *dest,
794 unsigned char r, g, b;
798 for(x = 0; x < width; x++)
813 for(x = 0; x < width; x++)
827 void FileTGA::upsample(unsigned char *dest,
834 dest += (width - 1) * 3;
835 src += (width - 1) * bytes;
836 for(x = width - 1; x >= 0; x--)
838 dest[0] = ((src[1] << 1) & 0xf8);
839 dest[0] += (dest[0] >> 5);
841 dest[1] = ((src[0] & 0xe0) >> 2) + ((src[1] & 0x03) << 6);
842 dest[1] += (dest[1] >> 5);
844 dest[2] = ((src[0] << 3) & 0xf8);
845 dest[2] += (dest[2] >> 5);
860 TGAUnit::TGAUnit(FileTGA *file, FrameWriter *writer)
861 : FrameWriterUnit(writer)
869 if(temp) delete temp;
884 TGAConfigVideo::TGAConfigVideo(BC_WindowBase *gui, Asset *asset)
885 : BC_Window(PROGRAM_NAME ": Video Compression",
886 gui->get_abs_cursor_x(1),
887 gui->get_abs_cursor_y(1),
894 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB_RLE)));
895 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA_RLE)));
896 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB)));
897 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA)));
900 TGAConfigVideo::~TGAConfigVideo()
902 compression_items.remove_all_objects();
905 int TGAConfigVideo::create_objects()
909 add_subwindow(new BC_Title(x, y, _("Compression:")));
910 TGACompression *textbox = new TGACompression(this,
915 textbox->create_objects();
916 add_subwindow(new BC_OKButton(this));
920 int TGAConfigVideo::close_event()
927 TGACompression::TGACompression(TGAConfigVideo *gui,
931 ArrayList<BC_ListBoxItem*> *compression_items)
932 : BC_PopupTextBox(gui,
934 FileTGA::compression_to_str(gui->asset->vcodec),
942 int TGACompression::handle_event()
944 strcpy(asset->vcodec, FileTGA::str_to_compression(get_text()));