~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to Marker.cc

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
    Xplanet 0.94 - render an image of a planet into an X window
3
 
    Copyright (C) 2002 Hari Nair <hari@alumni.caltech.edu>
4
 
 
5
 
    This program is free software; you can redistribute it and/or modify
6
 
    it under the terms of the GNU General Public License as published by
7
 
    the Free Software Foundation; either version 2 of the License, or
8
 
    (at your option) any later version.
9
 
 
10
 
    This program is distributed in the hope that it will be useful,
11
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
    GNU General Public License for more details.
14
 
 
15
 
    You should have received a copy of the GNU General Public License
16
 
    along with this program; if not, write to the Free Software
17
 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
****************************************************************************/
19
 
 
20
 
#include <algorithm>
21
 
#include <cstdlib>
22
 
#include <iostream>
23
 
#include <string>
24
 
using namespace std;
25
 
 
26
 
#include "Marker.h"
27
 
#include "Options.h"
28
 
#include "util.h"
29
 
 
30
 
static int num_markers = 0;
31
 
 
32
 
Marker::Marker(const int X, const int Y,
33
 
               const string Text, const int Align)
34
 
{
35
 
    ID = num_markers++;
36
 
 
37
 
    x = X;
38
 
    y = Y;
39
 
    text.assign(Text);
40
 
    align = Align;
41
 
 
42
 
    image_width = -1;
43
 
    image_height = -1;
44
 
    image_data = NULL;
45
 
 
46
 
    color = "";
47
 
    font = opts.font;
48
 
    fontsize = opts.fontsize;
49
 
 
50
 
    transparency = false;
51
 
 
52
 
    if (align == AUTO)
53
 
    {
54
 
        fixed_align = false;
55
 
        align = RIGHT;
56
 
    }
57
 
    else
58
 
    {
59
 
        fixed_align = true;
60
 
    }
61
 
}
62
 
 
63
 
Marker::~Marker()
64
 
{
65
 
}
66
 
 
67
 
bool
68
 
Marker::operator < (const Marker &m)
69
 
{
70
 
    return(x < m.x);
71
 
}
72
 
 
73
 
bool
74
 
Marker::operator > (const Marker &m)
75
 
{
76
 
    return(x > m.x);
77
 
}
78
 
 
79
 
bool
80
 
Marker::operator == (const Marker &m)
81
 
{
82
 
    return(x == m.x);
83
 
}
84
 
 
85
 
bool
86
 
Marker::operator != (const Marker &m)
87
 
{
88
 
    return(x != m.x);
89
 
}
90
 
 
91
 
void
92
 
Marker::setImage(const int width, const int height)
93
 
{
94
 
    image_width = width;
95
 
    image_height = height;
96
 
    image.left = x - width/2;
97
 
    image.right = x + width/2;
98
 
    image.top = y - height/2;
99
 
    image.bottom = y + height/2;
100
 
}
101
 
 
102
 
void
103
 
Marker::setImage(string filename)
104
 
{
105
 
    if (readImageFile(filename, image_width, image_height, image_data))
106
 
    {
107
 
        image.left = x - image_width/2;
108
 
        image.right = x + image_width/2;
109
 
        image.top = y - image_height/2;
110
 
        image.bottom = y + image_height/2;
111
 
    }
112
 
    else
113
 
    {
114
 
        cerr << "Can't read image " << filename << endl;
115
 
        setImage(0, 0);
116
 
    }
117
 
}
118
 
 
119
 
void
120
 
Marker::setCorners(const int x1, const int y1, 
121
 
                   const int x2, const int y2)
122
 
{
123
 
    label.left = min(x1, x2);
124
 
    label.right = max(x1, x2);
125
 
    label.top = min(y1, y2);
126
 
    label.bottom = max(y1, y2);
127
 
}
128
 
 
129
 
int
130
 
Marker::overlap(const Marker &m)
131
 
{
132
 
    return(overlap(m.image) + overlap(m.label));
133
 
}
134
 
 
135
 
int
136
 
Marker::overlap(const BoundingBox &b)
137
 
{
138
 
    if (label.left > b.right || label.right < b.left) return(0);
139
 
 
140
 
    if (label.top > b.bottom || label.bottom < b.top) return(0);
141
 
 
142
 
    int width, height;
143
 
    if (label.left > b.left)
144
 
        width = min(b.right, label.right) - label.left;
145
 
    else
146
 
        width = min(b.right, label.right) - max(b.left, label.left);
147
 
 
148
 
    if (label.top > b.top)
149
 
        height = min(b.bottom, label.bottom) - label.top;
150
 
    else
151
 
        height = min(b.bottom, label.bottom) - max(b.top, label.top);
152
 
 
153
 
    return(width * height);
154
 
}
155
 
 
156
 
void
157
 
Marker::printCorners(ofstream &outfile)
158
 
{
159
 
    outfile << min(image.left, label.left) << ","
160
 
            << min(image.top, label.top) << " " 
161
 
            << max(image.right, label.right) << ","
162
 
            << max(image.bottom, label.bottom) << "\t" << text << endl;
163
 
}
164
 
 
165
 
void 
166
 
Marker::setTransparency(unsigned char *pixel)
167
 
{
168
 
    transparency = true;
169
 
    memcpy(transparent, pixel, 3);
170
 
}
171
 
 
172
 
bool
173
 
Marker::isTransparent(const int pixel_x, const int pixel_y)
174
 
{
175
 
    if (!transparency || image_data == NULL) return(false);
176
 
    
177
 
    unsigned char *pixel = (image_data 
178
 
                            + 3 * (pixel_y * image_width + pixel_x));
179
 
 
180
 
    for (int i = 0; i < 3; i++)
181
 
        if (pixel[i] != transparent[i]) return(false);
182
 
 
183
 
    return(true);
184
 
}