static GimpTempBuf * gimp_buffer_get_new_preview (GimpViewable *viewable, GimpContext *context, gint width, gint height) { GimpBuffer *buffer = GIMP_BUFFER (viewable); const Babl *format = gimp_buffer_get_format (buffer); GimpTempBuf *preview; if (babl_format_is_palette (format)) format = gimp_babl_format (GIMP_RGB, GIMP_PRECISION_U8_GAMMA, babl_format_has_alpha (format)); else format = gimp_babl_format (gimp_babl_format_get_base_type (format), gimp_babl_precision (GIMP_COMPONENT_TYPE_U8, gimp_babl_format_get_linear (format)), babl_format_has_alpha (format)); preview = gimp_temp_buf_new (width, height, format); gegl_buffer_get (buffer->buffer, GEGL_RECTANGLE (0, 0, width, height), MIN ((gdouble) width / (gdouble) gimp_buffer_get_width (buffer), (gdouble) height / (gdouble) gimp_buffer_get_height (buffer)), format, gimp_temp_buf_get_data (preview), GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE); return preview; }
GimpTempBuf * gimp_image_get_new_preview (GimpViewable *viewable, GimpContext *context, gint width, gint height) { GimpImage *image = GIMP_IMAGE (viewable); const Babl *format; gboolean linear; GimpTempBuf *buf; gdouble scale_x; gdouble scale_y; scale_x = (gdouble) width / (gdouble) gimp_image_get_width (image); scale_y = (gdouble) height / (gdouble) gimp_image_get_height (image); format = gimp_projectable_get_format (GIMP_PROJECTABLE (image)); linear = gimp_babl_format_get_linear (format); format = gimp_babl_format (gimp_babl_format_get_base_type (format), gimp_babl_precision (GIMP_COMPONENT_TYPE_U8, linear), babl_format_has_alpha (format)); buf = gimp_temp_buf_new (width, height, format); gegl_buffer_get (gimp_pickable_get_buffer (GIMP_PICKABLE (image)), GEGL_RECTANGLE (0, 0, width, height), MIN (scale_x, scale_y), gimp_temp_buf_get_format (buf), gimp_temp_buf_get_data (buf), GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP); return buf; }
GimpColorProfile * gimp_image_get_builtin_color_profile (GimpImage *image) { static GimpColorProfile *srgb_profile = NULL; static GimpColorProfile *linear_rgb_profile = NULL; const Babl *format; g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL); if (! srgb_profile) { srgb_profile = gimp_color_profile_new_srgb (); linear_rgb_profile = gimp_color_profile_new_linear_rgb (); } format = gimp_image_get_layer_format (image, FALSE); if (gimp_babl_format_get_linear (format)) { if (! srgb_profile) { srgb_profile = gimp_color_profile_new_srgb (); g_object_add_weak_pointer (G_OBJECT (srgb_profile), (gpointer) &srgb_profile); } return linear_rgb_profile; } else { if (! linear_rgb_profile) { linear_rgb_profile = gimp_color_profile_new_linear_rgb (); g_object_add_weak_pointer (G_OBJECT (linear_rgb_profile), (gpointer) &linear_rgb_profile); } return srgb_profile; } }
void gimp_image_convert_precision (GimpImage *image, GimpPrecision precision, gint layer_dither_type, gint text_layer_dither_type, gint mask_dither_type, GimpProgress *progress) { GimpColorProfile *old_profile; GimpColorProfile *new_profile = NULL; const Babl *old_format; const Babl *new_format; GList *all_drawables; GList *list; const gchar *undo_desc = NULL; GimpProgress *sub_progress = NULL; gint nth_drawable, n_drawables; g_return_if_fail (GIMP_IS_IMAGE (image)); g_return_if_fail (precision != gimp_image_get_precision (image)); g_return_if_fail (precision == GIMP_PRECISION_U8_GAMMA || gimp_image_get_base_type (image) != GIMP_INDEXED); g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress)); all_drawables = g_list_concat (gimp_image_get_layer_list (image), gimp_image_get_channel_list (image)); n_drawables = g_list_length (all_drawables) + 1 /* + selection */; if (progress) sub_progress = gimp_sub_progress_new (progress); switch (precision) { case GIMP_PRECISION_U8_LINEAR: undo_desc = C_("undo-type", "Convert Image to 8 bit linear integer"); break; case GIMP_PRECISION_U8_GAMMA: undo_desc = C_("undo-type", "Convert Image to 8 bit gamma integer"); break; case GIMP_PRECISION_U16_LINEAR: undo_desc = C_("undo-type", "Convert Image to 16 bit linear integer"); break; case GIMP_PRECISION_U16_GAMMA: undo_desc = C_("undo-type", "Convert Image to 16 bit gamma integer"); break; case GIMP_PRECISION_U32_LINEAR: undo_desc = C_("undo-type", "Convert Image to 32 bit linear integer"); break; case GIMP_PRECISION_U32_GAMMA: undo_desc = C_("undo-type", "Convert Image to 32 bit gamma integer"); break; case GIMP_PRECISION_HALF_LINEAR: undo_desc = C_("undo-type", "Convert Image to 16 bit linear floating point"); break; case GIMP_PRECISION_HALF_GAMMA: undo_desc = C_("undo-type", "Convert Image to 16 bit gamma floating point"); break; case GIMP_PRECISION_FLOAT_LINEAR: undo_desc = C_("undo-type", "Convert Image to 32 bit linear floating point"); break; case GIMP_PRECISION_FLOAT_GAMMA: undo_desc = C_("undo-type", "Convert Image to 32 bit gamma floating point"); break; case GIMP_PRECISION_DOUBLE_LINEAR: undo_desc = C_("undo-type", "Convert Image to 64 bit linear floating point"); break; case GIMP_PRECISION_DOUBLE_GAMMA: undo_desc = C_("undo-type", "Convert Image to 64 bit gamma floating point"); break; } if (progress) gimp_progress_start (progress, FALSE, "%s", undo_desc); g_object_freeze_notify (G_OBJECT (image)); gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_CONVERT, undo_desc); /* Push the image precision to the stack */ gimp_image_undo_push_image_precision (image, NULL); old_profile = gimp_image_get_color_profile (image); old_format = gimp_image_get_layer_format (image, FALSE); /* Set the new precision */ g_object_set (image, "precision", precision, NULL); new_format = gimp_image_get_layer_format (image, FALSE); if (old_profile && gimp_babl_format_get_linear (old_format) != gimp_babl_format_get_linear (new_format)) { GimpColorProfile *new_profile; /* when converting between linear and gamma, we create a new * profile using the original profile's chromacities and * whitepoint, but a linear/sRGB-gamma TRC. */ if (gimp_babl_format_get_linear (new_format)) { new_profile = gimp_color_profile_new_linear_from_color_profile (old_profile); } else { new_profile = gimp_color_profile_new_srgb_trc_from_color_profile (old_profile); } /* if a new profile cannot be be generated, convert to the * builtin profile, which is better than leaving the user with * broken colors */ if (! new_profile) { new_profile = gimp_image_get_builtin_color_profile (image); g_object_ref (new_profile); } } for (list = all_drawables, nth_drawable = 0; list; list = g_list_next (list), nth_drawable++) { GimpDrawable *drawable = list->data; gint dither_type; if (gimp_item_is_text_layer (GIMP_ITEM (drawable))) dither_type = text_layer_dither_type; else dither_type = layer_dither_type; if (sub_progress) gimp_sub_progress_set_step (GIMP_SUB_PROGRESS (sub_progress), nth_drawable, n_drawables); gimp_drawable_convert_type (drawable, image, gimp_drawable_get_base_type (drawable), precision, new_profile, dither_type, mask_dither_type, TRUE, sub_progress); } g_list_free (all_drawables); if (old_profile) { gimp_image_set_color_profile (image, new_profile, NULL); g_object_unref (new_profile); } /* convert the selection mask */ { GimpChannel *mask = gimp_image_get_mask (image); GeglBuffer *buffer; if (sub_progress) gimp_sub_progress_set_step (GIMP_SUB_PROGRESS (sub_progress), nth_drawable, n_drawables); gimp_image_undo_push_mask_precision (image, NULL, mask); buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, gimp_image_get_width (image), gimp_image_get_height (image)), gimp_image_get_mask_format (image)); gegl_buffer_copy (gimp_drawable_get_buffer (GIMP_DRAWABLE (mask)), NULL, GEGL_ABYSS_NONE, buffer, NULL); gimp_drawable_set_buffer (GIMP_DRAWABLE (mask), FALSE, NULL, buffer); g_object_unref (buffer); nth_drawable++; } if (sub_progress) gimp_progress_set_value (sub_progress, 1.0); gimp_image_undo_group_end (image); gimp_image_precision_changed (image); g_object_thaw_notify (G_OBJECT (image)); if (sub_progress) g_object_unref (sub_progress); if (progress) gimp_progress_end (progress); }
gboolean gimp_image_convert_color_profile (GimpImage *image, GimpColorProfile *dest_profile, GimpColorRenderingIntent intent, gboolean bpc, GimpProgress *progress, GError **error) { GimpColorProfile *src_profile; GimpColorProfile *builtin_profile; const Babl *layer_format; g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE); g_return_val_if_fail (dest_profile != NULL, FALSE); g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); if (! gimp_image_validate_color_profile (image, dest_profile, error)) return FALSE; src_profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image)); if (gimp_color_profile_is_equal (src_profile, dest_profile)) { g_object_unref (src_profile); return TRUE; } if (progress) gimp_progress_start (progress, FALSE, _("Converting from '%s' to '%s'"), gimp_color_profile_get_label (src_profile), gimp_color_profile_get_label (dest_profile)); gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_CONVERT, _("Color profile conversion")); layer_format = gimp_image_get_layer_format (image, FALSE); if (gimp_babl_format_get_linear (layer_format)) builtin_profile = gimp_color_profile_new_linear_rgb (); else builtin_profile = gimp_color_profile_new_srgb (); if (gimp_color_profile_is_equal (dest_profile, builtin_profile)) { /* don't tag the image with the built-in profile */ gimp_image_set_color_profile (image, NULL, NULL); } else { gimp_image_set_color_profile (image, dest_profile, NULL); } g_object_unref (builtin_profile); /* omg... */ gimp_image_parasite_detach (image, "icc-profile-name"); switch (gimp_image_get_base_type (image)) { case GIMP_RGB: gimp_image_convert_profile_rgb (image, src_profile, dest_profile, intent, bpc, progress); break; case GIMP_GRAY: break; case GIMP_INDEXED: gimp_image_convert_profile_indexed (image, src_profile, dest_profile, intent, bpc, progress); break; } gimp_image_undo_group_end (image); if (progress) gimp_progress_end (progress); g_object_unref (src_profile); return TRUE; }
GtkWidget * convert_precision_dialog_new (GimpImage *image, GimpContext *context, GtkWidget *parent, GimpComponentType component_type, GimpProgress *progress) { ConvertDialog *dialog; GtkWidget *button; GtkWidget *main_vbox; GtkWidget *vbox; GtkWidget *hbox; GtkWidget *label; GtkWidget *frame; GtkWidget *combo; GtkSizeGroup *size_group; const gchar *enum_desc; gchar *blurb; GType dither_type; const Babl *format; gint bits; gboolean linear; g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL); g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL); g_return_val_if_fail (GTK_IS_WIDGET (parent), NULL); g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), NULL); dialog = g_slice_new0 (ConvertDialog); /* a random format with precision */ format = gimp_babl_format (GIMP_RGB, gimp_babl_precision (component_type, FALSE), FALSE); bits = (babl_format_get_bytes_per_pixel (format) * 8 / babl_format_get_n_components (format)); linear = gimp_babl_format_get_linear (gimp_image_get_layer_format (image, FALSE)); dialog->image = image; dialog->progress = progress; dialog->component_type = component_type; dialog->linear = linear; dialog->bits = bits; /* gegl:color-reduction only does 16 bits */ if (bits <= 16) { dialog->layer_dither_type = saved_layer_dither_type; dialog->text_layer_dither_type = saved_text_layer_dither_type; dialog->mask_dither_type = saved_mask_dither_type; } gimp_enum_get_value (GIMP_TYPE_COMPONENT_TYPE, component_type, NULL, NULL, &enum_desc, NULL); blurb = g_strdup_printf (_("Convert Image to %s"), enum_desc); dialog->dialog = gimp_viewable_dialog_new (GIMP_VIEWABLE (image), context, _("Precision Conversion"), "gimp-image-convert-precision", GIMP_STOCK_CONVERT_PRECISION, blurb, parent, gimp_standard_help_func, GIMP_HELP_IMAGE_CONVERT_PRECISION, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL); g_free (blurb); button = gtk_dialog_add_button (GTK_DIALOG (dialog->dialog), _("C_onvert"), GTK_RESPONSE_OK); gtk_button_set_image (GTK_BUTTON (button), gtk_image_new_from_icon_name (GIMP_STOCK_CONVERT_PRECISION, GTK_ICON_SIZE_BUTTON)); gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog->dialog), GTK_RESPONSE_OK, GTK_RESPONSE_CANCEL, -1); gtk_window_set_resizable (GTK_WINDOW (dialog->dialog), FALSE); g_object_weak_ref (G_OBJECT (dialog->dialog), (GWeakNotify) convert_precision_dialog_free, dialog); g_signal_connect (dialog->dialog, "response", G_CALLBACK (convert_precision_dialog_response), dialog); main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12); gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12); gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog->dialog))), main_vbox, TRUE, TRUE, 0); gtk_widget_show (main_vbox); /* dithering */ dither_type = gimp_gegl_get_op_enum_type ("gegl:color-reduction", "dither-strategy"); frame = gimp_frame_new (_("Dithering")); gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0); gtk_widget_show (frame); vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6); gtk_container_add (GTK_CONTAINER (frame), vbox); gtk_widget_show (vbox); /* gegl:color-reduction only does 16 bits */ gtk_widget_set_sensitive (vbox, bits <= 16); size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); /* layers */ hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); gtk_widget_show (hbox); label = gtk_label_new_with_mnemonic (_("_Layers:")); gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5); gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); gtk_size_group_add_widget (size_group, label); gtk_widget_show (label); combo = gimp_enum_combo_box_new (dither_type); gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo); gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0); gtk_widget_show (combo); gimp_int_combo_box_connect (GIMP_INT_COMBO_BOX (combo), dialog->layer_dither_type, G_CALLBACK (gimp_int_combo_box_get_active), &dialog->layer_dither_type); /* text layers */ hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); gtk_widget_show (hbox); label = gtk_label_new_with_mnemonic (_("_Text Layers:")); gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5); gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); gtk_size_group_add_widget (size_group, label); gtk_widget_show (label); combo = gimp_enum_combo_box_new (dither_type); gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo); gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0); gtk_widget_show (combo); gimp_int_combo_box_connect (GIMP_INT_COMBO_BOX (combo), dialog->text_layer_dither_type, G_CALLBACK (gimp_int_combo_box_get_active), &dialog->text_layer_dither_type); gimp_help_set_help_data (combo, _("Dithering text layers will make them uneditable"), NULL); /* channels */ hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); gtk_widget_show (hbox); label = gtk_label_new_with_mnemonic (_("_Channels and Masks:")); gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5); gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); gtk_size_group_add_widget (size_group, label); gtk_widget_show (label); combo = gimp_enum_combo_box_new (dither_type); gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo); gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0); gtk_widget_show (combo); gimp_int_combo_box_connect (GIMP_INT_COMBO_BOX (combo), dialog->mask_dither_type, G_CALLBACK (gimp_int_combo_box_get_active), &dialog->mask_dither_type); g_object_unref (size_group); /* gamma */ frame = gimp_frame_new (_("Gamma")); gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0); gtk_widget_show (frame); vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6); gtk_container_add (GTK_CONTAINER (frame), vbox); gtk_widget_show (vbox); hbox = gimp_int_radio_group_new (FALSE, NULL, G_CALLBACK (gimp_radio_button_update), &dialog->linear, linear, _("Perceptual gamma (sRGB)"), FALSE, NULL, _("Linear light"), TRUE, NULL, NULL); gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); gtk_widget_show (hbox); return dialog->dialog; }