int main(int argc, char** argv) { struct image_t* image; struct image_t* new_image; struct palette_coeff_t* palette; unsigned int k; COLOR_TYPE* new_coeff; unsigned int new_coeff_size; unsigned int i; if(argc != 4) { print_command_line(); return EXIT_FAILURE; } else { image = read_image(argv[1]); k = atoi(argv[2]); } fflush(stdout); palette = create_palette(image); printf("reduce palette : %d\n", reduce_palette(palette, 0, k)); new_coeff = backtrace_palette_index(palette->model->size, 0, k, &new_coeff_size, palette); printf("New color : "); for(i = 0; i<new_coeff_size; i++) { printf("%d ", new_coeff[i]); } printf("\n"); new_image = construct_image(new_coeff, new_coeff_size, image, palette->model); save_image(argv[3], new_image); free(new_image->pixels); free(new_image); free(new_coeff); free(image->pixels); free(image); free(palette->model->data); free(palette->model); free(palette->coeff); free(palette); return EXIT_SUCCESS; }
TEST(Palette, CreatePointerAndSize) { const int kSize = 3; const rgb888_t data[kSize] = {0xef8a62, 0xf7f7f7, 0x67a9cf}; const sRgb colors[kSize] = {{0.937255f, 0.5411765f, 0.38431373f}, {0.96862745f, 0.96862745f, 0.96862745f}, {0.40392157f, 0.66274512f, 0.81176472f}}; Palette palette = create_palette(data, kSize); ASSERT_EQ(kSize, palette.size()); for (int i = 0; i < kSize; i++) { COLOR_ASSERT_FLOAT_EQ(colors[i], palette[i]); } }
void create_palette(std::vector<rgb> & palette, node * itr) const { if (itr->count != 0) { unsigned count = itr->count; palette.push_back(rgb(byte(itr->reds/float(count)), byte(itr->greens/float(count)), byte(itr->blues/float(count)))); itr->index = static_cast<unsigned>(palette.size()) - 1; } for (unsigned i=0; i < 8 ;++i) { if (itr->children_[i] != 0) { create_palette(palette, itr->children_[i]); } } }
static void install_palette (xmlNodePtr palette, gboolean hide) { gchar *name = (gchar *) xmlGetProp (palette, (xmlChar *) "_name"); gboolean hidden = getXMLIntProp (palette, (xmlChar *) "hidden"); gboolean row_wise = getXMLIntProp (palette, (xmlChar *) "row-wise"); gboolean dock = getXMLIntProp (palette, (xmlChar *) "dock"); gint limit = getXMLIntProp (palette, (xmlChar *) "limit"); DenemoPalette *pal = create_palette (name, dock, row_wise); set_palate_shape (name, row_wise, limit);//does gtk_widget_show in repack installButtons (palette, pal); if (hide) hidden = TRUE; if(hidden) gtk_widget_hide(pal->docked?pal->box:pal->window); else gtk_widget_show(pal->docked?pal->box:pal->window); if (pal->buttons==NULL) { delete_palette (pal); } }
static gint merge_palette (xmlNodePtr palette, const gchar *sought) { gchar *name = (gchar *) xmlGetProp (palette, (xmlChar *) "_name"); gboolean hidden = getXMLIntProp (palette, (xmlChar *) "hidden"); gboolean row_wise = getXMLIntProp (palette, (xmlChar *) "row-wise"); gboolean dock = getXMLIntProp (palette, (xmlChar *) "dock"); gint limit = getXMLIntProp (palette, (xmlChar *) "limit"); if(!strcmp(name, sought)) { DenemoPalette *pal = create_palette (name, dock, row_wise); set_palate_shape (name, row_wise, limit);//does gtk_widget_show in repack installButtons (palette, pal); gtk_widget_show(pal->docked?pal->box:pal->window); if (pal->buttons==NULL) { delete_palette (pal); return -1; } return 0; } return -1; }
void create_palette(std::vector<rgb> & palette) { reduce(); palette.reserve(colors_); create_palette(palette, root_); }
GtkWidget * color_create_widget (GtkWidget *dlg) { GtkWidget *w; #if !GTK_CHECK_VERSION(3,0,0) w = gtk_vbox_new (FALSE, 2); #else w = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2); #endif color = gtk_color_selection_new (); gtk_widget_set_name (color, "yad-color-widget"); gtk_color_selection_set_has_palette (GTK_COLOR_SELECTION (color), settings.show_gtk_palette); if (options.color_data.init_color) { GdkColor c; if (gdk_color_parse (options.color_data.init_color, &c)) gtk_color_selection_set_current_color (GTK_COLOR_SELECTION (color), &c); } gtk_box_pack_start (GTK_BOX (w), color, FALSE, FALSE, 2); if (options.color_data.use_palette) { GtkTreeModel *model; if ((model = create_palette ()) != NULL) { GtkWidget *exp, *sw, *list; GtkCellRenderer *r; GtkTreeViewColumn *col; GtkTreeSelection *sel; /* create expander */ exp = gtk_expander_new (_("Palette")); gtk_expander_set_expanded (GTK_EXPANDER (exp), settings.expand_palette); gtk_container_set_border_width (GTK_CONTAINER (exp), 5); gtk_box_pack_start (GTK_BOX (w), exp, TRUE, TRUE, 2); /* create color list */ sw = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_container_add (GTK_CONTAINER (exp), sw); list = gtk_tree_view_new_with_model (model); gtk_widget_set_name (list, "yad-color-palette"); gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (list), settings.rules_hint); g_object_unref (model); gtk_container_add (GTK_CONTAINER (sw), list); /* add first columns */ col = gtk_tree_view_column_new (); gtk_tree_view_column_set_title (col, _("Color")); /* pixbuf */ r = gtk_cell_renderer_pixbuf_new (); gtk_tree_view_column_pack_start (col, r, FALSE); gtk_tree_view_column_set_attributes (col, r, "pixbuf", 0, NULL); /* color value */ r = gtk_cell_renderer_text_new (); gtk_tree_view_column_pack_start (col, r, TRUE); gtk_tree_view_column_set_attributes (col, r, "text", 1, NULL); gtk_tree_view_column_set_sort_column_id (col, 1); gtk_tree_view_append_column (GTK_TREE_VIEW (list), col); /* add second column */ r = gtk_cell_renderer_text_new (); col = gtk_tree_view_column_new_with_attributes (_("Name"), r, "text", 2, NULL); gtk_tree_view_column_set_sort_column_id (col, 2); gtk_tree_view_append_column (GTK_TREE_VIEW (list), col); /* enable searching on Name column */ gtk_tree_view_set_search_column (GTK_TREE_VIEW (list), 2); /* setup the selection handler */ sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (list)); gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE); g_signal_connect (G_OBJECT (sel), "changed", G_CALLBACK (palette_changed), NULL); gtk_widget_show_all (exp); } } return w; }
// Create a palette from a fixed size rgb888_t array. template <std::size_t Size> Palette create_palette(const rgb888_t(&values)[Size]) { return create_palette(values, Size); }
static rl2SectionPtr create_4colors () { /* creating a synthetic "4colors" image */ rl2SectionPtr scn; rl2RasterPtr rst; rl2PalettePtr palette = create_palette (); int row; int col; int bufsize = 1024 * 1024; int idx = 0; unsigned char *bufpix = malloc (bufsize); unsigned char *p = bufpix; if (bufpix == NULL) return NULL; if (palette == NULL) goto error; for (row = 0; row < 256; row += 4) { int i; for (i = 0; i < 4; i++) { int ix = idx; for (col = 0; col < 1024; col += 4) { *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; ix++; if (ix > 3) ix = 0; } } idx++; if (idx > 3) idx = 0; } for (row = 256; row < 512; row += 8) { int i; for (i = 0; i < 8; i++) { int ix = idx; for (col = 0; col < 1024; col += 8) { *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; ix++; if (ix > 3) ix = 0; } } idx++; if (idx > 3) idx = 0; } for (row = 512; row < 768; row++) { int ix = idx; for (col = 0; col < 1024; col += 8) { *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; *p++ = ix; ix++; if (ix > 3) ix = 0; } } for (row = 768; row < 1024; row += 8) { int i; for (i = 0; i < 8; i++) { for (col = 0; col < 1024; col++) *p++ = idx; } idx++; if (idx > 3) idx = 0; } rst = rl2_create_raster (1024, 1024, RL2_SAMPLE_2_BIT, RL2_PIXEL_PALETTE, 1, bufpix, bufsize, palette, NULL, 0, NULL); if (rst == NULL) goto error; scn = rl2_create_section ("4colors", RL2_COMPRESSION_NONE, RL2_TILESIZE_UNDEFINED, RL2_TILESIZE_UNDEFINED, rst); if (scn == NULL) rl2_destroy_raster (rst); return scn; error: free (bufpix); if (palette != NULL) rl2_destroy_palette (palette); return NULL; }
int main (int argc, char *argv[]) { rl2SectionPtr img; rl2RasterPtr raster; rl2RasterStatisticsPtr stats; unsigned char *blob_odd; int blob_odd_sz; unsigned char *blob_even; int blob_even_sz; unsigned char *blob_odd_png; int blob_odd_sz_png; unsigned char *blob_even_png; int blob_even_sz_png; unsigned char *blob_odd_gif; int blob_odd_sz_gif; unsigned char *blob_even_gif; int blob_even_sz_gif; int endian = naturalEndian (); rl2PalettePtr palette; rl2PalettePtr plt2; unsigned char *blob_stat; int blob_stat_size; if (argc > 1 || argv[0] == NULL) argc = 1; /* silencing stupid compiler warnings */ img = create_4colors (); if (img == NULL) { fprintf (stderr, "Unable to create image: 4colors\n"); return -1; } if (rl2_section_to_jpeg (img, "./4colors.jpg", 70) != RL2_OK) { fprintf (stderr, "Unable to write: 4colors.jpg\n"); return -2; } if (rl2_section_to_png (img, "./4colors.png") != RL2_OK) { fprintf (stderr, "Unable to write: 4colors.png\n"); return -3; } if (!test_4colors (img)) return -4; rl2_destroy_section (img); unlink ("./4colors.jpg"); img = rl2_section_from_png ("./4colors.png"); if (img == NULL) { fprintf (stderr, "Unable to read: %s\n", "./4colors.png"); return -5; } unlink ("./4colors.png"); if (rl2_section_to_png (img, "./from_4colors.png") != RL2_OK) { fprintf (stderr, "Unable to write: from_4colors.png\n"); return -6; } unlink ("./from_4colors.png"); raster = rl2_get_section_raster (img); if (raster == NULL) { fprintf (stderr, "Unable to retrieve the raster pointer\n"); return -7; } if (rl2_raster_encode (raster, RL2_COMPRESSION_NONE, &blob_odd, &blob_odd_sz, &blob_even, &blob_even_sz, 0, endian) != RL2_OK) { fprintf (stderr, "Unable to Encode - uncompressed\n"); return -8; } if (rl2_raster_encode (raster, RL2_COMPRESSION_PNG, &blob_odd_png, &blob_odd_sz_png, &blob_even_png, &blob_even_sz_png, 0, endian) != RL2_OK) { fprintf (stderr, "Unable to Encode - PNG\n"); return -9; } if (rl2_raster_encode (raster, RL2_COMPRESSION_GIF, &blob_odd_gif, &blob_odd_sz_gif, &blob_even_gif, &blob_even_sz_gif, 0, endian) == RL2_OK) { fprintf (stderr, "Unexpected result - GIF\n"); return -10; } plt2 = rl2_clone_palette (rl2_get_raster_palette (raster)); stats = rl2_get_raster_statistics (blob_odd_png, blob_odd_sz_png, blob_even_png, blob_even_sz_png, plt2, NULL); if (stats == NULL) { fprintf (stderr, "Unable to get Raster Statistics\n"); return -100; } if (rl2_serialize_dbms_raster_statistics (stats, &blob_stat, &blob_stat_size) != RL2_OK) { fprintf (stderr, "Unable to serialize Raster Statistics\n"); return -101; } rl2_destroy_raster_statistics (stats); stats = rl2_deserialize_dbms_raster_statistics (blob_stat, blob_stat_size); if (stats == NULL) { fprintf (stderr, "Unable to deserialize Raster Statistics\n"); return -102; } free (blob_stat); rl2_destroy_raster_statistics (stats); rl2_destroy_section (img); palette = create_palette (); raster = rl2_raster_decode (RL2_SCALE_1, blob_odd, blob_odd_sz, blob_even, blob_even_sz, palette); if (raster == NULL) { fprintf (stderr, "Unable to Decode 1:1 - uncompressed\n"); return -11; } img = rl2_create_section ("4colors 1:1", RL2_COMPRESSION_NONE, RL2_TILESIZE_UNDEFINED, RL2_TILESIZE_UNDEFINED, raster); if (img == NULL) { fprintf (stderr, "Unable to create a Section 1:1 - uncompressed\n"); return -12; } if (rl2_section_to_png (img, "./4colors_1_1_uncompressed.png") != RL2_OK) { fprintf (stderr, "Unable to write: 4colors_1_1_uncompressed.png\n"); return -13; } rl2_destroy_section (img); unlink ("./4colors_1_1_uncompressed.png"); if (rl2_raster_decode (RL2_SCALE_2, blob_odd, blob_odd_sz, blob_even, blob_even_sz, NULL) != NULL) { fprintf (stderr, "Unexpected result: Decode 1:2 - uncompressed\n"); return -14; } if (rl2_raster_decode (RL2_SCALE_4, blob_odd, blob_odd_sz, blob_even, blob_even_sz, NULL) != NULL) { fprintf (stderr, "Unexpected result: Decode 1:4 - uncompressed\n"); return -15; } if (rl2_raster_decode (RL2_SCALE_8, blob_odd, blob_odd_sz, blob_even, blob_even_sz, NULL) != NULL) { fprintf (stderr, "Unexpected result: Decode 1:8 - uncompressed\n"); return -16; } free (blob_odd); raster = rl2_raster_decode (RL2_SCALE_1, blob_odd_png, blob_odd_sz_png, blob_even_png, blob_even_sz_png, NULL); if (raster == NULL) { fprintf (stderr, "Unable to Decode 1:1 - png\n"); return -17; } img = rl2_create_section ("4colors 1:1", RL2_COMPRESSION_NONE, RL2_TILESIZE_UNDEFINED, RL2_TILESIZE_UNDEFINED, raster); if (img == NULL) { fprintf (stderr, "Unable to create a Section 1:1 - png\n"); return -18; } if (rl2_section_to_png (img, "./4colors_1_1_png.png") != RL2_OK) { fprintf (stderr, "Unable to write: 4colors_1_1_png.png\n"); return -19; } rl2_destroy_section (img); unlink ("./4colors_1_1_png.png"); if (rl2_raster_decode (RL2_SCALE_2, blob_odd_png, blob_odd_sz_png, blob_even_png, blob_even_sz_png, NULL) != NULL) { fprintf (stderr, "Unexpected result: Decode 1:2 - png\n"); return -20; } free (blob_odd_png); return 0; }