
* scribo/io/xml/internal/compute_text_colour.hh: New. * scribo/io/xml/internal/extended_page_xml_visitor.hh, * scribo/io/xml/internal/full_xml_visitor.hh, * scribo/io/xml/load.hh: Load and save color as hex format. --- scribo/ChangeLog | 26 ++++-- .../scribo/io/xml/internal/compute_text_colour.hh | 108 ++++++++++++++++++++ .../io/xml/internal/extended_page_xml_visitor.hh | 11 ++- scribo/scribo/io/xml/internal/full_xml_visitor.hh | 11 ++- scribo/scribo/io/xml/load.hh | 26 +---- 5 files changed, 148 insertions(+), 34 deletions(-) create mode 100644 scribo/scribo/io/xml/internal/compute_text_colour.hh diff --git a/scribo/ChangeLog b/scribo/ChangeLog index 2fb884b..f4dcf15 100644 --- a/scribo/ChangeLog +++ b/scribo/ChangeLog @@ -1,8 +1,18 @@ -2011-05-06 Guillaume Lazzara <lazzara@fidji.lrde.epita.fr> +2011-05-06 Guillaume Lazzara <lazzara@lrde.epita.fr> + + Load and save color information. + + * scribo/io/xml/internal/compute_text_colour.hh: New. + + * scribo/io/xml/internal/extended_page_xml_visitor.hh, + * scribo/io/xml/internal/full_xml_visitor.hh, + * scribo/io/xml/load.hh: Load and save color as hex format. + +2011-05-06 Guillaume Lazzara <lazzara@lrde.epita.fr> * scribo/text/paragraphs.hh: Remove unused argument. -2011-05-06 Guillaume Lazzara <lazzara@fidji.lrde.epita.fr> +2011-05-06 Guillaume Lazzara <lazzara@lrde.epita.fr> Fix namespace ambiguities. @@ -12,7 +22,7 @@ * scribo/convert/to_base64.hh, * scribo/preprocessing/denoise_bg.hh: Here. -2011-05-06 Guillaume Lazzara <lazzara@fidji.lrde.epita.fr> +2011-05-06 Guillaume Lazzara <lazzara@lrde.epita.fr> Improve PDF output. @@ -23,7 +33,7 @@ * demo/xml2doc/templates/pdf/regions.xsl: Fix image localization. -2011-05-05 Guillaume Lazzara <lazzara@fidji.lrde.epita.fr> +2011-05-05 Guillaume Lazzara <lazzara@lrde.epita.fr> Add hexadecimal color conversion routines. @@ -35,7 +45,7 @@ * tests/util/color_to_hex.cc, * tests/util/hex_to_color.cc: New. -2011-05-05 Guillaume Lazzara <lazzara@fidji.lrde.epita.fr> +2011-05-05 Guillaume Lazzara <lazzara@lrde.epita.fr> Explicitly set vertical or horizontal attribute for separators in XML output. @@ -53,7 +63,7 @@ * scribo/io/xml/internal/page_xml_visitor.hh: Update enum declaration and handle new cases. -2011-05-05 Guillaume Lazzara <lazzara@fidji.lrde.epita.fr> +2011-05-05 Guillaume Lazzara <lazzara@lrde.epita.fr> Make use of opacity in PDF output. @@ -62,7 +72,7 @@ * scribo/io/xml/internal/extended_page_xml_visitor.hh: Add bbox information in region tag. -2011-05-04 Guillaume Lazzara <lazzara@fidji.lrde.epita.fr> +2011-05-04 Guillaume Lazzara <lazzara@lrde.epita.fr> Small fixes in Scribo. @@ -81,7 +91,7 @@ * src/binarization/sauvola_ms_fg.cc: Fix argument use. -2011-05-04 Guillaume Lazzara <lazzara@fidji.lrde.epita.fr> +2011-05-04 Guillaume Lazzara <lazzara@lrde.epita.fr> Make paragraph extraction work with this branch. diff --git a/scribo/scribo/io/xml/internal/compute_text_colour.hh b/scribo/scribo/io/xml/internal/compute_text_colour.hh new file mode 100644 index 0000000..1caf358 --- /dev/null +++ b/scribo/scribo/io/xml/internal/compute_text_colour.hh @@ -0,0 +1,108 @@ +// Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to produce +// an executable, this file does not by itself cause the resulting +// executable to be covered by the GNU General Public License. This +// exception does not however invalidate any other reasons why the +// executable file might be covered by the GNU General Public License. + +#ifndef SCRIBO_IO_XML_INTERNAL_COMPUTE_TEXT_COLOUR_HH +# define SCRIBO_IO_XML_INTERNAL_COMPUTE_TEXT_COLOUR_HH + +/// \file +/// +/// Find the color name of a given color. + +# include <mln/value/rgb8.hh> +# include <mln/make/vec.hh> +# include <mln/norm/l2.hh> + + +namespace scribo +{ + + namespace io + { + + namespace xml + { + + namespace internal + { + + const char * + compute_txt_text_colour(const value::rgb8& v); + + + struct color_t + { + const algebra::vec<3, value::int_u8> v; + const char *name; + float res; + }; + + +# ifndef MLN_INCLUDE_ONLY + + + const char * + compute_txt_text_colour(const value::rgb8& v) + { + static color_t colors[] = { + { mln::make::vec(0, 0, 0), "Black", 0 }, + { mln::make::vec(255, 0, 0), "Red", 0 }, + { mln::make::vec(255, 255, 255), "White", 0 }, + { mln::make::vec(0, 255, 0), "Green", 0 }, + { mln::make::vec(0, 0, 255), "Blue", 0 }, + { mln::make::vec(255, 255, 0), "Yellow", 0 }, + { mln::make::vec(255, 165, 0), "Orange", 0 }, + { mln::make::vec(255, 192, 203), "Pink", 0 }, + { mln::make::vec(192, 192, 192), "Grey", 0 }, + { mln::make::vec(64, 224, 208), "Turquoise", 0 }, + { mln::make::vec(75, 0, 130), "Indigo", 0 }, + { mln::make::vec(238, 130, 238), "Violet", 0 }, + { mln::make::vec(0, 255, 255), "Cyan", 0 }, + { mln::make::vec(255, 0, 255), "Magenta", 0 }, + { mln::make::vec(0, 0, 0), 0, 0 } // Invalid + }; + + unsigned closest = 0; + for (unsigned i = 0; colors[i].name; ++i) + { + colors[i].res = norm::l2_distance(colors[i].v, v.to_equiv()); + if (colors[closest].res > colors[i].res) + closest = i; + } + + return colors[closest].name; + } + +#endif // MLN_INCLUDE_ONLY + + } // end of namespace scribo::io::xml::internal + + } // end of namespace scribo::io::xml + + } // end of namespace scribo::io + +} // end of namespace scribo + + +#endif // ! SCRIBO_IO_XML_INTERNAL_COMPUTE_TEXT_COLOUR_HH diff --git a/scribo/scribo/io/xml/internal/extended_page_xml_visitor.hh b/scribo/scribo/io/xml/internal/extended_page_xml_visitor.hh index 83e5b0b..869795b 100644 --- a/scribo/scribo/io/xml/internal/extended_page_xml_visitor.hh +++ b/scribo/scribo/io/xml/internal/extended_page_xml_visitor.hh @@ -50,7 +50,9 @@ # include <scribo/io/xml/internal/print_box_coords.hh> # include <scribo/io/xml/internal/print_image_coords.hh> # include <scribo/io/xml/internal/print_page_preambule.hh> +# include <scribo/io/xml/internal/compute_text_colour.hh> +# include <scribo/util/color_to_hex.hh> namespace scribo { @@ -122,7 +124,8 @@ namespace scribo if (doc.has_elements()) { // Prepare element edges - elt_edge = morpho::elementary::gradient_internal(doc.elements().labeled_image(), c4()); + elt_edge = morpho::elementary::gradient_internal( + extend(doc.elements().labeled_image(), 0), c4()).unmorph_(); doc.elements().accept(*this); } @@ -264,11 +267,12 @@ namespace scribo << "\" txt_text_type=\"" << lines(fid).type() << "\" txt_reverse_video=\"" << (lines(fid).reverse_video() ? "true" : "false") << "\" txt_indented=\"" << (lines(fid).indented() ? "true" : "false") + << "\" txt_text_colour=\"" << internal::compute_txt_text_colour(parset(p).color()) << "\" kerning=\"" << lines(fid).char_space(); // EXTENSIONS - Not officially supported // FIXME: add boldness? - output << "\" color=\"" << parset(p).color() + output << "\" color=\"" << scribo::util::color_to_hex(parset(p).color()) << "\" color_reliability=\"" << parset(p).color_reliability() << "\" baseline=\"" << lines(fid).baseline() << "\" meanline=\"" << lines(fid).meanline() @@ -309,7 +313,7 @@ namespace scribo output << "id=\"" << line.id() << "\" boldness=\"" << line.boldness() << "\" boldness_reliability=\"" << line.boldness_reliability() - << "\" color=\"" << line.color() + << "\" color=\"" << scribo::util::color_to_hex(line.color()) << "\" color_reliability=\"" << line.color_reliability() << "\" txt_orientation=\"" << line.orientation() << "\" txt_reading_orientation=\"" << line.reading_orientation() @@ -317,6 +321,7 @@ namespace scribo << "\" txt_text_type=\"" << line.type() << "\" txt_reverse_video=\"" << (line.reverse_video() ? "true" : "false") << "\" txt_indented=\"" << (line.indented() ? "true" : "false") + << "\" txt_text_colour=\"" << internal::compute_txt_text_colour(line.color()) << "\" kerning=\"" << line.char_space() << "\" baseline=\"" << line.baseline() << "\" meanline=\"" << line.meanline() diff --git a/scribo/scribo/io/xml/internal/full_xml_visitor.hh b/scribo/scribo/io/xml/internal/full_xml_visitor.hh index b920ca8..6b59e60 100644 --- a/scribo/scribo/io/xml/internal/full_xml_visitor.hh +++ b/scribo/scribo/io/xml/internal/full_xml_visitor.hh @@ -46,6 +46,9 @@ # include <scribo/io/xml/internal/save_image_to_xml.hh> # include <scribo/io/xml/internal/print_box_coords.hh> # include <scribo/io/xml/internal/print_page_preambule.hh> +# include <scribo/io/xml/internal/compute_text_colour.hh> + +# include <scribo/util/color_to_hex.hh> namespace scribo { @@ -103,6 +106,8 @@ namespace scribo full_xml_visitor::full_xml_visitor(std::ofstream& out) : output(out) { + output << std::setiosflags(std::ios::fixed); + output.precision(26); } @@ -448,6 +453,7 @@ namespace scribo << "\" txt_text_type=\"" << lines(fid).type() << "\" txt_reverse_video=\"" << (lines(fid).reverse_video() ? "true" : "false") << "\" txt_indented=\"" << (lines(fid).indented() ? "true" : "false") + << "\" txt_text_colour=\"" << internal::compute_txt_text_colour(parset(p).color()) << "\" kerning=\"" << lines(fid).char_space(); // EXTENSIONS - Not officially supported @@ -457,7 +463,7 @@ namespace scribo << "\" d_height=\"" << lines(fid).d_height() << "\" a_height=\"" << lines(fid).a_height() << "\" char_width=\"" << lines(fid).char_width() - << "\" color=\"" << parset(p).color() + << "\" color=\"" << scribo::util::color_to_hex(parset(p).color()) << "\" color_reliability=\"" << parset(p).color_reliability(); // End of EXTENSIONS output << "\">" @@ -493,7 +499,7 @@ namespace scribo output << "id=\"" << line.id() << "\" boldness=\"" << line.boldness() << "\" boldness_reliability=\"" << line.boldness_reliability() - << "\" color=\"" << line.color() + << "\" color=\"" << scribo::util::color_to_hex(line.color()) << "\" color_reliability=\"" << line.color_reliability() << "\" txt_orientation=\"" << line.orientation() << "\" txt_reading_orientation=\"" << line.reading_orientation() @@ -501,6 +507,7 @@ namespace scribo << "\" txt_text_type=\"" << line.type() << "\" txt_reverse_video=\"" << (line.reverse_video() ? "true" : "false") << "\" txt_indented=\"" << (line.indented() ? "true" : "false") + << "\" txt_text_colour=\"" << internal::compute_txt_text_colour(line.color()) << "\" kerning=\"" << line.char_space() << "\" baseline=\"" << line.baseline() << "\" meanline=\"" << line.meanline() diff --git a/scribo/scribo/io/xml/load.hh b/scribo/scribo/io/xml/load.hh index 8d89085..4e90b20 100644 --- a/scribo/scribo/io/xml/load.hh +++ b/scribo/scribo/io/xml/load.hh @@ -50,6 +50,8 @@ # include <scribo/core/line_set.hh> # include <scribo/core/line_info.hh> +# include <scribo/util/hex_to_color.hh> + namespace scribo { @@ -153,24 +155,6 @@ namespace scribo }; - namespace internal - { - - value::rgb8 parse_color(const QString& color_str) - { - QString color = color_str; - color.chop(1); - color = color.remove(0, 1); - QStringList rgb = color.split(','); - - return - value::rgb8(rgb.at(0).toInt(), - rgb.at(1).toInt(), - rgb.at(2).toInt()); - } - - } - template <typename L> class xml_handler : public QXmlDefaultHandler { @@ -228,7 +212,7 @@ namespace scribo { component_features_data comp_features; comp_features.valid = true; - comp_features.color = internal::parse_color(atts.value("color")); + comp_features.color = scribo::util::hex_to_color(atts.value("color")); comp_features.boldness = atts.value("boldness").toFloat(); comp_set_data->infos_.last().update_features(comp_features); @@ -277,7 +261,7 @@ namespace scribo // qDebug() << "TextRegion"; current_paragraph = paragraph_info<L>(llinks); - current_paragraph.set_color_(internal::parse_color(atts.value("color"))); + current_paragraph.set_color_(scribo::util::hex_to_color(atts.value("color"))); current_paragraph.set_color_reliability_(atts.value("color_reliability").toFloat()); } break; @@ -324,7 +308,7 @@ namespace scribo line_data->boldness_ = atts.value("boldness").toFloat(); line_data->boldness_reliability_ = atts.value("boldness_reliability").toFloat(); - line_data->color_ = internal::parse_color(atts.value("color")); + line_data->color_ = scribo::util::hex_to_color(atts.value("color")); line_data->color_reliability_ = atts.value("color_reliability").toFloat(); -- 1.5.6.5