image_page.h

Go to the documentation of this file.
00001 /*
00002  * File Name: image_page.h
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-images.
00007  *
00008  * uds-plugin-images is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-images is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #ifndef _IMAGE_PAGE_H_
00028 #define _IMAGE_PAGE_H_
00029 
00030 #include <string>
00031 #include <gdk-pixbuf/gdk-pixbuf-core.h>
00032 
00033 #include "plugin_type.h"
00034 
00035 namespace images
00036 {
00037 
00038 static const int DEFAULT_COLOR_DEPTH = 8;
00039 
00040 static const float DEFAULT_ZOOM_FACTOR = 100.0f;
00041 
00042 static const PluginRotationDegree DEFAULT_ROTATION = Clockwise_Degrees_0;
00043 
00044 /// Represent render attributes.
00045 struct ImagePageAttrs
00046 {
00047     int original_width;         ///< The original image width.
00048     int original_height;        ///< The original image height.
00049     int desired_width;          ///< The desired image width.
00050     int desired_height;         ///< The desired image height.
00051     int row_stride;             ///< Image width 4 bytes alignment.
00052     int original_color_depth;   ///< The original color depth.
00053     int desired_color_depth;    ///< The desired color depth.
00054     float zoom;                 ///< The zoom factor.
00055     // Quick fix for tracker 0003079: When opening an image it doesn't autorotate to the best fit.
00056     //   Added final_rotation which is rotation as requested by uds + autorotate as decided by plugin.
00057     //   When implementing tracker item 0002533: Auto-rotate content to fit aspect ratio
00058     //   the final_rotation member variable becomes useless and must be removed.
00059     PluginRotationDegree rotation;       ///< The rotation as requested by uds.
00060     PluginRotationDegree final_rotation; ///< The rotation as decided by plugin.
00061     unsigned char *data;        ///< Image data.
00062 
00063     /// Default constructor.
00064     ImagePageAttrs()
00065         : original_width(0)
00066         , original_height(0)
00067         , desired_width(0)
00068         , desired_height(0)
00069         , row_stride(0)
00070         , original_color_depth(0)
00071         , desired_color_depth(DEFAULT_COLOR_DEPTH)
00072         , zoom(DEFAULT_ZOOM_FACTOR)
00073         , rotation(DEFAULT_ROTATION)
00074         , final_rotation(DEFAULT_ROTATION)
00075         , data(0)
00076     {}
00077 
00078     /// Operator =
00079     inline ImagePageAttrs & operator = (const ImagePageAttrs & right)
00080     {
00081         if (this != &right)
00082         {
00083             original_width = right.original_width;
00084             original_height = right.original_height;
00085             desired_width = right.desired_width;
00086             desired_height = right.desired_height;
00087             row_stride = right.row_stride;
00088             desired_color_depth = right.desired_color_depth;
00089             zoom = right.zoom;
00090             rotation = right.rotation;
00091             final_rotation = right.final_rotation;
00092             data = right.data;
00093         }
00094         return *this;
00095     }
00096 };
00097 
00098 /// Represent each image. Through this class, caller is able to
00099 /// - Load a image.
00100 /// - Check the image state is valid or not.
00101 /// - Render a image with desired width, height and zoom factor.
00102 /// - Rotate a image.
00103 /// - Reference the raw image buffer.
00104 /// This class is designed to work in different thread, which means
00105 /// it can work in glib main thread or worker thread. But this class
00106 /// is not thread safety. The caller should make sure use it in thread
00107 /// safety environment.
00108 class ImagePage
00109 {
00110 public:
00111     ImagePage(const std::string & p,
00112               const ImagePageAttrs & request);
00113     ~ImagePage(void);
00114 
00115 public:
00116     /// Render image with specified request.
00117     bool render(void);
00118 
00119     /// Retrieve the attributes of render result.
00120     const ImagePageAttrs & attributes() const { return attrs; }
00121 
00122     /// Retrieve the image path.
00123     const std::string & get_path() const { return path; }
00124 
00125     static int max_zoom_factor() { return 400; }
00126 
00127     static int min_zoom_factor() { return 10; }
00128     
00129     static size_t calc_key(const std::string & anchor,
00130                            int width,
00131                            int height,
00132                            float zoom,
00133                            int rotation);
00134 
00135     // Define the hash function.
00136     size_t operator()(void) const;
00137 
00138     // Define the equal function.
00139     bool operator == (const ImagePage & right);
00140 
00141     // Define operator <
00142     bool operator < (const ImagePage & right);
00143 
00144     // Define operator >
00145     bool operator > (const ImagePage & right);
00146 
00147     // Get the approximate size of memory for the page.
00148     int length(void);
00149 
00150     // Calculate desired image size and rotation
00151     static void calc_desired_dimension(ImagePageAttrs & request);
00152 
00153     // Pre-calculate the approximate size of memory for rendering the page.
00154     static gint64 estimate_length(const ImagePageAttrs & request);
00155 
00156     // Update timestamp
00157     void update_timestamp(void);
00158  
00159     void set_in_use_flag(bool flag) { in_use = flag; }
00160     
00161     bool get_in_use_flag(void) { return in_use; }
00162   
00163     static PluginRotationDegree check_autorotate(int w, int h, int display_w, int display_h);
00164 
00165 private:
00166     /// Make sure g_type system has been initialized. 
00167     void init_type_system(void);
00168 
00169     /// Load the image at scale.
00170     /// Notes: need to consider the rotation value.
00171     bool try_to_load_at_scale(void);
00172 
00173     /// Check the attributes with the request, rotate the
00174     /// image if necessary.
00175     bool try_to_rotate_grey(void);
00176 
00177     /// Check the attributes with the request, dither the image
00178     /// if necessary.
00179     bool try_to_dither(void);
00180 
00181     /// Update all the other fields of attributes.
00182     bool update_attributes(void);
00183 
00184     /// Destroy the image.
00185     void destroy();
00186 
00187 private:
00188     std::string path;         ///< Image absolute path.
00189     ImagePageAttrs attrs;     ///< The image attributes.
00190  
00191     /// Indicate the page is in used by UDS host.
00192     /// The page in use that means it can't be removed 
00193     //  until UDS doesn't use it anymore.
00194     bool in_use;
00195 
00196     int display_width;
00197     int display_height;       ///< display size 
00198     
00199     long timestamp;            ///< 
00200     
00201     static bool g_type_initialize;
00202     GdkPixbuf   *loader;      ///< The gdk pixbuf object.
00203     bool        valid;        ///< Is a valid image.
00204 };
00205 
00206 }; // namespace images
00207 
00208 #endif
00209 
00210 
Generated by  doxygen 1.6.2-20100208