From adam@uunet.pipex.com Wed Apr 30 08:46:39 1997 Date: Wed, 30 Apr 1997 10:22:56 GMT From: "Adam D. Moss" To: gimp-developer@scam.xcf.berkeley.edu Cc: adam@sierra.sci-park.uunet.pipex.com Subject: [gimp-devel][patch] more convert.c fluff Hi! This patch to app/convert.c does the following things to the RGB->indexed conversion process: 1) Makes the results of quantization a little perkier (red, green and blue differences are weighted by their contributing intensities). 2) Displays a nonthreatening warning when you convert a RGBA/RGBA-layered image to indexed, and sets the default number of colours to quantize to, to 255 instead of 256 in this case. The rationale for this is that (at least in the future) when someone converts a layered or transparent RGB file to INDEXED mode they will be most likely be intending to save it as an animated/transparent GIF. If this is the case, at least one of the 256 potential colours in the image has to remain unused, so that it can be substituted for the alpha channel when saved (it's just the stupid way that GIFs work). It works out less destructive to quantize from RGB->255 indices than to quantize from RGB->256 indices and then have the GIF saver re-quantize to 255 indices (which I need to make an option in gif.c anyway, but isn't optimal for quality), which is why the new convert.c makes this friendly suggestion (it's a suggestion, not a rule) at the colour-reduction stage. --Adam *** convert.c Sun Apr 27 04:59:29 1997 --- ../../convert.c Wed Apr 30 08:46:54 1997 *************** *** 40,45 **** --- 40,46 ---- #define REUSE_PALETTE 1 #define WEB_PALETTE 2 #define MONO_PALETTE 3 + #define PRECISION_R 6 #define PRECISION_G 6 #define PRECISION_B 5 *************** *** 57,65 **** #define G_SHIFT (BITS_IN_SAMPLE-PRECISION_G) #define B_SHIFT (BITS_IN_SAMPLE-PRECISION_B) ! #define R_SCALE 2 /* scale R distances by this much */ ! #define G_SCALE 3 /* scale G distances by this much */ ! #define B_SCALE 1 /* and B by this much */ unsigned char webpal[] = { --- 58,68 ---- #define G_SHIFT (BITS_IN_SAMPLE-PRECISION_G) #define B_SHIFT (BITS_IN_SAMPLE-PRECISION_B) ! #define R_SCALE 30 /* scale R distances by this much */ ! #define G_SCALE 59 /* scale G distances by this much */ ! #define B_SCALE 11 /* and B by this much */ ! ! #define INTENSITY(r,g,b) (r * 0.30 + g * 0.59 + b * 0.11) unsigned char webpal[] = { *************** *** 226,231 **** --- 229,252 ---- dialog->num_cols = 256; dialog->dither = TRUE; + /* if the image isn't non-alpha/layered, set the default number of + colours to one less than max, to leave room for a transparent index + for transparent/animated GIFs */ + if ((!gimage_is_empty (gimage)) + && + ( + gimage->layers->next + || + layer_has_alpha((Layer *) gimage->layers->data) + ) + ) + { + dialog->num_cols = 255; + message_box ("Note: You are attempting to convert an RGB image\nwith alpha/layers. It is recommended that you quantize\nto no more than 255 colors if you wish to make\na transparent or animated GIF from it.", + NULL, NULL); + } + + dialog->makepal_flag = TRUE; dialog->webpal_flag = FALSE; dialog->monopal_flag = FALSE; *************** *** 264,270 **** gtk_widget_show (label); text = gtk_entry_new (); ! gtk_entry_set_text (GTK_ENTRY (text), "256"); gtk_widget_set_usize (text, 50, 25); gtk_box_pack_start (GTK_BOX (hbox), text, FALSE, FALSE, 0); gtk_signal_connect (GTK_OBJECT (text), "changed", --- 285,301 ---- gtk_widget_show (label); text = gtk_entry_new (); ! if ((!gimage_is_empty (gimage)) ! && ! ( ! gimage->layers->next ! || ! layer_has_alpha((Layer *) gimage->layers->data) ! ) ! ) ! gtk_entry_set_text (GTK_ENTRY (text), "255"); ! else ! gtk_entry_set_text (GTK_ENTRY (text), "256"); gtk_widget_set_usize (text, 50, 25); gtk_box_pack_start (GTK_BOX (hbox), text, FALSE, FALSE, 0); gtk_signal_connect (GTK_OBJECT (text), "changed", *************** *** 631,638 **** TileManager *new_tiles, int old_type) { - #define INTENSITY(r,g,b) (r * 0.30 + g * 0.59 + b * 0.11) - PixelRegion srcPR, destPR; int row, col; int offset, val; --- 662,667 ----