File: ImageManipulation.cpp

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (414 lines) | stat: -rw-r--r-- 13,268 bytes parent folder | download | duplicates (2)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**********************************************************************

  Audacity: A Digital Audio Editor

  ImageManipulation.cpp

  Dominic Mazzoni
  James Crook

  wxWidgets license (Dominic to confirm).

********************************************************************//*!

\file ImageManipulation.cpp

Provides Image Manipulation functions.

wxWidgets misses some important functions involving cutting and
pasting bitmaps, and (in version 2.6.1) is patchy in support of alpha
channel.  This collection of functions fills that gap.

*//*********************************************************************/


#include "ImageManipulation.h"

#include <wx/colour.h>
#include <wx/image.h>

#include "AllThemeResources.h"
#include "Theme.h"

/// This looks at the first pixel in the image, and shifts
/// the entire image by the vector difference between that
/// pixel and the dstColour.  For better control, use
/// ChangeImageColour(wxImage, wxColour*, wxColour*) below
std::unique_ptr<wxImage> ChangeImageColour(wxImage * srcImage, wxColour & dstColour)
{
   unsigned char *src = srcImage->GetData();
   wxColour c;
   c.Set(src[0], src[1], src[2]);
   return ChangeImageColour(srcImage, c, dstColour);
}

///This will explicitly shift the image color from
///srcColour to dstColour.
std::unique_ptr<wxImage> ChangeImageColour(wxImage * srcImage,
                           wxColour & srcColour,
                           wxColour & dstColour)
{
   // This function takes a source image, which it assumes to
   // be grayscale, and smoothly changes the overall color
   // to the specified color, and returns the result as a
   // NEW image.  This works well for grayscale 3D images.
   // Audacity uses this routines to make the buttons
   // (skip-start, play, stop, record, skip-end) adapt to
   // the color scheme of the user.

   unsigned char *src = srcImage->GetData();
   int width = srcImage->GetWidth();
   int height = srcImage->GetHeight();

   auto dstImage = std::make_unique<wxImage>(width, height);
   unsigned char *dst = dstImage->GetData();


   //Get the source color
   int srcVal[3], srcOpp[3];
   srcVal[0] = srcColour.Red();
   srcVal[1] = srcColour.Green();
   srcVal[2] = srcColour.Blue();

   int dstVal[3], dstOpp[3];
   dstVal[0] = dstColour.Red();
   dstVal[1] = dstColour.Green();
   dstVal[2] = dstColour.Blue();

   int i;
   for (i = 0; i < 3; i++) {
      srcOpp[i] = 256 - srcVal[i];  // avoid zero!
      dstOpp[i] = 255 - dstVal[i];
   }

   int c = 0;
   for (i = 0; i < width * height * 3; i++) {
      int s = (int) *src;

      if (s >= srcVal[c])
         *dst++ = dstVal[c] + dstOpp[c] * (s - srcVal[c]) / srcOpp[c];

      else
         *dst++ = dstVal[c] * s / srcVal[c];
      src++;
      c = (c + 1) % 3;
   }

   if (srcImage->HasAlpha()) {
      // Preserve transparencies
      dstImage->InitAlpha();
      memcpy(dstImage->GetAlpha(), srcImage->GetAlpha(), width * height);
   }

   return dstImage;
}

/// Takes a background image, foreground image, and mask
/// (i.e. the alpha channel for the foreground), and
/// returns a NEW image where the foreground has been
/// overlaid onto the background using alpha-blending,
/// at location (xoff, yoff).
std::unique_ptr<wxImage> OverlayImage(wxImage * background, wxImage * foreground,
                      wxImage * mask, int xoff, int yoff)
{
   unsigned char *bg = background->GetData();
   unsigned char *fg = foreground->GetData();
   unsigned char *mk = mask->GetData();

   int bgWidth = background->GetWidth();
   int bgHeight = background->GetHeight();
   int fgWidth = foreground->GetWidth();
   int fgHeight = foreground->GetHeight();
   int mkWidth = mask->GetWidth();
   int mkHeight = mask->GetHeight();


   //Now, determine the dimensions of the images to be masked together
   //on top of the background.  This should be equal to the area of the
   //smaller of the foreground and the mask, as long as it is
   //within the area of the background, given the offset.

   //Make sure the foreground size is no bigger than the mask
   int wCutoff = (fgWidth < mkWidth) ? fgWidth : mkWidth;
   int hCutoff = (fgHeight < mkHeight) ? fgHeight : mkHeight;


   // If the masked foreground + offset is bigger than the background, masking
   // should only occur within these bounds of the foreground image
   wCutoff = (bgWidth - xoff > wCutoff) ? wCutoff : bgWidth - xoff;
   hCutoff = (bgHeight - yoff > hCutoff) ? hCutoff : bgHeight - yoff;


   //Make a NEW image the size of the background
   auto dstImage = std::make_unique<wxImage>(bgWidth, bgHeight);
   unsigned char *dst = dstImage->GetData();
   memcpy(dst, bg, bgWidth * bgHeight * 3);


   // Go through the foreground image bit by bit and mask it on to the
   // background, at an offset of xoff,yoff.
   // BUT...Don't go beyond the size of the background image,
   // the foreground image, or the mask
   int x, y;
   for (y = 0; y < hCutoff; y++) {

      unsigned char *bkp = bg + 3 * ((y + yoff) * bgWidth + xoff);
      unsigned char *dstp = dst + 3 * ((y + yoff) * bgWidth + xoff);

      for (x = 0; x < wCutoff; x++) {

         int value = mk[3 * (y * mkWidth + x)];
         int opp = 255 - value;

         for (int c = 0; c < 3; c++)
            dstp[x * 3 + c] =
               ((bkp[x * 3 + c] * opp) +
                (fg[3 * (y * fgWidth + x) + c] * value)) / 255;
      }
   }
   return dstImage;
}

