2010-12-11 23:55:48 +01:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-02-24 01:19:47 +01:00
|
|
|
* Copyright (c) 2010-2021, Tomasz Sowa
|
2010-12-11 23:55:48 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2010-12-11 23:55:48 +01:00
|
|
|
*/
|
|
|
|
|
2011-09-13 08:08:34 +02:00
|
|
|
#ifndef headerfile_winix_core_image
|
|
|
|
#define headerfile_winix_core_image
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
2011-01-05 22:24:11 +01:00
|
|
|
#include "basethread.h"
|
2010-12-11 23:55:48 +01:00
|
|
|
#include "textstream.h"
|
|
|
|
#include "db/db.h"
|
2011-01-05 22:24:11 +01:00
|
|
|
#include "core/config.h"
|
2021-02-24 01:19:47 +01:00
|
|
|
#include "models/item.h"
|
2011-01-05 22:24:11 +01:00
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
class System;
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
|
2013-03-29 23:03:28 +01:00
|
|
|
// aspect modes:
|
2010-12-11 23:55:48 +01:00
|
|
|
// Width given, height automagically selected to preserve aspect ratio.
|
2011-09-13 08:08:34 +02:00
|
|
|
#define WINIX_IMAGE_MODE_1 1
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
// Height given, width automagically selected to preserve aspect ratio.
|
2011-09-13 08:08:34 +02:00
|
|
|
#define WINIX_IMAGE_MODE_2 2
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
// Maximum values of height and width given, aspect ratio preserved.
|
2011-09-13 08:08:34 +02:00
|
|
|
#define WINIX_IMAGE_MODE_3 3
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
// Minimum values of width and height given, aspect ratio preserved.
|
2011-09-13 08:08:34 +02:00
|
|
|
#define WINIX_IMAGE_MODE_4 4
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
// Width and height emphatically given, original aspect ratio ignored.
|
2011-09-13 08:08:34 +02:00
|
|
|
#define WINIX_IMAGE_MODE_5 5
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
// Change as per widthxheight but only if an image dimension exceeds a specified dimension.
|
2011-09-13 08:08:34 +02:00
|
|
|
#define WINIX_IMAGE_MODE_6 6
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
// Change dimensions only if both image dimensions are less than specified dimensions.
|
2011-09-13 08:08:34 +02:00
|
|
|
#define WINIX_IMAGE_MODE_7 7
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
|
2013-03-29 23:03:28 +01:00
|
|
|
|
|
|
|
|
2011-09-13 08:08:34 +02:00
|
|
|
// resizing
|
|
|
|
#define WINIX_IMAGE_TYPE_RESIZE 1
|
2010-12-11 23:55:48 +01:00
|
|
|
|
2011-09-13 08:08:34 +02:00
|
|
|
// generating a thumbnail
|
|
|
|
#define WINIX_IMAGE_TYPE_CREATE_THUMB 2
|
2010-12-11 23:55:48 +01:00
|
|
|
|
2013-03-29 23:03:28 +01:00
|
|
|
// cropping an image
|
|
|
|
#define WINIX_IMAGE_TYPE_CROP 3
|
|
|
|
|
|
|
|
// cropping an thumbnail
|
|
|
|
#define WINIX_IMAGE_TYPE_CROP_THUMB 4
|
2011-09-13 08:08:34 +02:00
|
|
|
|
2013-03-29 23:03:28 +01:00
|
|
|
// creating by cropping a new thumbnail (from an original image)
|
|
|
|
#define WINIX_IMAGE_TYPE_CREATE_CROP_NEW_THUMB 5
|
2011-09-13 08:08:34 +02:00
|
|
|
|
|
|
|
|
2013-03-29 23:03:28 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
*/
|
2011-09-13 08:08:34 +02:00
|
|
|
class Image : public BaseThread
|
2010-12-11 23:55:48 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2013-03-29 23:03:28 +01:00
|
|
|
struct Scale
|
|
|
|
{
|
|
|
|
size_t cx;
|
|
|
|
size_t cy;
|
|
|
|
int aspect_mode;
|
|
|
|
int quality;
|
|
|
|
|
|
|
|
Scale()
|
|
|
|
{
|
|
|
|
cx = cy = 1;
|
|
|
|
aspect_mode = 2;
|
|
|
|
quality = 100;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-11 23:55:48 +01:00
|
|
|
void SetDb(Db * pdb);
|
2011-01-05 22:24:11 +01:00
|
|
|
void SetConfig(Config * pconfig);
|
|
|
|
void SetSystem(System * psystem);
|
2010-12-11 23:55:48 +01:00
|
|
|
|
2013-03-29 23:03:28 +01:00
|
|
|
|
|
|
|
// returning scale info for a directory
|
|
|
|
Scale GetImageScale(long dir_id);
|
|
|
|
|
|
|
|
// returning scale info (for thumbnails) for a directory
|
|
|
|
Scale GetThumbScale(long dir_id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Resize(long file_id, size_t cx, size_t cy, int aspect_mode, int quality);
|
|
|
|
|
|
|
|
// creating a new thumbnail from an original image
|
|
|
|
void CreateThumb(long file_id, size_t thumb_cx, size_t thumb_cy, int aspect_mode, int quality);
|
|
|
|
|
|
|
|
// cropping an image (the thumbnail is not changed)
|
|
|
|
void Crop(long file_id, size_t xoffset, size_t yoffset, size_t cx, size_t cy, int quality);
|
|
|
|
|
|
|
|
// cropping an existing thumbnail
|
|
|
|
void CropThumb(long file_id, size_t xoffset, size_t yoffset, size_t cx, size_t cy, int quality);
|
|
|
|
|
|
|
|
// creating and cropping a new thumbnail (from an original image)
|
|
|
|
void CropNewThumb(long file_id, size_t xoffset, size_t yoffset, size_t cx, size_t cy,
|
|
|
|
size_t thumb_cx, size_t thumb_cy, int aspect_mode, int quality);
|
2011-09-13 08:08:34 +02:00
|
|
|
|
|
|
|
|
2010-12-11 23:55:48 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
Db * db;
|
2011-01-05 22:24:11 +01:00
|
|
|
Config * config;
|
|
|
|
System * system;
|
2010-12-11 23:55:48 +01:00
|
|
|
|
2011-09-13 08:08:34 +02:00
|
|
|
struct ImageItem
|
2010-12-11 23:55:48 +01:00
|
|
|
{
|
2011-09-13 08:08:34 +02:00
|
|
|
int type; // WINIX_IMAGE_TYPE_*
|
2013-03-29 23:03:28 +01:00
|
|
|
long file_id;
|
2010-12-11 23:55:48 +01:00
|
|
|
size_t cx;
|
|
|
|
size_t cy;
|
2013-03-29 23:03:28 +01:00
|
|
|
size_t xoffset; // xoffset and yoffset are used when cropping
|
|
|
|
size_t yoffset;
|
|
|
|
size_t thumb_cx;
|
|
|
|
size_t thumb_cy;
|
2010-12-11 23:55:48 +01:00
|
|
|
int aspect_mode;
|
2011-09-13 08:08:34 +02:00
|
|
|
int quality;
|
2010-12-11 23:55:48 +01:00
|
|
|
};
|
|
|
|
|
2013-03-29 23:03:28 +01:00
|
|
|
|
|
|
|
template<typename int_type>
|
|
|
|
void SetMinMax(int_type & var, int var_min, int var_max)
|
|
|
|
{
|
|
|
|
if( static_cast<int>(var) < var_min )
|
|
|
|
var = var_min;
|
|
|
|
|
|
|
|
if( static_cast<int>(var) > var_max )
|
|
|
|
var = var_max;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-11 23:55:48 +01:00
|
|
|
// queue of thumbnails to create
|
2011-09-13 08:08:34 +02:00
|
|
|
typedef std::list<ImageItem> ImageTab;
|
|
|
|
ImageTab image_tab;
|
|
|
|
ImageItem item_temp;
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
// only for second thread
|
2011-09-13 08:08:34 +02:00
|
|
|
ImageItem item_work;
|
2011-01-05 22:24:11 +01:00
|
|
|
std::wstring src_path, dst_path;
|
2010-12-11 23:55:48 +01:00
|
|
|
TextStream<std::string> command;
|
2011-01-05 22:24:11 +01:00
|
|
|
TextStream<std::wstring> stream_tmp_path;
|
2011-09-13 08:08:34 +02:00
|
|
|
std::string add_tempa;
|
|
|
|
std::string input_file_name;
|
|
|
|
std::string tmp_file_name;
|
2013-03-29 23:03:28 +01:00
|
|
|
Item file_work;
|
2010-12-11 23:55:48 +01:00
|
|
|
|
|
|
|
virtual bool SignalReceived();
|
|
|
|
virtual void Do();
|
2011-01-05 22:24:11 +01:00
|
|
|
bool CreateCommand();
|
2011-09-13 08:08:34 +02:00
|
|
|
bool CreateInputFileName();
|
|
|
|
void CreateTmpFileName();
|
|
|
|
void SaveImage();
|
|
|
|
void CreateImage();
|
2013-03-29 23:03:28 +01:00
|
|
|
void SelectAspect(size_t cx, size_t cy);
|
2011-09-13 08:08:34 +02:00
|
|
|
void EscapePath(const std::string & path, TextStream<std::string> & out, bool clear_stream = true);
|
|
|
|
void CheckParam(ImageItem & item);
|
|
|
|
void Add(const std::wstring & in, TextStream<std::string> & out);
|
2013-03-29 23:03:28 +01:00
|
|
|
void ImageSavedCorrectly();
|
|
|
|
|
2010-12-11 23:55:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
} // namespace Winix
|
|
|
|
|
|
|
|
|
2010-12-11 23:55:48 +01:00
|
|
|
#endif
|