Skip to content

Commit d8453f6

Browse files
committed
Merge branch 'main' of github.com:rodjjo/diffusion-expert
2 parents 9121dce + 8a19931 commit d8453f6

14 files changed

Lines changed: 140 additions & 125 deletions

python_stuff/images/diffusion_routines.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@
1313
from dexpert import progress, progress_canceled, progress_title
1414

1515

16+
REPORT_PREFIX = 'Text To Image'
17+
REPORT_PREFIXES = {
18+
'txt2img': 'Text to Image',
19+
'inpaint2img': 'Inpaint',
20+
'img2img': 'Image to Image',
21+
}
22+
23+
def set_prefix(text):
24+
global REPORT_PREFIX
25+
REPORT_PREFIX = text
26+
1627
def report(message):
17-
progress_title(f'[Text To Image] - {message}')
28+
progress_title(f'[{REPORT_PREFIX}] - {message}')
1829

1930

2031
def get_lora_path(lora: str) -> str:
@@ -90,11 +101,14 @@ def _run_pipeline(pipeline_type, params):
90101
latents_noise = create_latents_noise(shape, seed, subseed, var_stren)
91102

92103
generator = None if seed == -1 else torch.Generator(device=device).manual_seed(seed)
93-
report("started")
94104

95105
if pipeline_type == 'img2img' and input_mask is not None:
96106
pipeline_type = 'inpaint2img'
97107

108+
set_prefix(REPORT_PREFIXES.get(pipeline_type, "Text to Image"))
109+
110+
report("started")
111+
98112
report("creating the pipeline")
99113
pipeline = create_pipeline(pipeline_type, model, controlnets=controlnets, lora_list=lora_list, reload_model=reload_model)
100114
report("pipeline created")

src/controls/image_panel.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,6 @@ namespace dexpert
188188
}
189189

190190
bool ImagePanel::isSelecting() {
191-
if (images_[image_type_paste]) {
192-
selection_start_ = paste_coords_;
193-
selection_end_ = paste_coords_;
194-
selection_end_.x += images_[image_type_paste]->w();
195-
selection_end_.y += images_[image_type_paste]->h();
196-
return false;
197-
}
198191
return tool_ == image_tool_select && mouse_down_left_;
199192
}
200193

@@ -497,11 +490,19 @@ namespace dexpert
497490
scrollAgain();
498491
}
499492

493+
void ImagePanel::setMaskPanel(ImagePanel *mask_panel) {
494+
mask_panel_ = mask_panel;
495+
}
496+
500497
RawImage* ImagePanel::get_cached_image(int layer) {
501498
/*
502499
Dim the image to fit the window. Don't draw a huge image in a smaller area...
503500
*/
504501
RawImage *original = images_[layer].get();
502+
if (mask_panel_ != NULL && layer == image_type_mask && mask_panel_->image_visible_[layer]) {
503+
original = mask_panel_->images_[layer].get();
504+
}
505+
505506
if (layer == image_type_paste) {
506507
return NULL;
507508
} else if (layer == image_type_image && original == NULL) {
@@ -523,6 +524,7 @@ namespace dexpert
523524
}
524525

525526
image_ptr_t &cache = caches_[layer];
527+
526528
if (!cache.get() ||
527529
!valid_caches_[layer] ||
528530
cache_versions_[layer] != original->getVersion() ||
@@ -546,6 +548,8 @@ namespace dexpert
546548
s1.x -= xmove * zoom_;
547549
s1.y -= ymove * zoom_;
548550
cache->pasteAt(s1.x , s1.y, img->w() * zoom_, img->h() * zoom_, img);
551+
} else if (layer == image_type_mask && caches_[image_type_image].get() != NULL) {
552+
cache->pasteInvertMask(caches_[image_type_image].get());
549553
}
550554
}
551555