/// Takes a background image, foreground image, and mask
/// (i.e. the alpha channel for the foreground), and
/// returns a NEW image where the foreground has been
/// overlaid onto the background using alpha-blending,
/// at location (xoff, yoff).
std::unique_ptr<wxImage> OverlayImage(teBmps eBack, teBmps eForeground,
                      int xoff, int yoff)
{
   wxImage imgBack(theTheme.Image( eBack       ));
   wxImage imgFore(theTheme.Image( eForeground ));


   // TMP: dmazzoni - just so the code runs even though not all of
   // our images have transparency...
   if (!imgFore.HasAlpha())
      return std::make_unique<wxImage>(imgBack);


   wxASSERT( imgFore.HasAlpha() );

   unsigned char *bg = imgBack.GetData();
   unsigned char *fg = imgFore.GetData();
   unsigned char *mk = imgFore.GetAlpha();

   int bgWidth = imgBack.GetWidth();
   int bgHeight = imgBack.GetHeight();
   int fgWidth = imgFore.GetWidth();
   int fgHeight = imgFore.GetHeight();


   //Now, determine the dimensions of the images to be masked together
   //on top of the background.  This should be equal to the area of the
   //smaller of the foreground and the mask, as long as it is
   //within the area of the background, given the offset.

   //Make sure the foreground size is no bigger than the mask
   int wCutoff = fgWidth;
   int hCutoff = fgHeight;


   // If the masked foreground + offset is bigger than the background, masking
   // should only occur within these bounds of the foreground image
   wCutoff = (bgWidth - xoff > wCutoff) ? wCutoff : bgWidth - xoff;
   hCutoff = (bgHeight - yoff > hCutoff) ? hCutoff : bgHeight - yoff;

   //Make a NEW image the size of the background
   auto dstImage = std::make_unique<wxImage>(bgWidth, bgHeight);
   unsigned char *dst = dstImage->GetData();
   memcpy(dst, bg, bgWidth * bgHeight * 3);

   // If background image has tranparency, then we want to blend with the 
   // current background colour.
   if( imgBack.HasAlpha() ){
      unsigned char *pAlpha = imgBack.GetAlpha();
      wxColour c = theTheme.Colour( clrMedium  );
      unsigned char onePixImage[3];
      // GetData() guarantees RGB order [wxWidgets does the ocnversion]
      onePixImage[ 0 ] = c.Red();
      onePixImage[ 1 ] = c.Green();
      onePixImage[ 2 ] = c.Blue();
      for( int i=0;i< bgWidth*bgHeight;i++){
         unsigned char * pPix = &dst[ 3*i];
         float alpha = 1.0 - (pAlpha[i]/255.0);
         pPix[0] = pPix[0] + alpha *( (int)onePixImage[0]-(int)pPix[0]);
         pPix[1] = pPix[1] + alpha *( (int)onePixImage[1]-(int)pPix[1]);
         pPix[2] = pPix[2] + alpha *( (int)onePixImage[2]-(int)pPix[2]);
      }
   }

   // Go through the foreground image bit by bit and mask it on to the
   // background, at an offset of xoff,yoff.
   // BUT...Don't go beyond the size of the background image,
   // the foreground image, or the mask
   int x, y;
   for (y = 0; y < hCutoff; y++) {

      unsigned char *bkp = bg + 3 * ((y + yoff) * bgWidth + xoff);
      unsigned char *dstp = dst + 3 * ((y + yoff) * bgWidth + xoff);

      for (x = 0; x < wCutoff; x++) {

         int value = mk[(y * fgWidth + x)];// Don't multiply by 3...
         int opp = 255 - value;

         for (int c = 0; c < 3; c++)
            dstp[x * 3 + c] =
               ((bkp[x * 3 + c] * opp) +
                (fg[3 * (y * fgWidth + x) + c] * value)) / 255;
      }
   }
   return dstImage;
}

