11 #define _(String) gettext(String)
12 #define gettext_noop(String) String
13 #define N_(String) gettext_noop (String)
15 /* Known image types. */
16 #define TGA_TYPE_MAPPED 1
17 #define TGA_TYPE_COLOR 2
18 #define TGA_TYPE_GRAY 3
20 /* Only known compression is RLE */
21 #define TGA_COMP_NONE 0
22 #define TGA_COMP_RLE 1
25 FileTGA::FileTGA(Asset *asset, File *file)
26 : FileList(asset, file, "TGALIST", ".tga", FILE_TGA, FILE_TGA_LIST)
36 int FileTGA::check_sig(Asset *asset)
38 //printf("FileTGA::check_sig 1\n");
39 // Test file extension
41 char *ext = strrchr(asset->path, '.');
44 if(!strncasecmp(ext, ".tga", 4)) result = 1;
52 if(!(stream = fopen(asset->path, "rb")))
60 fread(test, 16, 1, stream);
62 if(test[0] == 'T' && test[1] == 'G' && test[2] == 'A' &&
63 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' &&
71 //printf("FileTGA::check_sig 2\n");
76 void FileTGA::get_parameters(BC_WindowBase *parent_window,
78 BC_WindowBase* &format_window,
84 TGAConfigVideo *window = new TGAConfigVideo(parent_window, asset);
85 format_window = window;
86 window->create_objects();
95 N_("RGB uncompressed")
96 N_("RGBA uncompressed")
99 #define TGA_RGB_RLE "rle "
100 #define TGA_RGBA_RLE "rlea"
101 #define TGA_RGB "raw "
102 #define TGA_RGBA "rawa"
104 #define TGA_RGB_RLE_NAME "RGB compressed"
105 #define TGA_RGBA_RLE_NAME "RGBA compressed"
106 #define TGA_RGB_NAME "RGB uncompressed"
107 #define TGA_RGBA_NAME "RGBA uncompressed"
109 char* FileTGA::compression_to_str(char *compression)
111 if(!strcasecmp(compression, TGA_RGB_RLE)) return _(TGA_RGB_RLE_NAME);
112 if(!strcasecmp(compression, TGA_RGBA_RLE)) return _(TGA_RGBA_RLE_NAME);
113 if(!strcasecmp(compression, TGA_RGB)) return _(TGA_RGB_NAME);
114 if(!strcasecmp(compression, TGA_RGBA)) return _(TGA_RGBA_NAME);
118 char* FileTGA::str_to_compression(char *string)
120 if(!strcasecmp(compression_to_str(TGA_RGB_RLE), string)) return TGA_RGB_RLE;
121 if(!strcasecmp(compression_to_str(TGA_RGBA_RLE), string)) return TGA_RGBA_RLE;
122 if(!strcasecmp(compression_to_str(TGA_RGB), string)) return TGA_RGB;
123 if(!strcasecmp(compression_to_str(TGA_RGBA), string)) return TGA_RGBA;
127 int FileTGA::can_copy_from(Edit *edit, int64_t position)
129 if(edit->asset->format == FILE_TGA_LIST ||
130 edit->asset->format == FILE_TGA)
137 int FileTGA::colormodel_supported(int colormodel)
142 int FileTGA::get_best_colormodel(Asset *asset, int driver)
144 if(!strcasecmp(asset->vcodec, TGA_RGB_RLE) ||
145 !strcasecmp(asset->vcodec, TGA_RGB)) return BC_RGB888;
146 if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE) ||
147 !strcasecmp(asset->vcodec, TGA_RGBA)) return BC_RGBA8888;
151 int FileTGA::read_frame(VFrame *frame, VFrame *data)
153 read_tga(asset, frame, data, temp);
157 int FileTGA::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
159 TGAUnit *tga_unit = (TGAUnit*)unit;
161 write_tga(asset, frame, data, tga_unit->temp);
165 FrameWriterUnit* FileTGA::new_writer_unit(FrameWriter *writer)
167 return new TGAUnit(this, writer);
170 int FileTGA::get_memory_usage()
172 int result = FileList::get_memory_usage();
173 if(temp) result += temp->get_data_size();
184 #define FOOTERSIZE 26
185 #define HEADERSIZE 18
186 int FileTGA::read_frame_header(char *path)
190 //printf("FileTGA::read_frame_header 1\n");
193 if(!(stream = fopen(path, "rb")))
195 perror("FileTGA::read_frame_header");
199 unsigned char header[HEADERSIZE];
200 fread(header, HEADERSIZE, 1, stream);
203 asset->width = header[12] | (header[13] << 8);
204 asset->height = header[14] | (header[15] << 8);
205 int bpp = header[16];
206 int rle = header[2] & 0x8;
211 strcpy(asset->vcodec, TGA_RGBA_RLE);
213 strcpy(asset->vcodec, TGA_RGBA);
217 strcpy(asset->vcodec, TGA_RGB_RLE);
219 strcpy(asset->vcodec, TGA_RGB);
222 //printf("FileTGA::read_frame_header 2 %d %d\n", asset->width, asset->height);
227 void FileTGA::read_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
230 unsigned char *footer, *header;
232 int64_t file_offset = 0;
234 footer = data->get_data() +
235 data->get_compressed_size() -
237 header = data->get_data();
238 file_offset += HEADERSIZE;
241 int image_compression;
245 image_type = TGA_TYPE_MAPPED;
246 image_compression = TGA_COMP_NONE;
249 image_type = TGA_TYPE_COLOR;
250 image_compression = TGA_COMP_NONE;
253 image_type = TGA_TYPE_GRAY;
254 image_compression = TGA_COMP_NONE;
257 image_type = TGA_TYPE_MAPPED;
258 image_compression = TGA_COMP_RLE;
261 image_type = TGA_TYPE_COLOR;
262 image_compression = TGA_COMP_RLE;
265 image_type = TGA_TYPE_GRAY;
266 image_compression = TGA_COMP_RLE;
272 int idlength = header[0];
273 int colormaptype = header[1];
274 int colormapindex = header[3] + header[4] * 256;
275 int colormaplength = header[5] + header[6] * 256;
276 int colormapsize = header[7];
277 int xorigin = header[8] + header[9] * 256;
278 int yorigin = header[10] + header[11] * 256;
279 int width = header[12] + header[13] * 256;
280 int height = header[14] + header[15] * 256;
281 int bpp = header[16];
282 int bytes = (bpp + 7) / 8;
283 int alphabits = header[17] & 0x0f;
284 int fliphoriz = (header[17] & 0x10) ? 1 : 0;
285 int flipvert = (header[17] & 0x20) ? 0 : 1;
286 int data_size = data->get_compressed_size();
288 if(idlength) file_offset += idlength;
291 unsigned char *tga_cmap;
292 unsigned char colormap[4 * 256];
294 if(colormaptype == 1)
296 int cmap_bytes = (colormapsize + 7) / 8;
297 tga_cmap = data->get_data() + file_offset;
298 file_offset += colormaplength * cmap_bytes;
303 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 1);
306 bgr2rgb(colormap, tga_cmap, colormaplength, cmap_bytes, 0);
309 upsample(colormap, tga_cmap, colormaplength, cmap_bytes);
314 int source_cmodel = BC_RGB888;
318 source_cmodel = BC_RGBA8888;
321 source_cmodel = BC_RGB888;
326 VFrame *output_frame;
327 if(frame->get_color_model() == source_cmodel)
329 output_frame = frame;
333 if(temp && temp->get_color_model() != source_cmodel)
341 temp = new VFrame(0, width, height, source_cmodel);
348 for(int i = height - 1; i >= 0; i--)
350 read_line(output_frame->get_rows()[i],
365 for(int i = 0; i < height; i++)
367 read_line(output_frame->get_rows()[i],
381 if(output_frame != frame)
383 cmodel_transfer(frame->get_rows(),
384 output_frame->get_rows(),
388 output_frame->get_y(),
389 output_frame->get_u(),
390 output_frame->get_v(),
399 output_frame->get_color_model(),
400 frame->get_color_model(),
407 void FileTGA::write_tga(Asset *asset, VFrame *frame, VFrame *data, VFrame* &temp)
409 unsigned char header[18];
410 unsigned char footer[26];
411 int64_t file_offset = 0;
414 int dest_cmodel = BC_RGB888;
416 //printf("FileTGA::write_tga 1\n");
420 if(!strcasecmp(asset->vcodec, TGA_RGBA_RLE))
425 header[16] = 32; /* bpp */
426 header[17] = 0x28; /* alpha + orientation */
427 dest_cmodel = BC_RGBA8888;
430 if(!strcasecmp(asset->vcodec, TGA_RGBA))
435 header[16] = 32; /* bpp */
436 header[17] = 0x28; /* alpha + orientation */
437 dest_cmodel = BC_RGBA8888;
440 if(!strcasecmp(asset->vcodec, TGA_RGB_RLE))
445 header[16] = 24; /* bpp */
446 header[17] = 0x20; /* alpha + orientation */
447 dest_cmodel = BC_RGB888;
454 header[16] = 24; /* bpp */
455 header[17] = 0x20; /* alpha + orientation */
456 dest_cmodel = BC_RGB888;
458 header[3] = header[4] = header[5] = header[6] = header[7] = 0;
459 //printf("FileTGA::write_tga 1\n");
462 if(frame->get_color_model() == dest_cmodel)
468 if(temp && temp->get_color_model() != dest_cmodel)
476 temp = new VFrame(0, frame->get_w(), frame->get_h(), dest_cmodel);
480 cmodel_transfer(input_frame->get_rows(),
482 input_frame->get_y(),
483 input_frame->get_u(),
484 input_frame->get_v(),
496 frame->get_color_model(),
497 input_frame->get_color_model(),
502 //printf("FileTGA::write_tga 1\n");
506 header[8] = header[9] = 0;
507 header[10] = header[11] = 0;
509 header[12] = input_frame->get_w() % 256;
510 header[13] = input_frame->get_w() / 256;
512 header[14] = input_frame->get_h() % 256;
513 header[15] = input_frame->get_h() / 256;
514 //printf("FileTGA::write_tga 1\n");
516 write_data(header, data, file_offset, sizeof(header));
517 //printf("FileTGA::write_tga 1\n");
519 unsigned char *output = new unsigned char[out_bpp * input_frame->get_w()];
520 //printf("FileTGA::write_tga 1\n");
521 for(int i = 0; i < input_frame->get_h(); i++)
523 //printf("FileTGA::write_tga 2\n");
524 bgr2rgb(output, input_frame->get_rows()[i], input_frame->get_w(), out_bpp, (out_bpp == 4));
525 //printf("FileTGA::write_tga 3\n");
529 //printf("FileTGA::write_tga 4\n");
531 input_frame->get_w(),
535 //printf("FileTGA::write_tga 5\n");
539 //printf("FileTGA::write_tga 6\n");
543 input_frame->get_w() * out_bpp);
544 //printf("FileTGA::write_tga 7\n");
547 //printf("FileTGA::write_tga 8\n");
549 //printf("FileTGA::write_tga 9\n");
552 void FileTGA::write_data(unsigned char *buffer,
554 int64_t &file_offset,
557 //printf("FileTGA::write_data 1 %d\n", len);
558 if(data->get_compressed_allocated() <= data->get_compressed_size() + len)
560 data->allocate_compressed_data((data->get_compressed_size() + len) * 2);
562 //printf("FileTGA::write_data 1 %d\n", len);
564 bcopy(buffer, data->get_data() + file_offset, len);
565 //printf("FileTGA::write_data 1 %d\n", len);
567 //printf("FileTGA::write_data 1 %d\n", len);
568 data->set_compressed_size(file_offset);
569 //printf("FileTGA::write_data 2 %d\n", len);
572 void FileTGA::read_line(unsigned char *row,
574 int64_t &file_offset,
577 int image_compression,
584 if(file_offset >= data_size) return;
585 if(image_compression == TGA_COMP_RLE)
595 if(file_offset + bytes * width <= data_size)
596 bcopy(data + file_offset, row, bytes * width);
597 file_offset += bytes * width;
602 flip_line(row, bytes, width);
605 if(image_type == TGA_TYPE_COLOR)
609 upsample(row, row, width, bytes);
613 bgr2rgb(row, row, width, bytes, alphabits);
622 void FileTGA::flip_line(unsigned char *row, int bytes, int width)
628 alt = row + (bytes * (width - 1));
630 for (x = 0; x * 2 <= width; x++)
632 for(s = 0; s < bytes; ++s)
644 void FileTGA::rle_read(unsigned char *row,
646 int64_t &file_offset,
652 unsigned char sample[4];
655 for(int x = 0; x < width; x++)
657 if(repeat == 0 && direct == 0)
659 head = data[file_offset++];
668 bcopy(data + file_offset, sample, bytes);
669 file_offset += bytes;
679 for(int k = 0; k < bytes; k++)
688 bcopy(data + file_offset, row, bytes);
689 file_offset += bytes;
698 void FileTGA::rle_write(unsigned char *buffer,
702 int64_t &file_offset)
706 unsigned char *from = buffer;
707 unsigned char output;
710 for(x = 1; x < width; ++x)
712 /* next pixel is different */
713 if(memcmp(buffer, buffer + bytes, bytes))
717 output = 128 + repeat;
718 write_data(&output, frame, file_offset, 1);
719 write_data(from, frame, file_offset, bytes);
720 from = buffer + bytes;
730 /* next pixel is the same */
735 write_data(&output, frame, file_offset, 1);
736 write_data(from, frame, file_offset, bytes * direct);
750 write_data(&output, frame, file_offset, 1);
751 write_data(from, frame, file_offset, bytes);
752 from = buffer + bytes;
760 write_data(&output, frame, file_offset, 1);
761 write_data(from, frame, file_offset, direct * bytes);
762 from = buffer + bytes;
772 output = 128 + repeat;
773 write_data(&output, frame, file_offset, 1);
774 write_data(from, frame, file_offset, bytes);
779 write_data(&output, frame, file_offset, 1);
780 write_data(from, frame, file_offset, bytes * (direct + 1));
785 void FileTGA::bgr2rgb(unsigned char *dest,
792 unsigned char r, g, b;
796 for(x = 0; x < width; x++)
811 for(x = 0; x < width; x++)
825 void FileTGA::upsample(unsigned char *dest,
832 dest += (width - 1) * 3;
833 src += (width - 1) * bytes;
834 for(x = width - 1; x >= 0; x--)
836 dest[0] = ((src[1] << 1) & 0xf8);
837 dest[0] += (dest[0] >> 5);
839 dest[1] = ((src[0] & 0xe0) >> 2) + ((src[1] & 0x03) << 6);
840 dest[1] += (dest[1] >> 5);
842 dest[2] = ((src[0] << 3) & 0xf8);
843 dest[2] += (dest[2] >> 5);
858 TGAUnit::TGAUnit(FileTGA *file, FrameWriter *writer)
859 : FrameWriterUnit(writer)
867 if(temp) delete temp;
882 TGAConfigVideo::TGAConfigVideo(BC_WindowBase *gui, Asset *asset)
883 : BC_Window(PROGRAM_NAME ": Video Compression",
884 gui->get_abs_cursor_x(1),
885 gui->get_abs_cursor_y(1),
892 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB_RLE)));
893 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA_RLE)));
894 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGB)));
895 compression_items.append(new BC_ListBoxItem(FileTGA::compression_to_str(TGA_RGBA)));
898 TGAConfigVideo::~TGAConfigVideo()
900 compression_items.remove_all_objects();
903 int TGAConfigVideo::create_objects()
907 add_subwindow(new BC_Title(x, y, _("Compression:")));
908 TGACompression *textbox = new TGACompression(this,
913 textbox->create_objects();
914 add_subwindow(new BC_OKButton(this));
918 int TGAConfigVideo::close_event()
925 TGACompression::TGACompression(TGAConfigVideo *gui,
929 ArrayList<BC_ListBoxItem*> *compression_items)
930 : BC_PopupTextBox(gui,
932 FileTGA::compression_to_str(gui->asset->vcodec),
940 int TGACompression::handle_event()
942 strcpy(asset->vcodec, FileTGA::str_to_compression(get_text()));