atomes 1.3.1
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
image.c
Go to the documentation of this file.
1/* This file is part of the 'atomes' software
2
3'atomes' is free software: you can redistribute it and/or modify it under the terms
4of the GNU Affero General Public License as published by the Free Software Foundation,
5either version 3 of the License, or (at your option) any later version.
6
7'atomes' is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9See the GNU General Public License for more details.
10
11You should have received a copy of the GNU Affero General Public License along with 'atomes'.
12If not, see <https://www.gnu.org/licenses/>
13
14Copyright (C) 2022-2026 by CNRS and University of Strasbourg */
15
21
22/*
23* This file: 'image.c'
24*
25* Contains:
26*
27
28 - The functions to render an image from the OpenGL window
29
30*
31* List of functions:
32
33 void render_image (glwin * view, video_options * iopts);
34 void simple_image_render ();
35
36 G_MODULE_EXPORT void run_render_image (GtkNativeDialog * info, gint response_id, gpointer data);
37 G_MODULE_EXPORT void run_render_image (GtkDialog * info, gint response_id, gpointer data);
38
39*/
40
41#include "global.h"
42#include "interface.h"
43#include "project.h"
44#include "glwindow.h"
45#include "glview.h"
46#include "movie.h"
47#include "preferences.h"
48
49#include <libavformat/avformat.h>
50
51#ifndef GTKGLAREA
52extern GdkWindow * xwin;
53extern GLXContext glcontext;
54extern Pixmap pixmap;
55extern GLXPixmap glpixmap;
56#endif
57extern GdkPixbuf * pixbuf;
58
59extern void fill_image (VideoStream * vs, int width, int height, glwin * view);
60extern void init_frame_buffer (int x, int y);
61extern void close_frame_buffer ();
62
63char * image_name[IMAGE_FORMATS] = {"PNG",
64 "JPG/JPEG",
65 "TIFF",
66 "Bitmap"};
67
68char * image_list[IMAGE_FORMATS] = {"png",
69 "jpeg",
70 "tiff",
71 "bmp"};
72
89gchar * render_image_output = NULL;
90
91#ifdef GTK4
101G_MODULE_EXPORT void run_render_image (GtkNativeDialog * info, gint response_id, gpointer data)
102{
103 GtkFileChooser * chooser = GTK_FILE_CHOOSER((GtkFileChooserNative *)info);
104#else
114G_MODULE_EXPORT void run_render_image (GtkDialog * info, gint response_id, gpointer data)
115{
116 GtkFileChooser * chooser = GTK_FILE_CHOOSER((GtkWidget *)info);
117#endif
118 if (response_id == GTK_RESPONSE_ACCEPT)
119 {
120 video_options * iopts = (video_options *)data;
121 gchar * videofile;
123 {
124 videofile = g_strdup_printf ("%s", render_image_output);
125 }
126 else
127 {
128 videofile = file_chooser_get_file_name (chooser);
129#ifdef GTK4
131#else
132 destroy_this_dialog (info);
133#endif
134 }
135 project * this_proj = get_project_by_id (iopts -> proj);
136 glwin * view = this_proj -> modelgl;
137 // On macOS, the CoreAnimation layer may invalidate the current GL context
138 // between the file dialog callback and the OpenGL calls. Without explicitly
139 // making the context current, FBO creation and rendering silently fail or
140 // operate on the wrong context, causing partial or missing image captures.
141 gtk_gl_area_make_current ((GtkGLArea *)view -> plot);
142 init_frame_buffer (iopts -> video_res[0], iopts -> video_res[1]);
143 init_opengl ();
144 int i, x, y, q;
145 for (i=0; i<NGLOBAL_SHADERS; i++)
146 {
147 if (in_md_shaders (this_proj, i)) view -> n_shaders[i][step] = -1;
148 }
150 in_movie_encoding = TRUE;
151 if (iopts -> oglquality != 0)
152 {
153 q = view -> anim -> last -> img -> quality;
154 view -> anim -> last -> img -> quality = iopts -> oglquality;
155 }
156 for (i=0; i<2; i++) tmp_pixels[i] = view -> pixels[i];
157 x = view -> pixels[0];
158 y = view -> pixels[1] - 100;
159 view -> pixels[0] = iopts -> video_res[0];
160 view -> pixels[1] = iopts -> video_res[1];
161 fill_image (NULL, iopts -> video_res[0], iopts -> video_res[1], view);
162 GError * error = NULL;
163 gboolean res = gdk_pixbuf_savev (pixbuf, videofile, image_list[iopts -> codec], NULL, NULL, & error);
164 if (! res && ! atomes_from_libreoffice)
165 {
166 show_warning (_("An error occurred when exporting an image\nyou might want to try again\nsorry for the trouble"), view -> win);
167 }
169 in_movie_encoding = FALSE;
171 {
172 if (iopts -> oglquality != 0) view -> anim -> last -> img -> quality = q;
173 for (i=0; i<NGLOBAL_SHADERS; i++)
174 {
175 if (in_md_shaders (this_proj, i)) view -> n_shaders[i][step] = -1;
176 }
178 reshape (view, x, y, TRUE);
179 update (view);
180 }
181 }
182 else
183 {
184#ifdef GTK4
186#else
187 destroy_this_dialog (info);
188#endif
189 }
190}
191
200void render_image (glwin * view, video_options * iopts)
201{
202 GtkFileFilter * filter;
203 gchar * str;
204#ifdef GTK4
205 GtkFileChooserNative * info;
206#else
207 GtkWidget * info;
208#endif
209 info = create_file_chooser (_("Render Image"),
210 GTK_WINDOW(view -> win),
211 GTK_FILE_CHOOSER_ACTION_SAVE,
212 _("Save"));
213 GtkFileChooser * chooser = GTK_FILE_CHOOSER(info);
214#ifdef GTK3
215 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
216#endif
218 str = g_strdup_printf ("%s.%s", prepare_for_title(get_project_by_id(view -> proj) -> name), image_list[iopts -> codec]);
219 gtk_file_chooser_set_current_name (chooser, str);
220 g_free (str);
221 filter = gtk_file_filter_new ();
222 str = g_strdup_printf (_("%s file (*.%s)"), image_name[iopts -> codec], image_list[iopts -> codec]);
223 gtk_file_filter_set_name (GTK_FILE_FILTER(filter), str);
224 g_free (str);
225 str = g_strdup_printf ("*.%s", image_list[iopts -> codec]);
226 gtk_file_filter_add_pattern (GTK_FILE_FILTER(filter), str);
227 g_free (str);
228 gtk_file_chooser_add_filter (chooser, filter);
229#ifdef GTK4
230 run_this_gtk_native_dialog ((GtkNativeDialog *)info, G_CALLBACK(run_render_image), iopts);
231#else
232 run_this_gtk_dialog (info, G_CALLBACK(run_render_image), iopts);
233#endif
234}
235
242{
243 video_options * vopts = g_malloc0(sizeof*vopts);
244 vopts -> proj = activep;
245 vopts -> oglquality = 0;
247 int h, i, j, k, l, m;
248 for (i=0; i<2; i++) active_glwin -> pixels[i] = vopts -> video_res[i];
249 vopts -> codec = render_image_format;
251 {
253 {
255 }
256 else
257 {
258 active_image -> style = SPACEFILL;
260 active_image -> filled_type = h;
261 j = active_project -> nspec;
262 k = (h) ? 9 + h : 2;
263 l = (h) ? 12 + h : 7;
264 for (i=0; i<j; i++)
265 {
266 m = (int)active_project -> chemistry -> chem_prop[CHEM_Z][i];
267 active_image -> atomicrad[i] = (default_o_at_rs[2]) ? default_at_rs[2] : get_radius (2, h, m, default_atomic_rad[k]);
268 active_image -> atomicrad[i+j] = (default_o_at_rs[7]) ? default_at_rs[7] : get_radius (7, h, m, default_atomic_rad[l]);
269 }
270 }
271 }
273 if (render_image_box != NONE)
274 {
275 if (active_image -> abc)
276 {
278 }
280 {
282 }
283 }
284 if (render_image_axis != NONE)
285 {
286 if (active_image -> xyz)
287 {
289 }
290 }
292 {
293 active_image -> color_map[0] = render_image_acolor;
294 }
296 {
297 // This also trigger to render polyhedra
298 active_image -> color_map[1] = render_image_pcolor;
299 i = (render_image_pcolor == 2) ? 1 : 0;
300 for (j=0; j<active_coord -> totcoord[i]; j++)
301 {
302 active_image -> show_poly[i][j] = TRUE;
303 }
304 }
306 {
307 active_image -> back -> gradient = render_image_back_grad;
308 }
310 {
311 active_image -> back -> gradient = 0;
313 }
315 {
316 if ((active_image -> back -> gradient == 1 && render_image_back_dir < 5)
317 || (active_image -> back -> gradient == 2 && render_image_back_dir < 8))
318 {
319 active_image -> back -> direction = render_image_back_dir;
320 }
321 }
323 {
325 }
326 for (i=0; i<2; i++)
327 {
329 {
330 active_image -> back -> gradient_color[i] = * render_image_grad_color[i];
331 }
332 }
333 run_render_image (NULL, GTK_RESPONSE_ACCEPT, vopts);
334 g_free (vopts);
336}
GtkFileFilter * filter[NCFORMATS+1]
Definition callbacks.c:1557
integer(kind=c_int) function chemistry()
Definition chemistry.F90:22
void to_close_this_project(int to_activate, project *this_proj)
to close this project
Definition close_p.c:479
dint rep
Definition dlp_edit.c:2242
int * duplicate_int(int num, int *old_val)
copy a list of int
Definition global.c:547
int activep
Definition global.c:162
gboolean in_movie_encoding
Definition global.c:184
gboolean atomes_render_image
Definition global.c:196
int tmp_pixels[2]
Definition global.c:177
gboolean atomes_from_libreoffice
Definition global.c:195
Global variable declarations Global convenience function declarations Global data structure defin...
void run_this_gtk_dialog(GtkWidget *dial, GCallback handler, gpointer data)
run a GTK (3 and 4) basic GtkDialog
Definition gtk-misc.c:533
void recreate_all_shaders(glwin *view)
re-initialize all OpenGL shaders
void file_chooser_set_current_folder(GtkFileChooser *chooser)
set current folder in a GtkFilechooser
Definition gtk-misc.c:2358
glwin * active_glwin
Definition project.c:53
coord_info * active_coord
Definition project.c:49
project * proj
gchar * file_chooser_get_file_name(GtkFileChooser *chooser)
get a file name from a GtkFileChooser (single file selected)
Definition gtk-misc.c:2306
void destroy_this_dialog(GtkDialog *dialog)
destroy a GtkDialog
Definition gtk-misc.c:2235
gchar * prepare_for_title(gchar *init)
prepare a string for a window title, getting rid of all markup
Definition tools.c:71
gboolean in_md_shaders(project *this_proj, int id)
is this shader MD dependent ?
#define CHEM_Z
Definition global.h:316
GtkWidget * create_file_chooser(const gchar *title, GtkWindow *parent, GtkFileChooserAction act, const gchar *act_name)
create a GtkFileChooser, utility to select file(s)
Definition gtk-misc.c:2393
project * active_project
Definition project.c:47
void destroy_this_native_dialog(GtkNativeDialog *dialog)
destroy a GtkNativeDialog
Definition gtk-misc.c:2254
image * active_image
Definition project.c:52
project * get_project_by_id(int p)
get project pointer using id number
Definition project.c:120
void update(glwin *view)
update the rendering of the OpenGL window
Definition glview.c:461
void reshape(glwin *view, int width, int height, gboolean use_ratio)
reshape (resize) the OpenGL window
Definition glview.c:538
void init_opengl()
initialize OpenGL rendering parameters
Definition glview.c:1440
Variable declarations related to the OpenGL window Function declarations related to the OpenGL wind...
image * plot
Definition ogl_draw.c:72
int step
Definition ogl_draw.c:76
@ SPACEFILL
Definition glview.h:184
@ NONE
Definition glview.h:181
#define OGL_STYLES
Definition glwin.h:107
#define NGLOBAL_SHADERS
Definition glwin.h:69
Function declarations for the creation of the OpenGL window.
ColRGBA * render_image_grad_color[2]
Definition image.c:86
ColRGBA * render_image_box_color
Definition image.c:87
GLXContext glcontext
GdkWindow * xwin
void close_frame_buffer()
close the frame buffer
Definition movie.c:1210
ColRGBA * render_image_back_color
Definition image.c:82
void init_frame_buffer(int x, int y)
init a frame buffer
Definition movie.c:1182
Pixmap pixmap
GdkPixbuf * pixbuf
Definition movie.c:122
int render_image_rep
Definition image.c:77
void simple_image_render()
simple direct rendering from command line
Definition image.c:241
G_MODULE_EXPORT void run_render_image(GtkDialog *info, gint response_id, gpointer data)
render an image from the OpenGL window - running the dialog
Definition image.c:114
int * render_image_pixels
Definition image.c:88
void render_image(glwin *view, video_options *iopts)
render an image from the OpenGL window - prepare the dialog
Definition image.c:200
int render_image_pcolor
Definition image.c:79
int render_image_back_grad
Definition image.c:83
gboolean trigger_fragment_analysis
Definition image.c:80
int render_image_acolor
Definition image.c:78
float render_image_back_pos
Definition image.c:85
GLXPixmap glpixmap
int render_image_axis
Definition image.c:75
int render_image_back_dir
Definition image.c:84
int render_image_format
Definition image.c:73
int render_image_box
Definition image.c:76
void fill_image(VideoStream *vs, int width, int height, glwin *view)
render an image from an OpenGL rendering
Definition movie.c:278
gchar * render_image_output
Definition image.c:89
char * image_name[IMAGE_FORMATS]
Definition image.c:63
char * image_list[IMAGE_FORMATS]
Definition image.c:68
gboolean trigger_molecule_analysis
Definition image.c:81
int render_image_style
Definition image.c:74
void show_warning(char *warning, GtkWidget *win)
show warning
Definition interface.c:266
Messaging function declarations.
position
Definition m_proj.c:48
Data structure declarations for movie encoding Function declarations for movie encoding.
#define IMAGE_FORMATS
Definition movie.h:37
double y
Definition ogl_draw.c:63
double x
Definition ogl_draw.c:63
double * default_at_rs
float get_radius(int object, int col, int z, element_radius *rad_list)
retrieve the radius/width of a species depending on style
gboolean * default_o_at_rs
element_radius * default_atomic_rad[16]
Preference variable declarations.
Function declarations for reading atomes project file Function declarations for saving atomes proje...
Definition glwin.h:332
Definition glwin.h:350
Definition glwin.h:967
int oglquality
Definition w_encode.c:59
GtkWidget * res[2]
Definition w_encode.c:342
int codec
Definition w_encode.c:58
int video_res[2]
Definition w_encode.c:55
GtkWidget * img
Definition workspace.c:70