2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
22 #if (defined HAVE_CONFIG_H) && (!defined TARGET_WINDOWS)
27 #include "settings/AdvancedSettings.h"
28 #include "settings/Settings.h"
30 #include "filesystem/File.h"
31 #include "utils/log.h"
32 #include "utils/URIUtils.h"
33 #include "DllSwScale.h"
34 #include "guilib/Texture.h"
35 #include "guilib/imagefactory.h"
36 #if defined(HAS_OMXPLAYER)
37 #include "cores/omxplayer/OMXImage.h"
40 using namespace XFILE;
42 bool CPicture::CreateThumbnailFromSurface(const unsigned char *buffer, int width, int height, int stride, const CStdString &thumbFile)
44 CLog::Log(LOGDEBUG, "cached image '%s' size %dx%d", thumbFile.c_str(), width, height);
45 if (URIUtils::HasExtension(thumbFile, ".jpg"))
47 #if defined(HAS_OMXPLAYER)
48 if (COMXImage::CreateThumbnailFromSurface((BYTE *)buffer, width, height, XB_FMT_A8R8G8B8, stride, thumbFile.c_str()))
53 unsigned char *thumb = NULL;
54 unsigned int thumbsize=0;
55 IImage* pImage = ImageFactory::CreateLoader(thumbFile);
56 if(pImage == NULL || !pImage->CreateThumbnailFromSurface((BYTE *)buffer, width, height, XB_FMT_A8R8G8B8, stride, thumbFile.c_str(), thumb, thumbsize))
58 CLog::Log(LOGERROR, "Failed to CreateThumbnailFromSurface for %s", thumbFile.c_str());
64 if (file.OpenForWrite(thumbFile, true))
66 file.Write(thumb, thumbsize);
68 pImage->ReleaseThumbnailBuffer();
72 pImage->ReleaseThumbnailBuffer();
77 CThumbnailWriter::CThumbnailWriter(unsigned char* buffer, int width, int height, int stride, const CStdString& thumbFile)
83 m_thumbFile = thumbFile;
86 bool CThumbnailWriter::DoWork()
90 if (!CPicture::CreateThumbnailFromSurface(m_buffer, m_width, m_height, m_stride, m_thumbFile))
92 CLog::Log(LOGERROR, "CThumbnailWriter::DoWork unable to write %s", m_thumbFile.c_str());
101 bool CPicture::CacheTexture(CBaseTexture *texture, uint32_t &dest_width, uint32_t &dest_height, const std::string &dest)
103 return CacheTexture(texture->GetPixels(), texture->GetWidth(), texture->GetHeight(), texture->GetPitch(),
104 texture->GetOrientation(), dest_width, dest_height, dest);
107 bool CPicture::CacheTexture(uint8_t *pixels, uint32_t width, uint32_t height, uint32_t pitch, int orientation, uint32_t &dest_width, uint32_t &dest_height, const std::string &dest)
109 // if no max width or height is specified, don't resize
112 if (dest_height == 0)
113 dest_height = height;
115 uint32_t max_height = g_advancedSettings.m_imageRes;
116 if (g_advancedSettings.m_fanartRes > g_advancedSettings.m_imageRes)
117 { // 16x9 images larger than the fanart res use that rather than the image res
118 if (fabsf((float)width / (float)height / (16.0f/9.0f) - 1.0f) <= 0.01f && height >= g_advancedSettings.m_fanartRes)
120 max_height = g_advancedSettings.m_fanartRes;
123 uint32_t max_width = max_height * 16/9;
125 dest_height = std::min(dest_height, max_height);
126 dest_width = std::min(dest_width, max_width);
128 if (width > dest_width || height > dest_height || orientation)
130 bool success = false;
132 dest_width = std::min(width, dest_width);
133 dest_height = std::min(height, dest_height);
135 // create a buffer large enough for the resulting image
136 GetScale(width, height, dest_width, dest_height);
137 uint32_t *buffer = new uint32_t[dest_width * dest_height];
140 if (ScaleImage(pixels, width, height, pitch,
141 (uint8_t *)buffer, dest_width, dest_height, dest_width * 4))
143 if (!orientation || OrientateImage(buffer, dest_width, dest_height, orientation))
145 success = CreateThumbnailFromSurface((unsigned char*)buffer, dest_width, dest_height, dest_width * 4, dest);
153 { // no orientation needed
155 dest_height = height;
156 return CreateThumbnailFromSurface(pixels, width, height, pitch, dest);
161 bool CPicture::CreateTiledThumb(const std::vector<std::string> &files, const std::string &thumb)
166 unsigned int num_across = (unsigned int)ceil(sqrt((float)files.size()));
167 unsigned int num_down = (files.size() + num_across - 1) / num_across;
169 unsigned int tile_width = g_advancedSettings.GetThumbSize() / num_across;
170 unsigned int tile_height = g_advancedSettings.GetThumbSize() / num_down;
171 unsigned int tile_gap = 1;
172 bool success = false;
174 // create a buffer for the resulting thumb
175 uint32_t *buffer = (uint32_t *)calloc(g_advancedSettings.GetThumbSize() * g_advancedSettings.GetThumbSize(), 4);
176 for (unsigned int i = 0; i < files.size(); ++i)
178 int x = i % num_across;
179 int y = i / num_across;
181 unsigned int width = tile_width - 2*tile_gap, height = tile_height - 2*tile_gap;
182 CBaseTexture *texture = CTexture::LoadFromFile(files[i], width, height, CSettings::Get().GetBool("pictures.useexifrotation"), true);
183 if (texture && texture->GetWidth() && texture->GetHeight())
185 GetScale(texture->GetWidth(), texture->GetHeight(), width, height);
187 // scale appropriately
188 uint32_t *scaled = new uint32_t[width * height];
189 if (ScaleImage(texture->GetPixels(), texture->GetWidth(), texture->GetHeight(), texture->GetPitch(),
190 (uint8_t *)scaled, width, height, width * 4))
192 if (!texture->GetOrientation() || OrientateImage(scaled, width, height, texture->GetOrientation()))
194 success = true; // Flag that we at least had one succesfull image processed
195 // drop into the texture
196 unsigned int posX = x*tile_width + (tile_width - width)/2;
197 unsigned int posY = y*tile_height + (tile_height - height)/2;
198 uint32_t *dest = buffer + posX + posY*g_advancedSettings.GetThumbSize();
199 uint32_t *src = scaled;
200 for (unsigned int y = 0; y < height; ++y)
202 memcpy(dest, src, width*4);
203 dest += g_advancedSettings.GetThumbSize();
212 // now save to a file
214 success = CreateThumbnailFromSurface((uint8_t *)buffer, g_advancedSettings.GetThumbSize(), g_advancedSettings.GetThumbSize(),
215 g_advancedSettings.GetThumbSize() * 4, thumb);
221 void CPicture::GetScale(unsigned int width, unsigned int height, unsigned int &out_width, unsigned int &out_height)
223 float aspect = (float)width / height;
224 if ((unsigned int)(out_width / aspect + 0.5f) > out_height)
225 out_width = (unsigned int)(out_height * aspect + 0.5f);
227 out_height = (unsigned int)(out_width / aspect + 0.5f);
230 bool CPicture::ScaleImage(uint8_t *in_pixels, unsigned int in_width, unsigned int in_height, unsigned int in_pitch,
231 uint8_t *out_pixels, unsigned int out_width, unsigned int out_height, unsigned int out_pitch)
233 DllSwScale dllSwScale;
235 struct SwsContext *context = dllSwScale.sws_getContext(in_width, in_height, PIX_FMT_BGRA,
236 out_width, out_height, PIX_FMT_BGRA,
237 SWS_FAST_BILINEAR | SwScaleCPUFlags(), NULL, NULL, NULL);
239 uint8_t *src[] = { in_pixels, 0, 0, 0 };
240 int srcStride[] = { (int)in_pitch, 0, 0, 0 };
241 uint8_t *dst[] = { out_pixels , 0, 0, 0 };
242 int dstStride[] = { (int)out_pitch, 0, 0, 0 };
246 dllSwScale.sws_scale(context, src, srcStride, 0, in_height, dst, dstStride);
247 dllSwScale.sws_freeContext(context);
253 bool CPicture::OrientateImage(uint32_t *&pixels, unsigned int &width, unsigned int &height, int orientation)
255 // ideas for speeding these functions up: http://cgit.freedesktop.org/pixman/tree/pixman/pixman-fast-path.c
260 out = FlipHorizontal(pixels, width, height);
263 out = Rotate180CCW(pixels, width, height);
266 out = FlipVertical(pixels, width, height);
269 out = Transpose(pixels, width, height);
272 out = Rotate270CCW(pixels, width, height);
275 out = TransposeOffAxis(pixels, width, height);
278 out = Rotate90CCW(pixels, width, height);
281 CLog::Log(LOGERROR, "Unknown orientation %i", orientation);
287 bool CPicture::FlipHorizontal(uint32_t *&pixels, unsigned int &width, unsigned int &height)
289 // this can be done in-place easily enough
290 for (unsigned int y = 0; y < height; ++y)
292 uint32_t *line = pixels + y * width;
293 for (unsigned int x = 0; x < width / 2; ++x)
294 std::swap(line[x], line[width - 1 - x]);
299 bool CPicture::FlipVertical(uint32_t *&pixels, unsigned int &width, unsigned int &height)
301 // this can be done in-place easily enough
302 for (unsigned int y = 0; y < height / 2; ++y)
304 uint32_t *line1 = pixels + y * width;
305 uint32_t *line2 = pixels + (height - 1 - y) * width;
306 for (unsigned int x = 0; x < width; ++x)
307 std::swap(*line1++, *line2++);
312 bool CPicture::Rotate180CCW(uint32_t *&pixels, unsigned int &width, unsigned int &height)
314 // this can be done in-place easily enough
315 for (unsigned int y = 0; y < height / 2; ++y)
317 uint32_t *line1 = pixels + y * width;
318 uint32_t *line2 = pixels + (height - 1 - y) * width + width - 1;
319 for (unsigned int x = 0; x < width; ++x)
320 std::swap(*line1++, *line2--);
323 { // height is odd, so flip the middle row as well
324 uint32_t *line = pixels + (height - 1)/2 * width;
325 for (unsigned int x = 0; x < width / 2; ++x)
326 std::swap(line[x], line[width - 1 - x]);
331 bool CPicture::Rotate90CCW(uint32_t *&pixels, unsigned int &width, unsigned int &height)
333 uint32_t *dest = new uint32_t[width * height * 4];
336 unsigned int d_height = width, d_width = height;
337 for (unsigned int y = 0; y < d_height; y++)
339 const uint32_t *src = pixels + (d_height - 1 - y); // y-th col from right, starting at top
340 uint32_t *dst = dest + d_width * y; // y-th row from top, starting at left
341 for (unsigned int x = 0; x < d_width; x++)
349 std::swap(width, height);
355 bool CPicture::Rotate270CCW(uint32_t *&pixels, unsigned int &width, unsigned int &height)
357 uint32_t *dest = new uint32_t[width * height * 4];
361 unsigned int d_height = width, d_width = height;
362 for (unsigned int y = 0; y < d_height; y++)
364 const uint32_t *src = pixels + width * (d_width - 1) + y; // y-th col from left, starting at bottom
365 uint32_t *dst = dest + d_width * y; // y-th row from top, starting at left
366 for (unsigned int x = 0; x < d_width; x++)
375 std::swap(width, height);
379 bool CPicture::Transpose(uint32_t *&pixels, unsigned int &width, unsigned int &height)
381 uint32_t *dest = new uint32_t[width * height * 4];
385 unsigned int d_height = width, d_width = height;
386 for (unsigned int y = 0; y < d_height; y++)
388 const uint32_t *src = pixels + y; // y-th col from left, starting at top
389 uint32_t *dst = dest + d_width * y; // y-th row from top, starting at left
390 for (unsigned int x = 0; x < d_width; x++)
399 std::swap(width, height);
403 bool CPicture::TransposeOffAxis(uint32_t *&pixels, unsigned int &width, unsigned int &height)
405 uint32_t *dest = new uint32_t[width * height * 4];
409 unsigned int d_height = width, d_width = height;
410 for (unsigned int y = 0; y < d_height; y++)
412 const uint32_t *src = pixels + width * (d_width - 1) + (d_height - 1 - y); // y-th col from right, starting at bottom
413 uint32_t *dst = dest + d_width * y; // y-th row, starting at left
414 for (unsigned int x = 0; x < d_width; x++)
423 std::swap(width, height);