From d130169ed2f9cdae25666f7c5ae95286f5d69e54 Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Tue, 14 Aug 2007 01:08:00 +0000 Subject: [PATCH] Rename video_frame.h to image.h, various cleanup, and add functions to handle edge conditions. Originally committed to SVN as r1487. --- OverLua/avisynth.cpp | 2 +- OverLua/{video_frame.h => image.h} | 61 +++++++++++++++++++++++++----- OverLua/overlua.h | 2 +- OverLua/raster_ops.cpp | 2 +- 4 files changed, 55 insertions(+), 12 deletions(-) rename OverLua/{video_frame.h => image.h} (92%) diff --git a/OverLua/avisynth.cpp b/OverLua/avisynth.cpp index 2838b5325..f1a51230f 100644 --- a/OverLua/avisynth.cpp +++ b/OverLua/avisynth.cpp @@ -25,7 +25,7 @@ */ // Must be included before , otherwise some macros from get in the way -#include "video_frame.h" +#include "image.h" #include #include diff --git a/OverLua/video_frame.h b/OverLua/image.h similarity index 92% rename from OverLua/video_frame.h rename to OverLua/image.h index 28f0e8183..a42a16e55 100644 --- a/OverLua/video_frame.h +++ b/OverLua/image.h @@ -1,5 +1,5 @@ /* - * OverLua video frame access interface + * OverLua RGB(A) image interface * Copyright 2007 Niels Martin Hansen @@ -269,13 +269,13 @@ private: } else { badtable: - lua_pushliteral(L, "Value set into image pixel must be a table with at least 3 entries"); + lua_pushliteral(L, "Value set into image pixel must be a table with 3 entries"); lua_error(L); return 0; } } - lua_pushliteral(L, "Undefined field in video frame"); + lua_pushliteral(L, "Undefined field in image"); lua_error(L); return 0; } @@ -362,7 +362,7 @@ badtable: // Always RGB24 format since we don't support video with alpha cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_RGB24, (*ud)->width, (*ud)->height); if (!surf || cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS) { - lua_pushliteral(L, "Unable to create cairo surface from video frame"); + lua_pushliteral(L, "Unable to create cairo surface from image"); lua_error(L); return 0; } @@ -499,11 +499,8 @@ badtable: }; -// Now, escape from the template madness that is OverLuaVideoFrame. -// OverLuaFrameAggregate is the class that will be used for most passing around -// in the C++ code. It nicely hides all templatyness away into various implementations. -// This could probably have been designed better. Shame on me. - +// Now something so we can pretend all images have the same pixel format +// and can pass around pointers to objects of one fixed base class. class BaseImageAggregate { public: virtual PixelFormat::ARGB GetPixel(int x, int y) = 0; @@ -574,4 +571,50 @@ typedef BaseImageAggregateImpl ImageBGRA; typedef BaseImageAggregateImpl ImageARGB; typedef BaseImageAggregateImpl ImageABGR; + +// Access pixels with various edge conditions + +template +inline PixFmt &GetPixelEdgeBlackness(BaseImage &img, int x, int y) +{ + if (x < 0 || y < 0 || x >= img.width || x >= img.height) { + return PixFmt(); // all construct with all zeroes + } else { + return img.Pixel(x,y); + } +} + +template +inline PixFmt &GetPixelEdgeClosest(BaseImage &img, int x, int y) +{ + if (x < 0) x = 0; + if (y < 0) y = 0; + if (x >= img.width) x = img.width-1; + if (y >= img.height) y = img.height - 1; + return img.Pixel(x,y); +} + +template +inline PixFmt &GetPixelEdgeRepeat(BaseImage &img, int x, int y) +{ + while (x < 0) x += img.width; + while (y < 0) y += img.height; + while (x >= img.width) x -= img.width; + while (y >= img.height) y -= img.height; + return img.Pixel(x,y); +} + +template +inline PixFmt &GetPixelEdgeMirror(BaseImage &img, int x, int y) +{ + while (x < 0) x += img.width*2; + while (y < 0) y += img.height*2; + while (x >= img.width*2) x -= img.width*2; + while (y >= img.height*2) y -= img.height*2; + if (x >= img.width) x = img.width - x; + if (y >= img.height) y = img.height - y; + return img.Pixel(x,y); +} + + #endif diff --git a/OverLua/overlua.h b/OverLua/overlua.h index f881efa3d..f34238a06 100644 --- a/OverLua/overlua.h +++ b/OverLua/overlua.h @@ -34,7 +34,7 @@ #include "../lua51/src/lauxlib.h" #include -#include "video_frame.h" +#include "image.h" #include "cairo_wrap.h" #include "raster_ops.h" diff --git a/OverLua/raster_ops.cpp b/OverLua/raster_ops.cpp index ae9a3620c..4473255de 100644 --- a/OverLua/raster_ops.cpp +++ b/OverLua/raster_ops.cpp @@ -25,8 +25,8 @@ */ #include "cairo_wrap.h" +#include "image.h" #include -//#include #include #include