2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder Image Class Interface, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ZM_IMAGE_H
|
|
|
|
#define ZM_IMAGE_H
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
#include "zm.h"
|
2003-03-26 19:57:29 +08:00
|
|
|
extern "C"
|
|
|
|
{
|
2004-01-15 05:26:47 +08:00
|
|
|
#include "zm_jpeg.h"
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
#include "zm_rgb.h"
|
|
|
|
#include "zm_coord.h"
|
|
|
|
#include "zm_box.h"
|
2005-11-30 01:51:06 +08:00
|
|
|
#include "zm_poly.h"
|
2011-05-01 02:39:34 +08:00
|
|
|
#include "zm_mem_utils.h"
|
|
|
|
#include "zm_utils.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2009-03-20 20:39:29 +08:00
|
|
|
#if HAVE_ZLIB_H
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif // HAVE_ZLIB_H
|
|
|
|
|
2011-04-29 22:26:50 +08:00
|
|
|
#define ZM_BUFTYPE_DONTFREE 0
|
|
|
|
#define ZM_BUFTYPE_MALLOC 1
|
|
|
|
#define ZM_BUFTYPE_NEW 2
|
|
|
|
#define ZM_BUFTYPE_AVMALLOC 3
|
|
|
|
#define ZM_BUFTYPE_ZM 4
|
|
|
|
|
2011-06-15 01:38:18 +08:00
|
|
|
typedef void (*blend_fptr_t)(const uint8_t*, const uint8_t*, uint8_t*, unsigned long, double);
|
|
|
|
typedef void (*delta_fptr_t)(const uint8_t*, const uint8_t*, uint8_t*, unsigned long);
|
|
|
|
typedef void (*convert_fptr_t)(const uint8_t*, uint8_t*, unsigned long);
|
|
|
|
typedef void* (*imgbufcpy_fptr_t)(void*, const void*, size_t);
|
|
|
|
|
|
|
|
|
|
|
|
extern imgbufcpy_fptr_t fptr_imgbufcpy;
|
2011-05-06 20:01:16 +08:00
|
|
|
|
|
|
|
/* Should be called from Image class functions */
|
|
|
|
inline static uint8_t* AllocBuffer(size_t p_bufsize) {
|
|
|
|
uint8_t* buffer = (uint8_t*)zm_mallocaligned(16,p_bufsize);
|
|
|
|
if(buffer == NULL)
|
|
|
|
Fatal("Memory allocation failed: %s",strerror(errno));
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static void DumpBuffer(uint8_t* buffer, int buffertype) {
|
|
|
|
if (buffer && buffertype != ZM_BUFTYPE_DONTFREE) {
|
|
|
|
if(buffertype == ZM_BUFTYPE_ZM)
|
|
|
|
zm_freealigned(buffer);
|
|
|
|
else if(buffertype == ZM_BUFTYPE_MALLOC)
|
|
|
|
free(buffer);
|
|
|
|
else if(buffertype == ZM_BUFTYPE_NEW)
|
|
|
|
delete buffer;
|
|
|
|
/*else if(buffertype == ZM_BUFTYPE_AVMALLOC)
|
|
|
|
av_free(buffer);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 01:38:18 +08:00
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// This is image class, and represents a frame captured from a
|
|
|
|
// camera in raw form.
|
|
|
|
//
|
|
|
|
class Image
|
|
|
|
{
|
|
|
|
protected:
|
2004-02-16 03:53:10 +08:00
|
|
|
|
2005-11-30 01:51:06 +08:00
|
|
|
struct Edge
|
|
|
|
{
|
|
|
|
int min_y;
|
|
|
|
int max_y;
|
|
|
|
double min_x;
|
|
|
|
double _1_m;
|
|
|
|
|
|
|
|
static int CompareYX( const void *p1, const void *p2 )
|
|
|
|
{
|
|
|
|
const Edge *e1 = (const Edge *)p1, *e2 = (const Edge *)p2;
|
|
|
|
if ( e1->min_y == e2->min_y )
|
|
|
|
return( int(e1->min_x - e2->min_x) );
|
|
|
|
else
|
|
|
|
return( int(e1->min_y - e2->min_y) );
|
|
|
|
}
|
|
|
|
static int CompareX( const void *p1, const void *p2 )
|
|
|
|
{
|
|
|
|
const Edge *e1 = (const Edge *)p1, *e2 = (const Edge *)p2;
|
|
|
|
return( int(e1->min_x - e2->min_x) );
|
|
|
|
}
|
|
|
|
};
|
2011-04-29 22:26:50 +08:00
|
|
|
|
2011-05-05 17:52:00 +08:00
|
|
|
inline void DumpImgBuffer() {
|
|
|
|
DumpBuffer(buffer,buffertype);
|
2011-04-29 22:26:50 +08:00
|
|
|
buffer = NULL;
|
|
|
|
allocation = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-05 17:52:00 +08:00
|
|
|
inline void AllocImgBuffer(size_t p_bufsize) {
|
2011-04-29 22:26:50 +08:00
|
|
|
if(buffer)
|
2011-05-05 17:52:00 +08:00
|
|
|
DumpImgBuffer();
|
2011-04-29 22:26:50 +08:00
|
|
|
|
2011-05-05 17:52:00 +08:00
|
|
|
buffer = AllocBuffer(p_bufsize);
|
2011-04-29 22:26:50 +08:00
|
|
|
buffertype = ZM_BUFTYPE_ZM;
|
|
|
|
allocation = p_bufsize;
|
|
|
|
}
|
2005-11-30 01:51:06 +08:00
|
|
|
|
2009-10-14 18:01:46 +08:00
|
|
|
public:
|
|
|
|
enum { CHAR_HEIGHT=11, CHAR_WIDTH=6 };
|
2011-04-29 22:26:50 +08:00
|
|
|
enum { LINE_HEIGHT=CHAR_HEIGHT+0 };
|
2009-10-14 18:01:46 +08:00
|
|
|
|
2004-02-16 03:53:10 +08:00
|
|
|
protected:
|
|
|
|
static bool initialised;
|
|
|
|
static unsigned char *abs_table;
|
|
|
|
static unsigned char *y_r_table;
|
|
|
|
static unsigned char *y_g_table;
|
|
|
|
static unsigned char *y_b_table;
|
2012-02-09 19:10:48 +08:00
|
|
|
static jpeg_compress_struct *jpg_ccinfo[101];
|
2005-11-22 18:41:50 +08:00
|
|
|
static jpeg_decompress_struct *jpg_dcinfo;
|
|
|
|
static struct zm_error_mgr jpg_err;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
|
|
|
protected:
|
2011-04-29 22:26:50 +08:00
|
|
|
int width;
|
2003-03-26 19:57:29 +08:00
|
|
|
int height;
|
2006-01-15 06:47:02 +08:00
|
|
|
int pixels;
|
2003-03-26 19:57:29 +08:00
|
|
|
int colours;
|
|
|
|
int size;
|
2011-04-29 22:26:50 +08:00
|
|
|
int subpixelorder;
|
|
|
|
unsigned long allocation;
|
2011-02-15 19:18:41 +08:00
|
|
|
uint8_t *buffer;
|
2011-04-29 22:26:50 +08:00
|
|
|
int buffertype; /* 0=not ours, no need to call free(), 1=malloc() buffer, 2=new buffer */
|
|
|
|
int holdbuffer; /* Hold the buffer instead of replacing it with new one */
|
2006-11-16 19:34:53 +08:00
|
|
|
char text[1024];
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2004-02-16 03:53:10 +08:00
|
|
|
protected:
|
|
|
|
static void Initialise();
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
public:
|
2007-08-30 02:11:09 +08:00
|
|
|
Image();
|
|
|
|
Image( const char *filename );
|
2011-04-29 22:26:50 +08:00
|
|
|
Image( int p_width, int p_height, int p_colours, int p_subpixelorder, uint8_t *p_buffer=0);
|
2007-08-30 02:11:09 +08:00
|
|
|
Image( const Image &p_image );
|
|
|
|
~Image();
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2004-03-04 23:05:54 +08:00
|
|
|
inline int Width() const { return( width ); }
|
|
|
|
inline int Height() const { return( height ); }
|
2006-01-15 06:47:02 +08:00
|
|
|
inline int Pixels() const { return( pixels ); }
|
2004-03-04 23:05:54 +08:00
|
|
|
inline int Colours() const { return( colours ); }
|
2011-05-01 02:39:34 +08:00
|
|
|
inline int SubpixelOrder() const { return( subpixelorder ); }
|
2006-01-15 06:47:02 +08:00
|
|
|
inline int Size() const { return( size ); }
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2011-04-29 22:26:50 +08:00
|
|
|
/* Internal buffer should not be modified from functions outside of this class */
|
|
|
|
inline const uint8_t* Buffer() const { return( buffer ); }
|
|
|
|
inline const uint8_t* Buffer( unsigned int x, unsigned int y= 0 ) const { return( &buffer[colours*((y*width)+x)] ); }
|
|
|
|
/* Request writeable buffer */
|
|
|
|
uint8_t* WriteBuffer(const int p_width, const int p_height, const int p_colours, const int p_subpixelorder);
|
|
|
|
|
2011-05-05 17:52:00 +08:00
|
|
|
inline int IsBufferHeld() const { return holdbuffer; }
|
|
|
|
inline void HoldBuffer(int tohold) { holdbuffer = tohold; }
|
2011-04-29 22:26:50 +08:00
|
|
|
|
|
|
|
inline void Empty() {
|
|
|
|
if(!holdbuffer)
|
2011-05-05 17:52:00 +08:00
|
|
|
DumpImgBuffer();
|
2011-04-29 22:26:50 +08:00
|
|
|
|
|
|
|
width = height = colours = size = pixels = subpixelorder = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-01 02:39:34 +08:00
|
|
|
void Assign( int p_width, int p_height, int p_colours, int p_subpixelorder, const uint8_t* new_buffer, const size_t buffer_size);
|
2007-08-30 02:11:09 +08:00
|
|
|
void Assign( const Image &image );
|
2011-05-01 02:39:34 +08:00
|
|
|
void AssignDirect( const int p_width, const int p_height, const int p_colours, const int p_subpixelorder, uint8_t *new_buffer, const size_t buffer_size, const int p_buffertype);
|
2003-03-26 19:57:29 +08:00
|
|
|
|
|
|
|
inline void CopyBuffer( const Image &image )
|
|
|
|
{
|
2006-05-08 20:46:53 +08:00
|
|
|
if ( image.size != size )
|
|
|
|
{
|
2011-06-15 01:38:18 +08:00
|
|
|
Panic( "Attempt to copy different size image buffers, expected %d, got %d", size, image.size );
|
2006-05-08 20:46:53 +08:00
|
|
|
}
|
2011-06-15 01:38:18 +08:00
|
|
|
(*fptr_imgbufcpy)(buffer, image.buffer, size);
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
inline Image &operator=( const unsigned char *new_buffer )
|
|
|
|
{
|
2011-06-15 01:38:18 +08:00
|
|
|
(*fptr_imgbufcpy)(buffer, new_buffer, size);
|
2003-03-26 19:57:29 +08:00
|
|
|
return( *this );
|
|
|
|
}
|
|
|
|
|
2005-11-30 01:51:06 +08:00
|
|
|
bool ReadRaw( const char *filename );
|
|
|
|
bool WriteRaw( const char *filename ) const;
|
|
|
|
|
2011-04-29 22:26:50 +08:00
|
|
|
bool ReadJpeg( const char *filename, int p_colours, int p_subpixelorder);
|
2005-11-19 01:26:12 +08:00
|
|
|
bool WriteJpeg( const char *filename, int quality_override=0 ) const;
|
2011-04-29 22:26:50 +08:00
|
|
|
bool DecodeJpeg( const JOCTET *inbuffer, int inbuffer_size, int p_colours, int p_subpixelorder);
|
2005-11-19 01:26:12 +08:00
|
|
|
bool EncodeJpeg( JOCTET *outbuffer, int *outbuffer_size, int quality_override=0 ) const;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2009-03-20 20:39:29 +08:00
|
|
|
#if HAVE_ZLIB_H
|
2006-01-20 23:27:48 +08:00
|
|
|
bool Unzip( const Bytef *inbuffer, unsigned long inbuffer_size );
|
|
|
|
bool Zip( Bytef *outbuffer, unsigned long *outbuffer_size, int compression_level=Z_BEST_SPEED ) const;
|
2009-03-20 20:39:29 +08:00
|
|
|
#endif // HAVE_ZLIB_H
|
2006-01-20 23:27:48 +08:00
|
|
|
|
2008-02-14 06:44:19 +08:00
|
|
|
bool Crop( int lo_x, int lo_y, int hi_x, int hi_y );
|
2007-08-30 02:11:09 +08:00
|
|
|
bool Crop( const Box &limits );
|
2005-12-02 00:24:25 +08:00
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
void Overlay( const Image &image );
|
2007-08-30 02:11:09 +08:00
|
|
|
void Overlay( const Image &image, int x, int y );
|
2011-06-16 00:04:29 +08:00
|
|
|
void Blend( const Image &image, int transparency=12 );
|
2003-03-26 19:57:29 +08:00
|
|
|
static Image *Merge( int n_images, Image *images[] );
|
|
|
|
static Image *Merge( int n_images, Image *images[], double weight );
|
|
|
|
static Image *Highlight( int n_images, Image *images[], const Rgb threshold=RGB_BLACK, const Rgb ref_colour=RGB_RED );
|
2011-04-29 22:26:50 +08:00
|
|
|
//Image *Delta( const Image &image ) const;
|
2011-05-01 02:39:34 +08:00
|
|
|
void Delta( const Image &image, Image* targetimage) const;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2011-04-29 22:26:50 +08:00
|
|
|
const Coord centreCoord( const char *text ) const;
|
2006-11-16 19:34:53 +08:00
|
|
|
void Annotate( const char *p_text, const Coord &coord, const Rgb fg_colour=RGB_WHITE, const Rgb bg_colour=RGB_BLACK );
|
2011-10-27 23:22:36 +08:00
|
|
|
Image *HighlightEdges( Rgb colour, int p_colours, int p_subpixelorder, const Box *limits=0 );
|
2005-12-21 08:13:23 +08:00
|
|
|
//Image *HighlightEdges( Rgb colour, const Polygon &polygon );
|
2003-03-26 19:57:29 +08:00
|
|
|
void Timestamp( const char *label, const time_t when, const Coord &coord );
|
2011-05-01 02:39:34 +08:00
|
|
|
void Colourise(const int p_reqcolours, const int p_reqsubpixelorder);
|
2003-03-26 19:57:29 +08:00
|
|
|
void DeColourise();
|
|
|
|
|
|
|
|
void Clear() { memset( buffer, 0, size ); }
|
|
|
|
void Fill( Rgb colour, const Box *limits=0 );
|
2005-11-30 01:51:06 +08:00
|
|
|
void Fill( Rgb colour, int density, const Box *limits=0 );
|
|
|
|
void Outline( Rgb colour, const Polygon &polygon );
|
|
|
|
void Fill( Rgb colour, const Polygon &polygon );
|
|
|
|
void Fill( Rgb colour, int density, const Polygon &polygon );
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2003-05-02 23:03:16 +08:00
|
|
|
void Rotate( int angle );
|
2005-05-06 00:42:37 +08:00
|
|
|
void Flip( bool leftright );
|
2004-02-16 03:47:23 +08:00
|
|
|
void Scale( unsigned int factor );
|
2012-01-25 22:59:56 +08:00
|
|
|
void Deinterlace_Discard();
|
|
|
|
void Deinterlace_Linear();
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ZM_IMAGE_H
|
2011-04-29 22:26:50 +08:00
|
|
|
|
2011-05-01 02:39:34 +08:00
|
|
|
/* Blend functions */
|
2011-05-05 17:52:00 +08:00
|
|
|
void sse2_fastblend(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, double blendpercent);
|
|
|
|
void std_fastblend(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, double blendpercent);
|
|
|
|
void std_blend(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, double blendpercent);
|
2011-05-01 02:39:34 +08:00
|
|
|
|
|
|
|
/* Delta functions */
|
2011-05-05 17:52:00 +08:00
|
|
|
void std_delta8_gray8(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void std_delta8_rgb(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void std_delta8_bgr(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void std_delta8_rgba(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void std_delta8_bgra(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void std_delta8_argb(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void std_delta8_abgr(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void sse2_delta8_gray8(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
2011-05-23 20:52:33 +08:00
|
|
|
void sse2_delta8_rgba(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void sse2_delta8_bgra(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void sse2_delta8_argb(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void sse2_delta8_abgr(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
2011-05-05 17:52:00 +08:00
|
|
|
void ssse3_delta8_rgba(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void ssse3_delta8_bgra(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void ssse3_delta8_argb(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
void ssse3_delta8_abgr(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
|
|
|
|
|
|
|
|
/* Convert functions */
|
2011-06-08 04:14:01 +08:00
|
|
|
void std_convert_rgb_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void std_convert_bgr_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
|
2011-05-05 17:52:00 +08:00
|
|
|
void std_convert_rgba_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void std_convert_bgra_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void std_convert_argb_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void std_convert_abgr_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void std_convert_yuyv_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void ssse3_convert_rgba_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void ssse3_convert_yuyv_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void zm_convert_yuyv_rgb(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void zm_convert_yuyv_rgba(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void zm_convert_rgb555_rgb(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void zm_convert_rgb555_rgba(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void zm_convert_rgb565_rgb(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
void zm_convert_rgb565_rgba(const uint8_t* col1, uint8_t* result, unsigned long count);
|
|
|
|
|
|
|
|
|