~ubuntu-branches/ubuntu/hardy/avidemux/hardy

« back to all changes in this revision

Viewing changes to avidemux/ADM_videoFilter/ADM_vidParticle.h

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2007-12-18 13:53:04 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20071218135304-cdqec2lg2bglyz15
Tags: 1:2.4~preview3-0.0ubuntu1
* Upload to Ubuntu. (LP: #163287, LP: #126572)
* debian/changelog: re-added Ubuntu releases.
* debian/control:
  - Require debhelper >= 5.0.51 (for dh_icons) and imagemagick.
  - Build-depend on libsdl1.2-dev instead of libsdl-dev.
  - Build against newer libx264-dev. (LP: #138854)
  - Removed libamrnb-dev, not in Ubuntu yet.
* debian/rules:
  - Install all icon sizes, using convert (upstream installs none).
  - Added missing calls to dh_installmenu, dh_installman, dh_icons and
    dh_desktop.
* debian/menu, debian/avidemux-qt.menu:
  - Corrected package and executable names.
* debian/avidemux-common.install: Install icons.
* debian/avidemux.common.manpages: Install man/avidemux.1.
* debian/links, debian/avidemux-cli.links, debian/avidemux-gtk.links:
  - Link manpages to avidemux.1.gz.
* debian/install, debian/avidemux-qt.install, debian/avidemux-gtk.desktop,
  debian/avidemux-qt.desktop: Install desktop files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          ADM_vidParticle.h  -  detect particles (groups of pixels)
 
3
                              -------------------
 
4
                          Chris MacGregor, 2005, 2007
 
5
                         chris-avidemux@bouncingdog.com
 
6
 ***************************************************************************/
 
7
 
 
8
/***************************************************************************
 
9
 *                                                                         *
 
10
 *   This program is free software; you can redistribute it and/or modify  *
 
11
 *   it under the terms of the GNU General Public License as published by  *
 
12
 *   the Free Software Foundation; either version 2 of the License, or     *
 
13
 *   (at your option) any later version.                                   *
 
14
 *                                                                         *
 
15
 ***************************************************************************/
 
16
 
 
17
#ifndef __PARTICLE__
 
18
#define __PARTICLE__   
 
19
 
 
20
#include <string>
 
21
 
 
22
#include "ADM_video/ADM_cache.h"
 
23
 
 
24
struct PARTICLE_PARAM
 
25
{
 
26
    uint32_t min_area;
 
27
    uint32_t max_area;
 
28
    uint32_t left_crop;
 
29
    uint32_t right_crop;
 
30
    uint32_t top_crop;
 
31
    uint32_t bottom_crop;
 
32
    uint32_t output_format;
 
33
    std::string output_file;
 
34
    uint32_t camera_number;
 
35
    uint32_t debug;
 
36
};
 
37
 
 
38
// Alas, because offsetof() is only supposed to work on POD (plain old data)
 
39
// structs, and our PARTICLE_PARAM includes a std::string (which has a
 
40
// constructor, and which causes PARTICLE_PARAM to therefore have an implicit
 
41
// constructor), we need to define our own offsetof() to use for the dialog
 
42
// menus.  See
 
43
// http://www.cplusplus.com/reference/clibrary/cstddef/offsetof.html for more
 
44
// on offsetof().
 
45
 
 
46
#define my_offsetof(_type, _memb) (size_t (&(((_type *)1)->_memb)) - 1)
 
47
 
 
48
class ADMVideoParticle : public AVDMGenericVideoStream
 
49
{
 
50
 
 
51
 protected:
 
52
        
 
53
     PARTICLE_PARAM *  _param;
 
54
 
 
55
     static FILE * outfp; // ugly, but hard to avoid
 
56
     static uint32_t last_frame_written; // ditto
 
57
 
 
58
 public:
 
59
                
 
60
     enum OutputFmt
 
61
     {
 
62
         OUTPUTFMT_INVALID = 0,
 
63
 
 
64
         OUTPUTFMT_FORMAT_NEW,
 
65
         OUTPUTFMT_FORMAT_OLD,
 
66
 
 
67
         OUTPUTFMT_COUNT
 
68
     };
 
69
 
 
70
 
 
71
     ADMVideoParticle (AVDMGenericVideoStream *in, CONFcouple *setup);
 
72
 
 
73
     ~ADMVideoParticle();
 
74
 
 
75
     virtual uint8_t getFrameNumberNoAlloc (uint32_t frame, uint32_t *len,
 
76
                                            ADMImage *data,uint32_t *flags);
 
77
 
 
78
     virtual uint8_t configure (AVDMGenericVideoStream *instream);
 
79
     virtual char * printConf (void);
 
80
     virtual uint8_t getCoupledConf (CONFcouple **couples);
 
81
                                                        
 
82
     static uint8_t doParticle (ADMImage * image, ADMImage * data,
 
83
                                AVDMGenericVideoStream * in,
 
84
                                uint32_t real_frame,
 
85
                                FILE * do_outfp, PARTICLE_PARAM * param,
 
86
                                uint32_t width, uint32_t height);
 
87
};
 
88
 
 
89
//////////////////////////////////////////////////////////////////////////////
 
90
 
 
91
#include <list>
 
92
#include <vector>
 
93
#include <iostream>
 
94
#include <iterator>
 
95
 
 
96
class PixelLoc
 
97
{
 
98
public:
 
99
    uint16_t x;
 
100
    uint16_t y;
 
101
 
 
102
    PixelLoc ()
 
103
        : x (0),
 
104
          y (0)
 
105
    {
 
106
    }
 
107
 
 
108
    PixelLoc (uint32_t a_x, uint32_t a_y)
 
109
        : x (a_x),
 
110
          y (a_y)
 
111
    {
 
112
    }
 
113
 
 
114
    // Use this to sort by y's, then (for PixelLoc's with the same y) by x's.
 
115
 
 
116
    class Compare
 
117
    {
 
118
    public:
 
119
        int operator () (const PixelLoc & p1, const PixelLoc & p2) const
 
120
        {
 
121
            return ((p1.y == p2.y) ? (p1.x < p2.x) : (p1.y < p2.y));
 
122
        }
 
123
    };
 
124
};
 
125
 
 
126
// Now that we no longer need to sort the list or insert anything into it, a
 
127
// vector should be faster.
 
128
// typedef std::list <PixelLoc> PixelList;
 
129
typedef std::vector <PixelLoc> PixelList;
 
130
 
 
131
inline std::ostream &
 
132
operator << (std::ostream & s, const PixelLoc & pl)
 
133
{
 
134
    return s << "(" << pl.x << "," << pl.y << ")";
 
135
}
 
136
 
 
137
inline std::ostream &
 
138
operator << (std::ostream & s, const PixelList & theList)
 
139
{
 
140
    s << "[ ";
 
141
    std::copy (theList.begin(), theList.end(),
 
142
               std::ostream_iterator <PixelLoc> (s, " "));
 
143
    s << "]";
 
144
    return s;
 
145
}
 
146
 
 
147
struct PixelOffset
 
148
{
 
149
    int8_t x;
 
150
    int8_t y;
 
151
};
 
152
 
 
153
class ImageTool
 
154
{
 
155
private:
 
156
    uint8_t * my_pixels;
 
157
    uint32_t my_w;
 
158
    uint32_t my_h;
 
159
    uint32_t my_left_margin;
 
160
    uint32_t my_right_margin;
 
161
    uint32_t my_top_margin;
 
162
    uint32_t my_bottom_margin;
 
163
    ADMImage * my_outImage;
 
164
    uint32_t my_minArea;
 
165
    uint32_t my_maxArea;
 
166
    uint32_t my_particleArea;
 
167
    float my_particleCentroidX;
 
168
    float my_particleCentroidY;
 
169
    uint32_t debug;
 
170
    PixelList pixelList;
 
171
 
 
172
    enum TracingDirection
 
173
    {
 
174
        // The order of the members here is important - several things depend
 
175
        // on it!
 
176
 
 
177
        DIRECTION_EAST,   // x + 1, y
 
178
        DIRECTION_SOUTH,  // x    , y + 1
 
179
        DIRECTION_WEST,   // x - 1, y
 
180
        DIRECTION_NORTH,  // x    , y - 1
 
181
        DIRECTION_COUNT
 
182
    };
 
183
 
 
184
    enum TurnDirection
 
185
    {
 
186
        // These values are designed to be added to TracingDirection values.
 
187
        TURN_RIGHT = 1,
 
188
        TURN_AROUND = 2, // reverse
 
189
        TURN_LEFT = 3,
 
190
    };
 
191
 
 
192
    static char directionLetters [];
 
193
    static PixelOffset leftOffset [DIRECTION_COUNT];
 
194
    static PixelOffset aheadOffset [DIRECTION_COUNT];
 
195
 
 
196
public:
 
197
    ImageTool (uint8_t * pixels, uint32_t w, uint32_t h, ADMImage * outImage = 0)
 
198
        : my_pixels (pixels),
 
199
          my_w (w),
 
200
          my_h (h),
 
201
          my_left_margin (0),
 
202
          my_right_margin (w),
 
203
          my_top_margin (0),
 
204
          my_bottom_margin (h),
 
205
          my_outImage (outImage),
 
206
          my_particleArea (0),
 
207
          my_particleCentroidX (0),
 
208
          my_particleCentroidY (0),
 
209
          debug (0)
 
210
    {
 
211
    }
 
212
 
 
213
    void setCropping (uint32_t left_crop, uint32_t right_crop,
 
214
                      uint32_t top_crop, uint32_t bottom_crop)
 
215
    {
 
216
        my_left_margin = left_crop;
 
217
        my_right_margin = my_w - right_crop;
 
218
        my_top_margin = top_crop;
 
219
        my_bottom_margin = my_h - bottom_crop;
 
220
    }
 
221
 
 
222
    // Return true if the pixel is part of a particle.
 
223
 
 
224
    bool goodPixel (uint8_t pixel) const
 
225
    {
 
226
        // HERE: If we wanted to incorporate thresholding into this step, or look
 
227
        // for black particles on a white background, this function would be the
 
228
        // place to make the change.
 
229
 
 
230
        return (pixel != 0);
 
231
    }
 
232
 
 
233
    uint8_t & getPixel (uint32_t x, uint32_t y) const
 
234
    {
 
235
        return my_pixels [(y * my_w) + x];
 
236
    }
 
237
 
 
238
    uint8_t & getPixel (uint32_t index) const
 
239
    {
 
240
        return my_pixels [index];
 
241
    }
 
242
 
 
243
    // This one does bounds checking, and forces references "off the edge" to
 
244
    // the nearest valid pixel - it's useful for convolutions.  It does not
 
245
    // currently respect the crop settings, only because we know that this
 
246
    // function is used only when those aren't set.
 
247
 
 
248
    uint8_t & getPixelSafely (int32_t x, int32_t y) const
 
249
    {
 
250
        if (x < 0)
 
251
            x = 0;
 
252
        else if (x >= my_w)
 
253
            x = my_w - 1;
 
254
 
 
255
        if (y < 0)
 
256
            y = 0;
 
257
        else if (y >= my_h)
 
258
            y = my_h - 1;
 
259
 
 
260
        return my_pixels [(y * my_w) + x];
 
261
    }
 
262
 
 
263
    uint8_t & getPixelSafely (uint32_t x, uint32_t y) const
 
264
    {
 
265
        return getPixelSafely (static_cast <int32_t> (x),
 
266
                               static_cast <int32_t> (y));
 
267
    }
 
268
 
 
269
    uint8_t & outPixel (uint32_t x, uint32_t y) const
 
270
    {
 
271
        return YPLANE (my_outImage) [(y * my_w) + x];
 
272
    }
 
273
 
 
274
    uint8_t & outUPixel (uint32_t x, uint32_t y) const
 
275
    {
 
276
        return UPLANE (my_outImage) [((y >> 2) * my_w) + (x >> 1)];
 
277
    }
 
278
 
 
279
    uint8_t & outVPixel (uint32_t x, uint32_t y) const
 
280
    {
 
281
        return VPLANE (my_outImage) [((y >> 2) * my_w) + (x >> 1)];
 
282
    }
 
283
 
 
284
    bool validPixel (uint32_t x, uint32_t y) const
 
285
    {
 
286
        // return (x < my_w && y < my_h);
 
287
        return (x >= my_left_margin && x < my_right_margin
 
288
                && y >= my_top_margin && y < my_bottom_margin);
 
289
    }
 
290
 
 
291
    bool goodPixel (uint32_t x, uint32_t y) const
 
292
    {
 
293
        // HERE: this might be a good place to implement ROI (region of
 
294
        // interest) if we want that.
 
295
 
 
296
        return (validPixel (x, y) && goodPixel (getPixel (x, y)));
 
297
    }
 
298
 
 
299
    uint32_t width () const
 
300
    {
 
301
        return my_w;
 
302
    }
 
303
 
 
304
    uint32_t height () const
 
305
    {
 
306
        return my_h;
 
307
    }
 
308
 
 
309
    uint32_t particleArea () const
 
310
    {
 
311
        return my_particleArea;
 
312
    }
 
313
 
 
314
    float particleCentroidX () const
 
315
    {
 
316
        return my_particleCentroidX;
 
317
    }
 
318
 
 
319
    float particleCentroidY () const
 
320
    {
 
321
        return my_particleCentroidY;
 
322
    }
 
323
 
 
324
    uint8_t autoOutline (uint32_t x, uint32_t y);
 
325
 
 
326
    void showStuff () const;
 
327
 
 
328
    // implementation is in ADM_vidSwissArmyKnife.cpp - nothing else uses it
 
329
    // (currently).
 
330
 
 
331
    template <class Oper, class Histo>
 
332
    void convolve (const std::vector <float> & kernel,
 
333
                   uint32_t kw, uint32_t kh, int32_t bias, const Oper & op,
 
334
                   const Histo & histogram_in);
 
335
 
 
336
    void setDebug (uint32_t newDebug)
 
337
    {
 
338
        debug = newDebug;
 
339
    }
 
340
 
 
341
    void setMinArea (uint32_t newMinArea)
 
342
    {
 
343
        my_minArea = newMinArea;
 
344
    }
 
345
 
 
346
    void setMaxArea (uint32_t newMaxArea)
 
347
    {
 
348
        my_maxArea = newMaxArea;
 
349
    }
 
350
};
 
351
 
 
352
struct MenuMapping;
 
353
uint8_t DIA_particle (AVDMGenericVideoStream *in,
 
354
                      PARTICLE_PARAM * param,
 
355
                      const MenuMapping * menu_mapping,
 
356
                      uint32_t menu_mapping_count);
 
357
 
 
358
#endif