File: crop.cc

package info (click to toggle)
exactimage 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,048 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (139 lines) | stat: -rw-r--r-- 3,862 bytes parent folder | download | duplicates (5)
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
/*
 * Canvas cropping.
 * Copyright (C) 2006 - 2017 René Rebe, ExactCODE
 *
 * 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; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * Alternatively, commercial licensing options are available from the
 * copyright holder ExactCODE GmbH Germany.
 */

#include <string.h> // memmove
#include <iostream>
#include <algorithm>
#ifndef _MSC_VER
#include <vector>
#endif

#include "Image.hh"
#include "Codecs.hh"

#include "Colorspace.hh"

#include "crop.hh"

void crop (Image& image, int x, int y, unsigned int w, unsigned int h)
{
  // limit to valid boundaries
  if (x < 0) { w += x; x = 0; };
  if (y < 0) { h += y; y = 0; };
  x = std::min (x, image.w-1);
  y = std::min (y, image.h-1);
  w = std::min (w, (unsigned)image.w-x);
  h = std::min (h, (unsigned)image.h-y);

  // something to do?
  if (x == 0 && y == 0 && w == (unsigned int)image.w && h == (unsigned int)image.h)
    return;
  
  if (!image.isModified() && image.getCodec())
    if (image.getCodec()->crop(image, x, y, w, h))
      return;
  
  /*
    std::cerr << "after limiting: " << x << " " << y
    << " " << w << " " << h << std::endl;
  */

  // truncate the height, this is optimized for the "just height" case
  // (of e.g. fastAutoCrop)
  if (x == 0 && y == 0 && w == (unsigned int)image.w) {
    image.setRawData (); // invalidate
    image.h = h;
    return;
  }
  
  // bit shifting is too expensive, crop at least byte-wide
  int orig_bps = image.bps;
  if (orig_bps < 8)
    colorspace_grayX_to_gray8 (image);
  
  int stride = image.stride();
  int cut_stride = image.spp * image.bps * w / 8; // sub-byte avoided above
  
  uint8_t* dst = image.getRawData ();
  uint8_t* src = dst + stride * y + (stride * x / image.w);
  
  for (unsigned int i = 0; i < h; ++i) {
    memmove (dst, src, cut_stride);
    dst += cut_stride;
    src += stride;
  }
  
  image.setRawData (); // invalidate
  image.rowstride = 0; // re-set to native stride
  image.w = w;
  image.h = h;
 
  switch (orig_bps) {
  case 1:
    colorspace_gray8_to_gray1 (image);
    break;
  case 2:
    colorspace_gray8_to_gray2 (image);
    break;
  case 4:
    colorspace_gray8_to_gray4 (image);
    break;
  default:
    ;
  }
}

// auto crop just the bottom of an image filled in the same, solid color
// optimization: for sub-byte depth we compare a 8bit pattern unit at-a-time
void fastAutoCrop (Image& image)
{
  if (!image.getRawData())
    return;
  
  const int stride = image.stride();
  const int stridefill = image.stridefill();
 
  int h = image.h - 1;
  uint8_t* data = image.getRawData() + stride * h;
  uint8_t* ref = data; // ref value to compare against
  
  // decrement at begining, to compare one line earlier with reference
  for (--h, data -= stride; h >= 0; --h, data -= stride) {
    // data row
    int i = 0;
    for (; i < stridefill; ++i)
      {
	if (data[i] != ref[i]) {
	  break; // pixel differs, break out
	}
      }
    
    if (i != stridefill)
      break; // non-solid line, break out
  }
  ++h; // we are at the line that differs
  if (h == 0) // do not crop if the image is totally empty
    return;
  
  // We could just tweak the image height here, but using the generic
  // code we benefit from possible optimization, such as lossless
  // jpeg cropping.
  // We do not explicitly check if we crop, the crop function will optimize
  // a NOP crop away for all callers.
  return crop (image, 0, 0, image.w, h);
}