source: trunk/libdjvu/GRect.h @ 15

Last change on this file since 15 was 15, checked in by Eugene Romanenko, 15 years ago

needed libs update

File size: 15.7 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, Version 2. The license should have
9//C- accompanied the software or you may obtain a copy of the license
10//C- from the Free Software Foundation at http://www.fsf.org .
11//C-
12//C- This program is distributed in the hope that it will be useful,
13//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//C- GNU General Public License for more details.
16//C-
17//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
18//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech
19//C- Software authorized us to replace the original DjVu(r) Reference
20//C- Library notice by the following text (see doc/lizard2002.djvu):
21//C-
22//C-  ------------------------------------------------------------------
23//C- | DjVu (r) Reference Library (v. 3.5)
24//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
25//C- | The DjVu Reference Library is protected by U.S. Pat. No.
26//C- | 6,058,214 and patents pending.
27//C- |
28//C- | This software is subject to, and may be distributed under, the
29//C- | GNU General Public License, Version 2. The license should have
30//C- | accompanied the software or you may obtain a copy of the license
31//C- | from the Free Software Foundation at http://www.fsf.org .
32//C- |
33//C- | The computer code originally released by LizardTech under this
34//C- | license and unmodified by other parties is deemed "the LIZARDTECH
35//C- | ORIGINAL CODE."  Subject to any third party intellectual property
36//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
37//C- | non-exclusive license to make, use, sell, or otherwise dispose of
38//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
39//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
40//C- | General Public License.   This grant only confers the right to
41//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
42//C- | the extent such infringement is reasonably necessary to enable
43//C- | recipient to make, have made, practice, sell, or otherwise dispose
44//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
45//C- | any greater extent that may be necessary to utilize further
46//C- | modifications or combinations.
47//C- |
48//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
49//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
50//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
51//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
52//C- +------------------------------------------------------------------
53//
54// $Id: GRect.h,v 1.9 2003/11/07 22:08:21 leonb Exp $
55// $Name: release_3_5_16 $
56
57#ifndef _GRECT_H_
58#define _GRECT_H_
59#ifdef HAVE_CONFIG_H
60#include "config.h"
61#endif
62#if NEED_GNUG_PRAGMAS
63# pragma interface
64#endif
65
66
67/** @name GRect.h
68    Files #"GRect.h"# and #"GRect.cpp"# implement basic operations on
69    rectangles. Class \Ref{GRect} is used to represent rectangles.  Class
70    \Ref{GRectMapper} represent the correspondence between points relative to
71    given rectangles.  Class \Ref{GRatio} is used to represent scaling factors
72    as rational numbers.
73    @memo
74    Rectangle manipulation class.
75    @author
76    L\'eon Bottou <leonb@research.att.com> -- initial implementation.
77    @version
78    #$Id: GRect.h,v 1.9 2003/11/07 22:08:21 leonb Exp $# */
79//@{
80
81#include "DjVuGlobal.h"
82
83#ifdef HAVE_NAMESPACES
84namespace DJVU {
85# ifdef NOT_DEFINED // Just to fool emacs c++ mode
86}
87#endif
88#endif
89
90
91
92/** @name Point Coordinates vs. Pixel Coordinates
93
94    The DjVu technology relies on the accurate superposition of images at
95    different resolutions.  Such an accuracy cannot be reached with the usual
96    assumption that pixels are small enough to be considered infinitesimally
97    small.  We must distinguish very precisely ``points'' and ``pixels''.
98    This distinction is essential for performing scaling operations.
99
100    The pixels of an image are identified by ``pixel coordinates''.  The
101    bottom-left corner pixel has coordinates #(0,0)# and the top-right corner
102    pixel has coordinates #(w-1,h-1)# where #w# and #h# are the image size.
103    Pixel coordinates are necessarily integers since pixels never overlap.
104
105    An infinitesimally small point is identified by its ``point coordinates''.
106    There may be fractional point coordinates, although this library does not
107    make use of them.  Points with integer coordinates are located {\em on the
108    corners of each pixel}.  They are not located on the pixel centers.  The
109    center of the pixel with pixel coordinates #(i,j)# is located at point
110    coordinates #(i+1/2,j+1/2)#.  In other words, the pixel #(i,j)# extends
111    from point #(i,j)# to point #(i+1,j+1)#.
112
113    Therefore, the point located on the bottom left corner of an image has
114    coordinates #(0,0)#.  This point is in fact the bottom left corner of the
115    bottom left pixel of the image.  The point located on the top right corner
116    of an image has coordinates #(w,h)# where #w# and #h# are the image size.
117    This is in fact the top right corner of pixel #(w-1,h-1)# which is the
118    image pixel with the highest coordinates.
119*/
120//@{
121//@}
122
123
124
125/** Rectangle class.  Each instance of this class represents a rectangle whose
126    sides are parallel to the axis. Such a rectangle represents all the points
127    whose coordinates lies between well defined minimal and maximal values.
128    Member functions can combine several rectangles by computing the
129    intersection of rectangles (\Ref{intersect}) or the smallest rectangle
130    enclosing two rectangles (\Ref{recthull}).  */
131
132class GRect
133{
134public:
135  /** #OrientationBits# defines 3 mutually exclusive
136     bits to indicate the image orientation.
137
138     There are four possible rotation values for an image
139     which are 0 degrees, 90 degrees, 180 degrees, and 270 degrees.
140     In addition the image can be mirrored backwards in any of these
141     orientations, giving a possible of 8 orientations.  To sanely deal
142     with these orientations, we have defined 3 mutually exclusive
143     bits.  These are BOTTOM_UP, MIRROR, and ROTATE90_CW.
144  */
145  enum OrientationBits
146  {
147    BOTTOM_UP=0x1,  /* Upside down */
148    MIRROR=0x2,     /* Written backwards. (right to left) */
149    ROTATE90_CW=0x4 /* rotated 90 degrees */
150  };
151
152  /**  #Orientations# defines all 8 possible orientations, using
153   the three \Ref{OrientationBits}.
154   \begin{itemize}
155   \item {\em TDLRNR} for Top Down, Left to Right, No Rotation.
156   \item {\em BULRNR} for Bottom Up, Left to Right, No Rotation.
157   \item {\em TDRLNR} for Top Down, Right to Left, No Rotation.
158   \item {\em BURLNR} for Bottom Up, Right to Left, No Rotation.
159   \item {\em TDLRCW} for Top Down, Left to Right, 90 degree CW rotation.
160   \item {\em BULRCW} for Bottom Up, Left to Right, 90 degree CW rotation.
161   \item {\em TDRLCW} for Top Down, Right to Left, 90 degree CW rotation.
162   \item {\em BURLCW} for Bottom Up, Right to Left, 90 degree CW rotation.
163   \end{itemize}
164  */
165  enum Orientations
166  {
167    TDLRNR=0,                                     /* normal orientation */
168    BULRNR=BOTTOM_UP,                               /* upside down */
169    TDRLNR=MIRROR,                    /* backwards (right to left) */
170    BURLNR=MIRROR|BOTTOM_UP,                    /* rotate 180 */
171    TDLRCW=ROTATE90_CW,                              /* rotated 90 */
172    BULRCW=ROTATE90_CW|BOTTOM_UP, /* backwards and rotate 180 */
173    TDRLCW=ROTATE90_CW|MIRROR,     /* backwards and rotate 90 */
174    BURLCW=ROTATE90_CW|MIRROR|BOTTOM_UP    /* rotate 270 */
175  };
176
177  static Orientations
178  rotate(const int angle,Orientations orientation)
179  {
180    for(int a=(((angle)%360)+405)%360;a>90;a-=90)
181      orientation=(Orientations)((int)orientation^(int)(orientation&ROTATE90_CW)?BURLCW:TDLRCW);
182    return orientation;
183  }
184
185  static int
186  findangle(const Orientations orientation)
187  {
188    int a=270;
189    while(a&&(rotate(a,BURLNR)!=orientation)&&(rotate(a,TDRLNR)!=orientation))
190      a-=90;
191    return a;
192  }
193
194  /** Constructs an empty rectangle */
195  GRect();
196  /** Constructs a rectangle given its minimal coordinates #xmin# and #ymin#,
197      and its measurements #width# and #height#. Setting #width# or #height# to zero
198      produces an empty rectangle.  */
199  GRect(int xmin, int ymin, unsigned int width=0, unsigned int height=0);
200  /** Returns the rectangle width. */
201  int  width() const;
202  /** Returns the rectangle height. */
203  int  height() const;
204  /** Returns the area of the rectangle. */
205  int  area() const;
206  /** Returns true if the rectangle is empty. */
207  int  isempty() const;
208  /** Returns true if the rectangle contains pixel (#x#,#y#).  A rectangle
209      contains all pixels with horizontal pixel coordinates in range #xmin#
210      (inclusive) to #xmax# (exclusive) and vertical coordinates #ymin#
211      (inclusive) to #ymax# (exclusive). */
212  int  contains(int x, int y) const;
213  /** Returns true if this rectangle contains the passed rectangle #rect#.
214      The function basically checks, that the intersection of this rectangle
215      with #rect# is #rect#. */
216  int  contains(const GRect & rect) const;
217  /** Returns true if rectangles #r1# and #r2# are equal. */
218  friend int operator==(const GRect & r1, const GRect & r2);
219  /** Returns true if rectangles #r1# and #r2# are not equal. */
220  friend int operator!=(const GRect & r1, const GRect & r2);
221  /** Resets the rectangle to the empty rectangle */
222  void clear();
223  /** Fatten the rectangle. Both vertical sides of the rectangle are pushed
224      apart by #dx# units. Both horizontal sides of the rectangle are pushed
225      apart by #dy# units. Setting arguments #dx# (resp. #dy#) to a negative
226      value reduces the rectangle horizontal (resp. vertical) size. */
227  int  inflate(int dx, int dy);
228  /** Translate the rectangle. The new rectangle is composed of all the points
229      of the old rectangle translated by #dx# units horizontally and #dy#
230      units vertically. */
231  int  translate(int dx, int dy);
232  /** Sets the rectangle to the intersection of rectangles #rect1# and #rect2#.
233      This function returns true if the intersection rectangle is not empty. */
234  int  intersect(const GRect &rect1, const GRect &rect2);
235  /** Sets the rectangle to the smallest rectangle containing the points of
236      both rectangles #rect1# and #rect2#. This function returns true if the
237      created rectangle is not empty. */
238  int  recthull(const GRect &rect1, const GRect &rect2);
239  /** Multiplies xmin, ymin, xmax, ymax by factor and scales the rectangle*/
240  void scale(float factor);
241  /** Multiplies xmin, xmax by xfactor and ymin, ymax by yfactor and scales the rectangle*/
242  void scale(float xfactor, float yfactor);
243  /** Minimal horizontal point coordinate of the rectangle. */
244  int xmin;
245  /** Minimal vertical point coordinate of the rectangle. */
246  int ymin;
247  /** Maximal horizontal point coordinate of the rectangle. */
248  int xmax;
249  /** Maximal vertical point coordinate of the rectangle. */
250  int ymax;
251};
252
253
254/** Maps points from one rectangle to another rectangle.  This class
255    represents a relation between the points of two rectangles. Given the
256    coordinates of a point in the first rectangle (input rectangle), function
257    \Ref{map} computes the coordinates of the corresponding point in the
258    second rectangle (the output rectangle).  This function actually implements
259    an affine transform which maps the corners of the first rectangle onto the
260    matching corners of the second rectangle. The scaling operation is
261    performed using integer fraction arithmetic in order to maximize
262    accuracy. */
263class GRectMapper
264{
265public:
266  /** Constructs a rectangle mapper. */
267  GRectMapper();
268  /** Resets the rectangle mapper state. Both the input rectangle
269      and the output rectangle are marked as undefined. */
270  void clear();
271  /** Sets the input rectangle. */
272  void set_input(const GRect &rect);
273  /** Returns the input rectangle. */
274  GRect get_input();
275  /** Sets the output rectangle. */
276  void set_output(const GRect &rect);
277  /** Returns the output rectangle. */
278  GRect get_output();
279  /** Composes the affine transform with a rotation of #count# quarter turns
280      counter-clockwise.  This operation essentially is a modification of the
281      match between the corners of the input rectangle and the corners of the
282      output rectangle. */
283  void rotate(int count=1);
284  /** Composes the affine transform with a symmetry with respect to the
285      vertical line crossing the center of the output rectangle.  This
286      operation essentially is a modification of the match between the corners
287      of the input rectangle and the corners of the output rectangle. */
288  void mirrorx();
289  /** Composes the affine transform with a symmetry with respect to the
290      horizontal line crossing the center of the output rectangle.  This
291      operation essentially is a modification of the match between the corners
292      of the input rectangle and the corners of the output rectangle. */
293  void mirrory();
294  /** Maps a point according to the affine transform.  Variables #x# and #y#
295      initially contain the coordinates of a point. This operation overwrites
296      these variables with the coordinates of a second point located in the
297      same position relative to the corners of the output rectangle as the
298      first point relative to the matching corners of the input rectangle.
299      Coordinates are rounded to the nearest integer. */
300  void map(int &x, int &y);
301  /** Maps a rectangle according to the affine transform. This operation
302      consists in mapping the rectangle corners and reordering the corners in
303      the canonical rectangle representation.  Variable #rect# is overwritten
304      with the new rectangle coordinates. */
305  void map(GRect &rect);
306  /** Maps a point according to the inverse of the affine transform.
307      Variables #x# and #y# initially contain the coordinates of a point. This
308      operation overwrites these variables with the coordinates of a second
309      point located in the same position relative to the corners of input
310      rectangle as the first point relative to the matching corners of the
311      input rectangle. Coordinates are rounded to the nearest integer. */
312  void unmap(int &x, int &y);
313  /** Maps a rectangle according to the inverse of the affine transform. This
314      operation consists in mapping the rectangle corners and reordering the
315      corners in the canonical rectangle representation.  Variable #rect# is
316      overwritten with the new rectangle coordinates. */
317  void unmap(GRect &rect);
318private:
319  // GRatio
320  struct GRatio {
321    GRatio ();
322    GRatio (int p, int q);
323    int p;
324    int q;
325  };
326  // Data
327  GRect rectFrom;
328  GRect rectTo;
329  int   code;
330  // Helper
331  void  precalc();
332  friend int operator*(int n, GRatio r ); 
333  friend int operator/(int n, GRatio r ); 
334  GRatio rw;
335  GRatio rh;
336};
337
338
339//@}
340
341
342
343// ---- INLINES
344
345inline
346GRect::GRect()
347: xmin(0), ymin(0), xmax(0), ymax(0)
348{
349}
350
351inline 
352GRect::GRect(int xmin, int ymin, unsigned int width, unsigned int height)
353: xmin(xmin), ymin(ymin), xmax(xmin+width), ymax(ymin+height)
354{
355}
356
357inline int 
358GRect::width() const
359{
360  return xmax - xmin;
361}
362
363inline int 
364GRect::height() const
365{
366  return ymax - ymin;
367}
368
369inline int 
370GRect::isempty() const
371{
372  return (xmin>=xmax || ymin>=ymax);
373}
374
375inline int 
376GRect::area() const
377{
378  return isempty() ? 0 : (xmax-xmin)*(ymax-ymin);
379}
380
381inline int
382GRect::contains(int x, int y) const
383{
384  return (x>=xmin && x<xmax && y>=ymin && y<ymax);
385}
386 
387inline void 
388GRect::clear()
389{
390  xmin = xmax = ymin = ymax = 0;
391}
392
393inline int
394operator!=(const GRect & r1, const GRect & r2)
395{
396   return !(r1==r2);
397}
398
399// ---- THE END
400
401#ifdef HAVE_NAMESPACES
402}
403# ifndef NOT_USING_DJVU_NAMESPACE
404using namespace DJVU;
405# endif
406#endif
407#endif
Note: See TracBrowser for help on using the repository browser.