Code: Select all

```
#include "windows.h"
#include "avisynth.h"
#include "stdio.h"
class DGReinhard : public GenericVideoFilter
{
float contrast;
float bright;
public:
DGReinhard(PClip _child, float _contrast, float _bright, IScriptEnvironment* env) : GenericVideoFilter(_child)
{
if (vi.pixel_type != VideoInfo::CS_RGBPS)
{
env->ThrowError("DGReinhard: input must be CS_RGBPS");
}
contrast = _contrast;
bright = _bright;
}
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* env);
};
PVideoFrame __stdcall DGReinhard::GetFrame(int n, IScriptEnvironment* env)
{
PVideoFrame src = child->GetFrame(n, env);
PVideoFrame dst = env->NewVideoFrame(vi);
float *srcpR, *srcpG, *srcpB;
float *dstpR, *dstpG, *dstpB;
const int src_pitch = src->GetPitch(PLANAR_R) / 4;
const int dst_pitch = dst->GetPitch(PLANAR_R) / 4;
const int row_size = dst->GetRowSize(PLANAR_R);
const int width = vi.width;
const int height = dst->GetHeight(PLANAR_R);
const float offset = (1.0f - contrast) / contrast;
const float factor = (bright + offset) / bright;
srcpR = (float *)src->GetReadPtr(PLANAR_R);
dstpR = (float *)dst->GetWritePtr(PLANAR_R);
srcpG = (float *)src->GetReadPtr(PLANAR_G);
dstpG = (float *)dst->GetWritePtr(PLANAR_G);
srcpB = (float *)src->GetReadPtr(PLANAR_B);
dstpB = (float *)dst->GetWritePtr(PLANAR_B);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
dstpR[x] = srcpR[x] / (srcpR[x] + offset) * factor;
dstpG[x] = srcpG[x] / (srcpG[x] + offset) * factor;
dstpB[x] = srcpB[x] / (srcpB[x] + offset) * factor;
}
srcpR += src_pitch;
srcpG += src_pitch;
srcpB += src_pitch;
dstpR += dst_pitch;
dstpG += dst_pitch;
dstpB += dst_pitch;
}
return dst;
}
class DGHable : public GenericVideoFilter
{
float exposure, a, b, c, d, e, f, w;
public:
DGHable(PClip _child, float _exposure,
float _a,
float _b,
float _c,
float _d,
float _e,
float _f,
float _w,
IScriptEnvironment* env) : GenericVideoFilter(_child)
{
if (vi.pixel_type != VideoInfo::CS_RGBPS)
{
env->ThrowError("DGHable: input must be CS_RGBPS");
}
exposure = _exposure;
a = _a;
b = _b;
c = _c;
d = _d;
e = _e;
f = _f;
w = _w;
}
float hable(float in)
{
return (in * (in * a + b * c) + d * e) / (in * (in * a + b) + d * f) - e / f;
}
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* env);
};
PVideoFrame __stdcall DGHable::GetFrame(int n, IScriptEnvironment* env)
{
PVideoFrame src = child->GetFrame(n, env);
PVideoFrame dst = env->NewVideoFrame(vi);
float *srcpR, *srcpG, *srcpB;
float *dstpR, *dstpG, *dstpB;
const int src_pitch = src->GetPitch(PLANAR_R) / 4;
const int dst_pitch = dst->GetPitch(PLANAR_R) / 4;
const int row_size = dst->GetRowSize(PLANAR_R);
const int width = vi.width;
const int height = dst->GetHeight(PLANAR_R);
srcpR = (float *)src->GetReadPtr(PLANAR_R);
dstpR = (float *)dst->GetWritePtr(PLANAR_R);
srcpG = (float *)src->GetReadPtr(PLANAR_G);
dstpG = (float *)dst->GetWritePtr(PLANAR_G);
srcpB = (float *)src->GetReadPtr(PLANAR_B);
dstpB = (float *)dst->GetWritePtr(PLANAR_B);
float whitescale = 1.0f / hable(w);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
dstpR[x] = hable(exposure * srcpR[x]) * whitescale;
dstpG[x] = hable(exposure * srcpG[x]) * whitescale;
dstpB[x] = hable(exposure * srcpB[x]) * whitescale;
}
srcpR += src_pitch;
srcpG += src_pitch;
srcpB += src_pitch;
dstpR += dst_pitch;
dstpG += dst_pitch;
dstpB += dst_pitch;
}
return dst;
}
AVSValue __cdecl Create_DGReinhard(AVSValue args, void* user_data, IScriptEnvironment* env)
{
return new DGReinhard(args[0].AsClip(),
(float)args[1].AsFloat(0.3f),
(float)args[2].AsFloat(5.0f),
env);
}
AVSValue __cdecl Create_DGHable(AVSValue args, void* user_data, IScriptEnvironment* env)
{
return new DGHable(args[0].AsClip(),
(float)args[1].AsFloat(2.0f),
(float)args[2].AsFloat(0.15f),
(float)args[3].AsFloat(0.50f),
(float)args[4].AsFloat(0.10f),
(float)args[5].AsFloat(0.20f),
(float)args[6].AsFloat(0.02f),
(float)args[7].AsFloat(0.30f),
(float)args[8].AsFloat(11.20f),
env);
}
const AVS_Linkage *AVS_linkage = 0;
extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit3(IScriptEnvironment* env, AVS_Linkage* vectors)
{
AVS_linkage = vectors;
env->AddFunction("DGReinhard", "c[contrast]f[bright]f", Create_DGReinhard, 0);
env->AddFunction("DGHable", "c[exposure]f[a]f[b]f[c]f[d]f[e]f[f]f[w]f", Create_DGHable, 0);
return 0;
}
```