// Creates an image with a solid background color
std::unique_ptr<wxImage> CreateBackground(int width, int height, wxColour colour)
{
   auto i = std::make_unique<wxImage>(width, height);
   unsigned char *ip;
   int srcVal[3];
   int x;

   srcVal[0] = colour.Red();
   srcVal[1] = colour.Green();
   srcVal[2] = colour.Blue();

   ip = i->GetData();
   for(x=0; x<width*height; x++) {
      *ip++ = srcVal[0];
      *ip++ = srcVal[1];
      *ip++ = srcVal[2];
   }

   return i;
}

std::unique_ptr<wxImage> CreateSysBackground
   (int width, int height, int WXUNUSED(offset), wxColour colour)
{
   return CreateBackground(width, height, colour);
}

/// Pastes one image into another including the alpha channel.
/// Differs from OverlayImage in that:
///   Happens in place to existing background image.
///   Pastes image on top; no blending with existing background is done.
void PasteSubImage( wxImage * background, wxImage * foreground, int xoff, int yoff )
{

   unsigned char *bg = background->GetData();
   unsigned char *fg = foreground->GetData();
   unsigned char *bgAlpha = background->HasAlpha() ? background->GetAlpha() : NULL;
   unsigned char *fgAlpha = foreground->HasAlpha() ? foreground->GetAlpha() : NULL;
   // For testing... Set as if no alpha in foreground....
   // fgAlpha = NULL;

   int bgWidth = background->GetWidth();
   int bgHeight = background->GetHeight();
   int fgWidth = foreground->GetWidth();
   int fgHeight = foreground->GetHeight();

   int wCutoff = fgWidth;
   int hCutoff = fgHeight;

   // If the masked foreground + offset is bigger than the background, masking
   // should only occur within these bounds of the foreground image
   wCutoff = (bgWidth - xoff > wCutoff) ? wCutoff : bgWidth - xoff;
   hCutoff = (bgHeight - yoff > hCutoff) ? hCutoff : bgHeight - yoff;

   // Go through the foreground image bit by bit and place it on to the
   // background, at an offset of xoff,yoff.
   // Don't go beyond the size of the background image,
   // or the foreground image.
   int y;
   unsigned char *bkp;
   unsigned char *fgp;
   unsigned char *bgAlphap;
   unsigned char *fgAlphap;
   for (y = 0; y < hCutoff; y++) {
      // RGB bytes
      bkp = bg + 3 * ((y + yoff) * bgWidth + xoff);
      fgp = fg + 3 * ( y * fgWidth);
      memcpy( bkp, fgp, 3 * wCutoff );

      // Alpha bytes.
      if( bgAlpha )
      {
         bgAlphap = bgAlpha + ((y+yoff) * bgWidth + xoff );
         if( fgAlpha )
         {
            fgAlphap = fgAlpha + (y * fgWidth );
            memcpy( bgAlphap, fgAlphap, wCutoff );
         }
         else
         {
            memset( bgAlphap, 255, wCutoff );
         }
      }
   }
}

/// Gets a rectangle from within another image, INCLUDING the alpha channel
/// \bug in wxWidgets, wxImage::GetSubImage should do this itself.
wxImage GetSubImageWithAlpha( const wxImage & Src,  const wxRect &rect )
{
   //First part of this code is lifted from wxImage::GetSubImage() source code.
   wxImage image;

   wxCHECK_MSG( Src.Ok(), image, wxT("invalid image") );

   wxCHECK_MSG( (rect.GetLeft()>=0) && (rect.GetTop()>=0) && (
      rect.GetRight()<=Src.GetWidth()) && (rect.GetBottom()<=Src.GetHeight()),
      image, wxT("invalid subimage size") );

   int subwidth=rect.GetWidth();
   const int subheight=rect.GetHeight();

   image.Create( subwidth, subheight, false );

   unsigned char *subdata = image.GetData(), *data=Src.GetData();

   wxCHECK_MSG( subdata, image, wxT("unable to create image") );

   // JKC: Quick hack - don't deal with masks - need to understand macro M_IMGDATA first.
//   if (Src.M_IMGDATA->m_hasMask)
//      image.SetMaskColour( Src.M_IMGDATA->m_maskRed, Src.M_IMGDATA->m_maskGreen, Src.M_IMGDATA->m_maskBlue );

   int subleft=3*rect.GetLeft();
   int width=3*Src.GetWidth();
   subwidth*=3;

   data+=rect.GetTop()*width+subleft;

   for (long j = 0; j < subheight; ++j)
   {
      memcpy( subdata, data, subwidth);
      subdata+=subwidth;
      data+=width;
   }

   image.InitAlpha();
   if( !Src.HasAlpha() )
      return image;
   // OK, so we've copied the RGB data.
   // Now do the Alpha channel.
   //wxASSERT( Src.HasAlpha() );

   subleft/=3;
   width/=3;
   subwidth/=3;

   data =Src.GetAlpha();
   subdata =image.GetAlpha();

   data+=rect.GetTop()*width+subleft;

   for (long j = 0; j < subheight; ++j)
   {
      memcpy( subdata, data, subwidth);
      subdata+=subwidth;
      data+=width;
   }
   return image;
}