summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/image/image.cc
blob: cee6690135e42367c4cf7174ccab831408d26e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// image.cc
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Nicolas Schodet
//
// Robot APB Team/Efrei 2005.
//        Web: http://assos.efrei.fr/robot/
//      Email: robot AT efrei DOT fr
//
// This program 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; either version 2 of the License, or
// (at your option) any later version.
// 
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "image.hh"
#include "image_reader.hh"
#include "image_writer.hh"

#include <cstring>
#include <new>

/// Construit une image vide.
Image::Image (void)
    // �vite les tests pour toutes les op�rations en fabriquant un Rep vide.
    : rep_ (Rep::create (0, 0, rgb))
{
}

/// Construit une image aux pixels non initialis�e.
Image::Image (int width, int length, Image::PixelFormat pixelFormat)
    : rep_ (Rep::create (width, length, pixelFormat))
{
}

/// Construit une image directement � partir d'un ImageReader.
Image::Image (ImageReader &reader)
    : rep_ (0)
{
    read (reader);
}

/// Op�rateur d'affectation.
Image &
Image::operator= (const Image &rhs)
{
    rep_->release ();
    rep_ = rhs.rep_->copyRef ();
    return *this;
}

/// Lit une image.
void
Image::read (ImageReader &reader)
{
    int width, height;
    PixelFormat pixelFormat;
    // R�cup�re les param�tres.
    reader.getParam (width, height, pixelFormat);
    // R�alloue de la m�moire si n�cessaire.
    if (!rep_
	|| rep_->references_ != 0
	|| width != rep_->width_
	|| height != rep_->height_
	|| pixelFormat != rep_->pixelFormat_)
      {
	if (rep_)
	    rep_->release ();
	rep_ = Rep::create (width, height, pixelFormat);
      }
    // Charge l'image.
    reader.read (rep_->getBuf (), getBufSize ());
}

/// �crit une image.
void
Image::write (ImageWriter &writer) const
{
    writer.setParam (rep_->width_, rep_->height_, rep_->pixelFormat_);
    writer.write (rep_->getBuf (), getBufSize ());
}

/// Cr�e un Rep, avec de la place pour l'image.
Image::Rep *
Image::Rep::create (int width, int height, Image::PixelFormat pixelFormat)
{
    unsigned bpp = Image::getBytePerPixel (pixelFormat);
    unsigned bufSize = Image::getBufSize (width, height, pixelFormat);
    void *p = operator new[] (bufSize * sizeof (uint8_t) + sizeof (Rep));
    Rep *rep = new (p) Rep;
    rep->width_ = width;
    rep->height_ = height;
    rep->pixelFormat_ = pixelFormat;
    rep->bpp_ = bpp;
    rep->bufSize_ = bufSize;
    rep->references_ = 0;
    return rep;
}

/// Copie un Rep, par r�f�rence (ne fait qu'incr�menter le nombre de
/// r�f�rences).
Image::Rep *
Image::Rep::copyRef (void)
{
    references_++;
    return this;
}

/// Copie un Rep, vrai copie.
Image::Rep *
Image::Rep::copy (void)
{
    Rep *rep = Rep::create (width_, height_, pixelFormat_);
    memcpy (rep->getBuf (), getBuf (), bufSize_);
    // On peut d�cr�menter references_ car sa valeur a d�j� �t� test�e (sinon
    // on n'aurait pas �t� appel�.
    references_--;
    return rep;
}

/// Lib�re le Rep, detruit si necessaire.
void
Image::Rep::release (void)
{
    if (references_ == 0)
	destroy ();
    else
	references_--;
}

/// D�truit le Rep.
void
Image::Rep::destroy (void)
{
    this->~Rep ();
    operator delete[] (this);
}