Skip to content

Commit b2ecfac

Browse files
Thiago Milczarek Sayaokevinrushforth
Thiago Milczarek Sayao
authored andcommitted
8271398: GTK3 drag view image swaps red and blue color channels
Reviewed-by: pbansal, kcr
1 parent 7329279 commit b2ecfac

File tree

2 files changed

+94
-19
lines changed

2 files changed

+94
-19
lines changed

modules/javafx.graphics/src/main/native-glass/gtk/glass_dnd.cpp

+8-19
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,13 @@ GdkPixbuf* DragView::get_drag_image(GtkWidget *widget, gboolean* is_raw_image, g
911911
guchar* data = (guchar*) g_try_malloc0(nraw - whsz);
912912
if (data) {
913913
memcpy(data, (raw + whsz), nraw - whsz);
914+
915+
if (is_raw_image) {
916+
guchar* origdata = data;
917+
data = (guchar*) convert_BGRA_to_RGBA((const int*) data, w * 4, h);
918+
g_free(origdata);
919+
}
920+
914921
pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE, 8,
915922
w, h, w * 4, pixbufDestroyNotifyFunc, NULL);
916923
}
@@ -1047,26 +1054,8 @@ void DragView::View::expose()
10471054
{
10481055
cairo_t *context = gdk_cairo_create(gtk_widget_get_window(widget));
10491056

1050-
cairo_surface_t* cairo_surface;
1051-
1052-
guchar* pixels = is_raw_image
1053-
? (guchar*) convert_BGRA_to_RGBA((const int*) gdk_pixbuf_get_pixels(pixbuf),
1054-
gdk_pixbuf_get_rowstride(pixbuf),
1055-
height)
1056-
: gdk_pixbuf_get_pixels(pixbuf);
1057-
1058-
cairo_surface = cairo_image_surface_create_for_data(
1059-
pixels,
1060-
CAIRO_FORMAT_ARGB32,
1061-
width, height, width * 4);
1062-
1063-
cairo_set_source_surface(context, cairo_surface, 0, 0);
1064-
cairo_set_operator(context, CAIRO_OPERATOR_SOURCE);
1057+
gdk_cairo_set_source_pixbuf(context, pixbuf, 0, 0);
10651058
cairo_paint(context);
10661059

1067-
if (is_raw_image) {
1068-
g_free(pixels);
1069-
}
10701060
cairo_destroy(context);
1071-
cairo_surface_destroy(cairo_surface);
10721061
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
import javafx.application.Application;
27+
import javafx.embed.swing.SwingFXUtils;
28+
import javafx.geometry.Pos;
29+
import javafx.scene.Scene;
30+
import javafx.scene.control.Label;
31+
import javafx.scene.image.Image;
32+
import javafx.scene.image.ImageView;
33+
import javafx.scene.input.ClipboardContent;
34+
import javafx.scene.input.Dragboard;
35+
import javafx.scene.input.TransferMode;
36+
import javafx.scene.layout.VBox;
37+
import javafx.stage.Stage;
38+
39+
import java.awt.image.BufferedImage;
40+
41+
public class DndTestDragViewRawImage extends Application {
42+
Image image = createImage(240, 240);
43+
44+
public static void main(String[] args) {
45+
Application.launch(args);
46+
}
47+
48+
@Override
49+
public void start(Stage stage) {
50+
ImageView imageView = new ImageView(image);
51+
imageView.setOnDragDetected(event -> {
52+
ClipboardContent content = new ClipboardContent();
53+
content.putImage(image);
54+
Dragboard dragboard = imageView.startDragAndDrop(TransferMode.ANY);
55+
dragboard.setContent(content);
56+
dragboard.setDragView(image);
57+
});
58+
59+
Label label = new Label("Click the image and drag. " +
60+
"The drag image displayed with the cursor (drag view) " +
61+
"should match the source image");
62+
63+
VBox vBox = new VBox(label, imageView);
64+
vBox.setSpacing(5.0);
65+
vBox.setAlignment(Pos.CENTER);
66+
stage.setScene(new Scene(vBox, 480, 480));
67+
stage.setTitle("Drag View Image Colors");
68+
stage.show();
69+
}
70+
71+
private static Image createImage(int width, int height) {
72+
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
73+
for (int y = 0; y < height; y++) {
74+
for (int x = 0; x < width; x++) {
75+
if (x < width * 0.33) {
76+
image.setRGB(x, y, 0xFF0000);
77+
} else if (x < width * 0.66) {
78+
image.setRGB(x, y, 0x00FF00);
79+
} else {
80+
image.setRGB(x, y, 0x0000FF);
81+
}
82+
}
83+
}
84+
return SwingFXUtils.toFXImage(image, null);
85+
}
86+
}

0 commit comments

Comments
 (0)