@@ -577,6 +581,7 @@ namespace dexpert
577581
draw_buffer(img);
578582
}
579583
}
584+
580585
blur_gl_contents(this->w(), this->h(), current_x_, current_y_);
581586
draw_tool();
582587
}
@@ -607,6 +612,26 @@ namespace dexpert
607612
void ImagePanel::draw_tool() {
608613
float sx = 2.0 / this->w();
609614
float sy = 2.0 / this->h();
615+
616+
auto r = getReferenceImage();
617+
if (r) {
618+
coordinate_t s1, s2;
619+
s1.x = -1;
620+
s1.y = -1;
621+
s2.x = r->w()+2;
622+
s2.y = r->h()+2;
623+
convertToScreenCoords(&s1.x, &s1.y);
624+
convertToScreenCoords(&s2.x, &s2.y);
625+
glBegin(GL_LINE_LOOP);
626+
glColor4f(0.5, 0.5, 0.5, 0.3);
627+
glVertex2f(s1.x * sx - 1.0, 1.0 - s1.y * sy);
628+
glVertex2f(s2.x * sx - 1.0, 1.0 - s1.y * sy);
629+
glVertex2f(s2.x * sx - 1.0, 1.0 - s2.y * sy);
630+
glVertex2f(s1.x * sx - 1.0, 1.0 - s2.y * sy);
631+
glVertex2f(s1.x * sx - 1.0, 1.0 - s1.y * sy);
632+
glEnd();
633+
}
634+
610635
if (tool_ == image_tool_brush) {
611636
glBegin(GL_LINE_LOOP);
612637
float theta;
@@ -767,7 +792,6 @@ namespace dexpert
767792
scrollAgain();
768793
}
769794

770-
771795
void ImagePanel::setBrushSize(uint8_t size) {
772796
if (size > 128)
773797
brush_size_ = 128;
@@ -1036,7 +1060,7 @@ namespace dexpert
10361060
}
10371061

10381062
void ImagePanel::selectAll() {
1039-
if (images_[image_type_paste]) {
1063+
if (images_[image_type_paste]) {
10401064
return;
10411065
}
10421066
auto r = getReferenceImage();

src/controls/image_panel.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
typedef enum {
1212
image_type_image, // the final image
1313
image_type_paste, // a image that floats before being pasted in the image
14-
image_type_controlnet, // pre-processed
1514
image_type_mask,
15+
image_type_controlnet, // pre-processed
1616
// keep layer count at the end
1717
image_type_count
1818
} image_type_t;
@@ -42,8 +42,9 @@ typedef enum {
4242
edit_type_none, // edit disabled
4343
edit_type_image, // the user can change the main image
4444
edit_type_paste,
45-
edit_type_controlnet, // the user can change the controlnet image
4645
edit_type_mask, // the user can change the mask
46+
edit_type_controlnet, // the user can change the controlnet image
47+
4748
// keep edit_type_count at the end.
4849
edit_type_count
4950
} edit_type_t;
@@ -125,6 +126,7 @@ namespace dexpert
125126
bool clicked();
126127
void scheduleRedraw();
127128
void zoomFit();
129+
void setMaskPanel(ImagePanel *mask_panel);
128130

129131
protected:
130132
int handle(int event) override;
@@ -159,10 +161,10 @@ namespace dexpert
159161

160162
void convertToImageCoords(int *x, int *y);
161163
void convertToScreenCoords(int *x, int *y);
162-
163164
void applyBrush(int mousex, int mousey, bool clear);
164165

165166
private:
167+
ImagePanel *mask_panel_ = NULL;
166168
callback_t on_change_;
167169
image_tool_t tool_ = image_tool_none;
168170
bool mouse_changed_ = false;

src/dialogs/common_dialogs.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,31 @@ std::string executeChooser(Fl_File_Chooser *fc) {
131131
}
132132

133133
std::string choose_image_to_open_fl(std::string* current_dir) {
134-
Fl_File_Chooser dialog("", kIMAGE_FILES_FILTER_FL, Fl_File_Chooser::SINGLE, "Open image");
135-
return executeChooser(&dialog);
134+
if (!path_exists(current_dir->c_str())) {
135+
*current_dir = "";
136+
}
137+
Fl_File_Chooser dialog(current_dir->c_str(), kIMAGE_FILES_FILTER_FL, Fl_File_Chooser::SINGLE, "Open image");
138+
std::string result = executeChooser(&dialog);
139+
if (!result.empty()) {
140+
size_t latest = result.find_last_of("/\\");
141+
*current_dir = result.substr(0, latest);
142+
}
143+
return result;
136144
}
137145

138146
std::string choose_image_to_save_fl(std::string* current_dir) {
139-
Fl_File_Chooser dialog("", kIMAGE_FILES_FILTER_FL, Fl_File_Chooser::SINGLE | Fl_File_Chooser::CREATE, "Save image");
147+
if (!path_exists(current_dir->c_str())) {
148+
*current_dir = "";
149+
}
150+
Fl_File_Chooser dialog(current_dir->c_str(), kIMAGE_FILES_FILTER_FL, Fl_File_Chooser::SINGLE | Fl_File_Chooser::CREATE, "Save image");
140151
std::string result = executeChooser(&dialog);
141152

142153
if (!result.empty() && path_exists(result.c_str())) {
143154
if (!ask("Do you want to replace the destination file ?")) {
144155
result.clear();
156+
} else {
157+
size_t latest = result.find_last_of("/\\");
158+
*current_dir = result.substr(0, latest);
145159
}
146160
}
147161

src/dialogs/utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace dexpert
1111

1212
image_ptr_t open_image_from_dialog() {
1313
image_ptr_t result;
14-
std::string path = choose_image_to_open(&getConfig().lastImageSaveDir());
14+
std::string path = choose_image_to_open(&getConfig().lastImageOpenDir());
1515
if (!path.empty()) {
1616
result = get_sd_state()->openImage(path.c_str());
1717
if (!result) {

src/panels/embedding_panel.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,25 @@ EmbeddingPanel::EmbeddingPanel(embedding_type_t embedding_type, int x, int y, in
2323

2424
btnLeft_.reset(new Button(xpm::image(xpm::arrow_left_16x16), [this]{
2525
if (index_ > 0) {
26-
--index_;
26+
if (Fl::event_shift() != 0) {
27+
if ((int)index_ - (int)images_.size() >= 0) {
28+
index_ -= images_.size();
29+
} else {
30+
index_ = 0;
31+
}
32+
} else if (index_> 0){
33+
--index_;
34+
}
2735
}
2836
updateData();
2937
}));
3038

3139
btnRight_.reset(new Button(xpm::image(xpm::arrow_right_16x16), [this]{
32-
++index_;
40+
if (Fl::event_shift() != 0) {
41+
index_ += images_.size();
42+
} else {
43+
++index_;
44+
}
3345
updateData();
3446
}));
3547

src/panels/painting_panel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ PaintingPanel::PaintingPanel(int x, int y, int w, int h, PromptPanel *prompt, P
222222

223223
if (!inputPanel_) {
224224
btnInput_->hide();
225+
} else {
226+
this->image_panel_->setMaskPanel(inputPanel_->image_panel_);
225227
}
226228

227229
blur_mask_->hide();
@@ -508,6 +510,7 @@ void PaintingPanel::modeSelected() {
508510
case painting_canny:
509511
case painting_scribble:
510512
case painting_deepth: {
513+
image_panel_->setLayerVisible(image_type_mask, only_control_net_);
511514
image_panel_->setEditType(edit_type_controlnet);
512515
image_panel_->setControlnetImageType(controltype_from_mode(getSelectedMode()));
513516
image_panel_->setLayerVisible(image_type_controlnet, true);

src/panels/preview_panel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ void PreviewPanel::setRow(size_t value) {
126126
}
127127

128128
size_t PreviewPanel::getRow() {
129+
if (row_ >= get_sd_state()->getGeneratorSize() && get_sd_state()->getGeneratorSize() > 0) {
130+
row_ = get_sd_state()->getGeneratorSize() - 1;
131+
}
129132
return row_;
130133
}
131134

src/panels/preview_panel.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ namespace dexpert
3737
Fl_Box *lblCounter_;
3838

3939
std::unique_ptr<Button> btnUse_;
40-
std::unique_ptr<Button> btnNext_;
41-
std::unique_ptr<Button> btnNextVar_;
4240
std::unique_ptr<Button> btnRemove_;
4341
std::unique_ptr<Button> btnView_;
4442
std::unique_ptr<Button> btnScrollLeft_;

src/panels/prompt_panel.cpp

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -280,48 +280,29 @@ void PromptPanel::refreshModels() {
280280
return;
281281
}
282282

283-
std::string modelName = "";
284-
std::string modelInpaint = "";
285-
if (models_->value() >= 0) {
286-
modelName = models_->text(models_->value());
287-
} else {
288-
modelName = getConfig().getLatestSdModel();
289-
}
290-
if (modelsInpaint_->value() >= 0) {
291-
modelInpaint = modelsInpaint_->text(modelsInpaint_->value());
292-
} else {
293-
modelInpaint = getConfig().getLatestSdModelInpaint();
294-
}
283+
std::string modelName = getConfig().getLatestSdModel();
284+
std::string modelInpaint = getConfig().getLatestSdModelInpaint();
295285

296286
models_->clear();
297287
modelsInpaint_->clear();
298288

299289
auto mdls = get_sd_state()->getSdModels();
300290

301-
int index = 0;
302-
int inpaintIndex = 0;
303-
int value = -1;
304-
int valueInpaint = -1;
305291
std::string name_lower;
306292

307293
for (auto it = mdls.cbegin(); it != mdls.cend(); it++) {
308294
name_lower = it->name;
309295
std::transform(name_lower.begin(), name_lower.end(), name_lower.begin(), ::tolower);
310296
if (name_lower.find("inpaint") != std::string::npos) {
311297
modelsInpaint_->add(it->name.c_str());
312-
if (valueInpaint == -1 && it->name == modelInpaint) {
313-
valueInpaint = inpaintIndex;
314-
}
315-
++inpaintIndex;
316298
} else {
317299
models_->add(it->name.c_str());
318-
if (value == -1 && it->name == modelName) {
319-
value = index;
320-
}
321-
++index;
322300
}
323301
}
324-
302+
303+
int value = models_->find_index(modelName.c_str());
304+
int valueInpaint = modelsInpaint_->find_index(modelInpaint.c_str());
305+
325306
if (value < 0)
326307
value = 0;
327308

@@ -350,11 +331,9 @@ const char *PromptPanel::getSdModel(bool for_inpainting) {
350331
if (hasInpaint) {
351332
return modelsInpaint_->text(modelsInpaint_->value());
352333
}
353-
} else {
354-
if (hasNormal) {
355-
return models_->text(models_->value());
356-
}
357-
}
334+
} else if (hasNormal) {
335+
return models_->text(models_->value());
336+
}
358337

359338
return NULL;
360339
}

0 commit comments

Comments
 (0)