forked from fanxu/ffld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JPEGImage.h
104 lines (84 loc) · 4.11 KB
/
JPEGImage.h
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
//--------------------------------------------------------------------------------------------------
// Implementation of the paper "Exact Acceleration of Linear Object Detectors", 12th European
// Conference on Computer Vision, 2012.
//
// Copyright (c) 2012 Idiap Research Institute, <http://www.idiap.ch/>
// Written by Charles Dubout <[email protected]>
//
// This file is part of FFLD (the Fast Fourier Linear Detector)
//
// FFLD is free software: you can redistribute it and/or modify it under the terms of the GNU
// General Public License version 3 as published by the Free Software Foundation.
//
// FFLD 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.
//
// You should have received a copy of the GNU General Public License along with FFLD. If not, see
// <http://www.gnu.org/licenses/>.
//--------------------------------------------------------------------------------------------------
#ifndef FFLD_JPEGIMAGE_H
#define FFLD_JPEGIMAGE_H
#include <string>
#include <vector>
#include <stdint.h>
namespace FFLD
{
/// The JPEGImage class allows to load/save an image from/to a jpeg file, and to apply basic
/// operations such as resizing or cropping to it. The pixels are stored contiguously in row-major
/// order (scanline 0: RGB RGB RGB, scanline 1: RGB RGB RGB...)
class JPEGImage
{
public:
/// Constructs an empty image. An empty image has zero size.
JPEGImage();
/// Constructs an image with the given @p width, @p height and @p depth, and initializes it from
/// the given @p bits.
/// @note The returned image might be empty if any of the parameters is incorrect.
JPEGImage(int width, int height, int depth, const uint8_t * bits = 0);
/// Constructs an image and tries to load the image from the jpeg file with the given
/// @p filename.
/// @note The returned image might be empty if the image could not be loaded.
JPEGImage(const std::string & filename);
/// Returns the width of the image.
int width() const;
/// Returns the height of the image.
int height() const;
/// Returns the depth of the image. The image depth is the number of color channels.
int depth() const;
/// Returns a pointer to the pixel data.
/// @note Returns a null pointer if the image is empty.
const uint8_t * bits() const;
/// Returns a pointer to the pixel data. Returns a null pointer if the image is empty.
uint8_t * bits();
/// Returns a pointer to the pixel data at the scanline with index y. The first scanline is at
/// index 0. Returns a null pointer if the image is empty or if y is out of bounds.
const uint8_t * scanLine(int y) const;
/// Returns a pointer to the pixel data at the scanline with index y. The first scanline is at
/// index 0. Returns a null pointer if the image is empty or if y is out of bounds.
uint8_t * scanLine(int y);
/// Returns whether the image is empty. An empty image has zero size.
bool empty() const;
/// Saves the image to a jpeg file with the given @p filename and @p quality.
void save(const std::string & filename, int quality = 100) const;
/// Returns a copy of the image scaled to the given @p width and @p height.
/// If either the width or the height is zero or negative, the method returns an empty image.
JPEGImage resize(int width, int height) const;
/// Returns a copy of a region of the image located at @p x and @p y, and of dimensions @p width
/// and @p height.
/// @note The returned image might be smaller if some of the coordinates are outside the image.
JPEGImage crop(int x, int y, int width, int height) const;
private:
// Blur and downscale an image by a factor 2
static void Halve(const uint8_t * src, int srcWidth, int srcHeight, uint8_t * dst,
int dstWidth, int dstHeight, int depth);
// Resize an image to the specified dimensions using bilinear interpolation
static void Resize(const uint8_t * src, int srcWidth, int srcHeight, uint8_t * dst,
int dstWidth, int dstHeight, int depth);
int width_;
int height_;
int depth_;
std::vector<uint8_t> bits_;
};
}
#endif