-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmapViewer.cpp
197 lines (165 loc) · 6.37 KB
/
BitmapViewer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "BitmapViewer.h"
#include "texture_names.h"
#include "StringUtils.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
BitmapViewer::BitmapViewer(){
builder = Gtk::Builder::create_from_file("res/bitmapViewer.glade");
Gtk::Box* root;
builder->get_widget("bitmapViewerTLBox", root);
add(*root);
root->set_hexpand(true);
root->set_vexpand(true);
root->show();
handle = nullptr;
builder->get_widget("statusFormat", dataTypeLabel);
builder->get_widget("frameIndex", frameNumberButton);
builder->get_widget("statusCurrentMipmapSize", currentSizeLabel);
builder->get_widget("statusMaxSize", maxSizeLabel);
builder->get_widget("mipmapComboBox", mipmapComboBox);
builder->get_widget_derived("bitmapDrawingArea", viewerDrawingArea);
builder->get_widget("ddsExportSingleMip", exportSingleDDS);
builder->get_widget("ddsExportAllMips", exportAllDDS);
builder->get_widget("pngExport", exportSinglePNG);
builder->get_widget("exportFormatCombo", formatComboBox);
frameNumberButton->signal_changed().connect([this]{
updateStuff();
});
mipmapComboBox->signal_changed().connect([this]{
updateMipmap();
});
exportSingleDDS->signal_clicked().connect([this]{
std::string path = runExportDialog(getFilename(item->name) + std::string(".dds"));
if(path == std::string("")) return;
int idx = frameNumberButton->get_adjustment()->get_value();
int lvl = mipmapComboBox->get_active_row_number();
detexTexture tex = handle->frames[idx].getDetexTexture(lvl);
if(tex.data == nullptr) return;
detexSaveDDSFile(&tex, path.c_str());
free(tex.data);
});
exportAllDDS->signal_clicked().connect([this]{
std::string path = runExportDialog(getFilename(item->name) + std::string(".dds"));
if(path == std::string("")) return;
int idx = frameNumberButton->get_adjustment()->get_value();
detexTexture* texs = (detexTexture*) malloc(handle->frames[idx].mipmapCount * sizeof(detexTexture));
detexTexture** texpointers = (detexTexture**) calloc(handle->frames[idx].mipmapCount,sizeof(detexTexture*));
for(int i = 0; i < handle->frames[idx].mipmapCount; i++){
texs[i] = handle->frames[idx].getDetexTexture(i);
if(texs[i].data == nullptr){
goto cleanup;
}
texpointers[i] = &texs[i];
}
detexSaveDDSFileWithMipmaps(texpointers, handle->frames[idx].mipmapCount, path.c_str());
cleanup:
for(int i = 0; i < handle->frames[idx].mipmapCount; i++){
if(texs[i].data != nullptr){
free(texs[i].data);
}
}
free(texpointers);
free(texs);
});
exportSinglePNG->signal_clicked().connect([this]{
int idx = frameNumberButton->get_adjustment()->get_value();
int lvl = mipmapComboBox->get_active_row_number();
void* data = handle->frames[idx].getR8G8B8A8Data(lvl);
if(data == nullptr){
return;
}
if(formatComboBox->get_active_id() == "png"){
// PNG export
std::string path = runExportDialog(getFilename(item->name) + std::string(".png"));
if(path == std::string("")) return;
if(!stbi_write_png(path.c_str(), handle->frames[idx].mipMaps[lvl].width, handle->frames[idx].mipMaps[lvl].height,
4, data, handle->frames[idx].mipMaps[lvl].width * 4)){
// write failed
printf("png export failed\n");
}
}
if(formatComboBox->get_active_id() == "tga"){
// PNG export
std::string path = runExportDialog(getFilename(item->name) + std::string(".tga"));
if(path == std::string("")) return;
if(!stbi_write_tga(path.c_str(), handle->frames[idx].mipMaps[lvl].width, handle->frames[idx].mipMaps[lvl].height,
4, data)){
// write failed
printf("tga export failed\n");
}
}
});
item = nullptr;
}
BitmapViewer::~BitmapViewer(){
if(item != nullptr){
delete item;
}
}
std::string BitmapViewer::runExportDialog(std::string fileName){
Gtk::FileChooserDialog* fileChooser = new Gtk::FileChooserDialog("Export",Gtk::FILE_CHOOSER_ACTION_SAVE);
fileChooser->add_button("_Cancel", Gtk::RESPONSE_CANCEL);
fileChooser->add_button("_Save", Gtk::RESPONSE_OK);
fileChooser->set_current_name(fileName);
int response = fileChooser->run();
fileChooser->close();
if(response == Gtk::RESPONSE_OK){
return fileChooser->get_filename();
} else {
return std::string("");
}
}
void BitmapViewer::updateStuff(){
// set all values for this frame
int idx = frameNumberButton->get_adjustment()->get_value();
if(idx >= handle->frameCount){
return;
}
maxSizeLabel->set_text(std::to_string(handle->frames[idx].width) + std::string("x") + std::to_string(handle->frames[idx].height));
mipmapComboBox->remove_all();
for(int m = 0; m < handle->frames[idx].mipmapCount; m++){
std::string levelLabel = "Level ";
levelLabel += std::to_string(m);
levelLabel += " (";
levelLabel += std::to_string(handle->frames[idx].mipMaps[m].width);
levelLabel += "x";
levelLabel += std::to_string(handle->frames[idx].mipMaps[m].height);
levelLabel += ")";
mipmapComboBox->append(std::to_string(m),levelLabel);
mipmapComboBox->set_active(0);
}
dataTypeLabel->set_text(std::string(dxgi_types[handle->frames[idx].format]));
updateMipmap();
}
void BitmapViewer::updateMipmap(){
int frameIdx = frameNumberButton->get_adjustment()->get_value();
int lvl = mipmapComboBox->get_active_row_number();
int width = handle->frames[frameIdx].mipMaps[lvl].width;
int height = handle->frames[frameIdx].mipMaps[lvl].height;
currentSizeLabel->set_text(std::to_string(width) + std::string("x") + std::to_string(height));
// also, actually display the texture. That might be important
void* buf = handle->frames[frameIdx].getR8G8B8A8Data(lvl);
if(buf == nullptr){
// no Texture to show, whoops
viewerDrawingArea->pixbufValid = false;
} else {
viewerDrawingArea->pixbufValid = true;
viewerDrawingArea->width = width;
viewerDrawingArea->height = height;
viewerDrawingArea->pixbuf = Gdk::Pixbuf::create_from_data((uint8_t*)buf, Gdk::Colorspace::COLORSPACE_RGB, true, 8, width, height, width*4);
//free(buf);
}
}
void BitmapViewer::setItem(Item* item){
this->item = item;
handle = new BitmapHandle(item->moduleItem,item->logger);
// set the stuff that's only needed when an item is loaded
Gtk::Label* lab;
builder->get_widget("statusFrameCount", lab);
lab->set_text(std::string("Frames: ") + std::to_string(handle->frameCount));
frameNumberButton->get_adjustment()->set_upper(handle->frameCount - 1);
frameNumberButton->get_adjustment()->set_value(0);
frameNumberButton->get_adjustment()->set_lower(0);
frameNumberButton->get_adjustment()->set_step_increment(1);
updateStuff();
}