summaryrefslogtreecommitdiffstats
path: root/digikam/imageplugins/filmgrain/filmgrain.cpp
blob: 9ca2df0f350b8477244eaf370f21e842127a16de (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-05-25
 * Description : FilmGrain threaded image filter.
 * 
 * Copyright (C) 2005-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * Copyright (C) 2005-2007 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 * 
 * 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, 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.
 * 
 * ============================================================ */

// C++ includes.

#include <cmath>
#include <cstdlib>

// TQt includes.

#include <tqdatetime.h> 

// Local includes.

#include "ddebug.h"
#include "dimg.h"
#include "dimggaussianblur.h"
#include "imagecurves.h"
#include "imagehistogram.h"
#include "dimgimagefilters.h"
#include "filmgrain.h"

namespace DigikamFilmGrainImagesPlugin
{

FilmGrain::FilmGrain(Digikam::DImg *orgImage, TQObject *tqparent, int sensibility)
         : Digikam::DImgThreadedFilter(orgImage, tqparent, "FilmGrain")
{
    m_sensibility = sensibility;
    initFilter();
}

void FilmGrain::filterImage(void)
{
    filmgrainImage(&m_orgImage, m_sensibility);
}

// This method is based on the Simulate Film grain tutorial from GimpGuru.org web site 
// available at this url : http://www.gimpguru.org/Tutorials/FilmGrain

void FilmGrain::filmgrainImage(Digikam::DImg *orgImage, int Sensibility)
{
    // Sensibility: 800..6400

    if (Sensibility <= 0) return;

    int Width = orgImage->width();
    int Height = orgImage->height();
    int bytesDepth = orgImage->bytesDepth();
    bool sixteenBit = orgImage->sixteenBit();
    uchar* data = orgImage->bits();

    Digikam::DImg grain(Width, Height, sixteenBit);      // Grain blured without curves adjustment.
    Digikam::DImg tqmask(Width, Height, sixteenBit);       // Grain tqmask with curves adjustment.
    uchar* pGrainBits = grain.bits();
    uchar* pMaskBits  = tqmask.bits();
    uchar* pOutBits = m_destImage.bits(); // Destination image with merged grain tqmask and original.

    int Noise, Shade, nRand, component, progress;
    uchar *ptr;
    Digikam::DColor blendData, grainData, maskData, outData;

    if (sixteenBit)
        Noise = (Sensibility / 10 + 1) * 256 - 1;
    else
        Noise = Sensibility / 10;

    // This value controls the shading pixel effect between original image and grain tqmask.
    if (sixteenBit)
        Shade = (52 + 1) * 256 - 1;
    else
        Shade = 52;

    TQDateTime dt = TQDateTime::tqcurrentDateTime();
    TQDateTime Y2000( TQDate(2000, 1, 1), TQTime(0, 0, 0) );
    uint seed = (uint) dt.secsTo(Y2000);

    // Make gray grain tqmask.

    grainData.setSixteenBit(sixteenBit);

    for (int x = 0; !m_cancel && x < Width; x++)
    {
        for (int y = 0; !m_cancel && y < Height; y++)
        {
            ptr = pGrainBits + x*bytesDepth + (y*Width*bytesDepth);

            nRand = (rand_r(&seed) % Noise) - (Noise / 2);
            if (sixteenBit)
                component = CLAMP(32768 + nRand, 0, 65535);
            else
                component = CLAMP(128 + nRand, 0, 255);

            grainData.setRed  (component);
            grainData.setGreen(component);
            grainData.setBlue (component);
            grainData.setAlpha(0);

            grainData.setPixel(ptr);
        }

        // Update progress bar in dialog.
        progress = (int) (((double)x * 25.0) / Width);

        if (progress%5 == 0)
            postProgress( progress );
    }

    // Smooth grain tqmask using gaussian blur with radius 1.
    Digikam::DImgGaussianBlur(this, grain, grain, 25, 30, 1);

    // Normally, film grain tends to be most noticeable in the midtones, and much less 
    // so in the shadows and highlights. Adjust histogram curve to adjust grain like this.

    Digikam::ImageCurves *grainCurves = new Digikam::ImageCurves(sixteenBit);

    // We modify only global luminosity of the grain.
    if (sixteenBit)
    {
        grainCurves->setCurvePoint(Digikam::ImageHistogram::ValueChannel, 0,  TQPoint(0,     0));
        grainCurves->setCurvePoint(Digikam::ImageHistogram::ValueChannel, 8,  TQPoint(32768, 32768));
        grainCurves->setCurvePoint(Digikam::ImageHistogram::ValueChannel, 16, TQPoint(65535, 0));
    }
    else
    {
        grainCurves->setCurvePoint(Digikam::ImageHistogram::ValueChannel, 0,  TQPoint(0,   0));
        grainCurves->setCurvePoint(Digikam::ImageHistogram::ValueChannel, 8,  TQPoint(128, 128));
        grainCurves->setCurvePoint(Digikam::ImageHistogram::ValueChannel, 16, TQPoint(255, 0));
    }

    // Calculate curves and lut to apply on grain.
    grainCurves->curvesCalculateCurve(Digikam::ImageHistogram::ValueChannel);
    grainCurves->curvesLutSetup(Digikam::ImageHistogram::AlphaChannel);
    grainCurves->curvesLutProcess(pGrainBits, pMaskBits, Width, Height);

    grain.reset();
    delete grainCurves;

    // Update progress bar in dialog.
    postProgress( 40 );

    // Merge src image with grain using shade coefficient.

    int alpha;
    // get composer for default blending
    Digikam::DColorComposer *composer = Digikam::DColorComposer::getComposer(Digikam::DColorComposer::PorterDuffNone);

    for (int x = 0; !m_cancel && x < Width; x++)
    {
        for (int y = 0; !m_cancel && y < Height; y++)
        {
            int offset = x*bytesDepth + (y*Width*bytesDepth);

            // read color from orig image
            blendData.setColor(data + offset, sixteenBit);
            // read color from tqmask
            maskData.setColor(pMaskBits + offset, sixteenBit);
            // set shade as alpha value - it will be used as source alpha when blending
            maskData.setAlpha(Shade);

            // compose, write result to blendData.
            // Preserve alpha, do not blend it (taken from old algorithm - correct?)
            alpha = blendData.alpha();
            composer->compose(blendData, maskData);
            blendData.setAlpha(alpha);

            // write to destination
            blendData.setPixel(pOutBits + offset);
        }

        // Update progress bar in dialog.
        progress = (int) (50.0 + ((double)x * 50.0) / Width);

        if (progress%5 == 0)
           postProgress( progress );
    }

    delete composer;
}

}  // NameSpace DigikamFilmGrainImagesPlugin