2 * Copyright (C) 2012-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/>.
21 #include "TextureCacheJob.h"
22 #include "TextureCache.h"
23 #include "guilib/Texture.h"
24 #include "guilib/DDSImage.h"
25 #include "settings/AdvancedSettings.h"
26 #include "settings/Settings.h"
27 #include "utils/log.h"
28 #include "filesystem/File.h"
29 #include "pictures/Picture.h"
30 #include "utils/URIUtils.h"
31 #include "utils/StringUtils.h"
34 #include "music/MusicThumbLoader.h"
35 #include "music/tags/MusicInfoTag.h"
36 #if defined(HAS_OMXPLAYER)
37 #include "cores/omxplayer/OMXImage.h"
40 CTextureCacheJob::CTextureCacheJob(const CStdString &url, const CStdString &oldHash)
44 m_cachePath = CTextureCache::GetCacheFile(m_url);
47 CTextureCacheJob::~CTextureCacheJob()
51 bool CTextureCacheJob::operator==(const CJob* job) const
53 if (strcmp(job->GetType(),GetType()) == 0)
55 const CTextureCacheJob* cacheJob = dynamic_cast<const CTextureCacheJob*>(job);
56 if (cacheJob && cacheJob->m_cachePath == m_cachePath)
62 bool CTextureCacheJob::DoWork()
64 if (ShouldCancel(0, 0))
66 if (ShouldCancel(1, 0)) // HACK: second check is because we cancel the job in the first callback, but we don't detect it
67 return false; // until the second
69 // check whether we need cache the job anyway
70 bool needsRecaching = false;
71 CStdString path(CTextureCache::Get().CheckCachedImage(m_url, false, needsRecaching));
72 if (!path.empty() && !needsRecaching)
74 return CacheTexture();
77 bool CTextureCacheJob::CacheTexture(CBaseTexture **out_texture)
79 // unwrap the URL as required
80 std::string additional_info;
81 unsigned int width, height;
82 CStdString image = DecodeImageURL(m_url, width, height, additional_info);
84 m_details.updateable = additional_info != "music" && UpdateableURL(image);
87 m_details.hash = GetImageHash(image);
88 if (m_details.hash.empty())
90 else if (m_details.hash == m_oldHash)
93 #if defined(HAS_OMXPLAYER)
94 if (COMXImage::CreateThumb(image, width, height, additional_info, CTextureCache::GetCachedPath(m_cachePath + ".jpg")))
96 m_details.width = width;
97 m_details.height = height;
98 m_details.file = m_cachePath + ".jpg";
100 *out_texture = LoadImage(CTextureCache::GetCachedPath(m_details.file), width, height, additional_info);
101 CLog::Log(LOGDEBUG, "Fast %s image '%s' to '%s': %p", m_oldHash.empty() ? "Caching" : "Recaching", image.c_str(), m_details.file.c_str(), out_texture);
105 CBaseTexture *texture = LoadImage(image, width, height, additional_info, true);
108 if (texture->HasAlpha())
109 m_details.file = m_cachePath + ".png";
111 m_details.file = m_cachePath + ".jpg";
113 CLog::Log(LOGDEBUG, "%s image '%s' to '%s':", m_oldHash.empty() ? "Caching" : "Recaching", image.c_str(), m_details.file.c_str());
115 if (CPicture::CacheTexture(texture, width, height, CTextureCache::GetCachedPath(m_details.file)))
117 m_details.width = width;
118 m_details.height = height;
119 if (out_texture) // caller wants the texture
120 *out_texture = texture;
130 CStdString CTextureCacheJob::DecodeImageURL(const CStdString &url, unsigned int &width, unsigned int &height, std::string &additional_info)
132 // unwrap the URL as required
133 CStdString image(url);
134 additional_info.clear();
136 if (StringUtils::StartsWith(url, "image://"))
138 // format is image://[type@]<url_encoded_path>?options
141 if (!CTextureCache::CanCacheImageURL(thumbURL))
143 if (thumbURL.GetUserName() == "music")
144 additional_info = "music";
146 image = thumbURL.GetHostName();
148 CStdString optionString = thumbURL.GetOptions().empty() ? "" : thumbURL.GetOptions().substr(1);
149 StringUtils::TrimRight(optionString, "/"); // In case XBMC adds a slash.
151 std::vector<CStdString> options;
152 StringUtils::SplitString(optionString, "&", options);
153 for (std::vector<CStdString>::iterator i = options.begin(); i != options.end(); i++)
155 CStdString option, value;
156 size_t pos = i->find('=');
157 if (pos != std::string::npos)
159 option = i->substr(0, pos);
160 value = i->substr(pos + 1);
167 if (option == "size" && value == "thumb")
169 width = height = g_advancedSettings.GetThumbSize();
171 else if (option == "flipped")
173 additional_info = "flipped";
180 CBaseTexture *CTextureCacheJob::LoadImage(const CStdString &image, unsigned int width, unsigned int height, const std::string &additional_info, bool requirePixels)
182 if (additional_info == "music")
183 { // special case for embedded music images
184 MUSIC_INFO::EmbeddedArt art;
185 if (CMusicThumbLoader::GetEmbeddedThumb(image, art))
186 return CBaseTexture::LoadFromFileInMemory(&art.data[0], art.size, art.mime, width, height);
189 // Validate file URL to see if it is an image
190 CFileItem file(image, false);
191 file.FillInMimeType();
192 if (!(file.IsPicture() && !(file.IsZIP() || file.IsRAR() || file.IsCBR() || file.IsCBZ() ))
193 && !StringUtils::StartsWithNoCase(file.GetMimeType(), "image/") && !file.GetMimeType().Equals("application/octet-stream")) // ignore non-pictures
196 CBaseTexture *texture = CBaseTexture::LoadFromFile(image, width, height, CSettings::Get().GetBool("pictures.useexifrotation"), requirePixels, file.GetMimeType());
200 // EXIF bits are interpreted as: <flipXY><flipY*flipX><flipX>
201 // where to undo the operation we apply them in reverse order <flipX>*<flipY*flipX>*<flipXY>
202 // When flipped we have an additional <flipX> on the left, which is equivalent to toggling the last bit
203 if (additional_info == "flipped")
204 texture->SetOrientation(texture->GetOrientation() ^ 1);
209 bool CTextureCacheJob::UpdateableURL(const CStdString &url) const
211 // we don't constantly check online images
212 if (StringUtils::StartsWith(url, "http://") ||
213 StringUtils::StartsWith(url, "https://"))
218 CStdString CTextureCacheJob::GetImageHash(const CStdString &url)
221 if (XFILE::CFile::Stat(url, &st) == 0)
223 int64_t time = st.st_mtime;
226 if (time || st.st_size)
227 return StringUtils::Format("d%"PRId64"s%"PRId64, time, st.st_size);;
230 CLog::Log(LOGDEBUG, "%s - unable to stat url %s", __FUNCTION__, url.c_str());
234 CTextureDDSJob::CTextureDDSJob(const CStdString &original)
236 m_original = original;
239 bool CTextureDDSJob::operator==(const CJob* job) const
241 if (strcmp(job->GetType(),GetType()) == 0)
243 const CTextureDDSJob* ddsJob = dynamic_cast<const CTextureDDSJob*>(job);
244 if (ddsJob && ddsJob->m_original == m_original)
250 bool CTextureDDSJob::DoWork()
252 if (URIUtils::HasExtension(m_original, ".dds"))
254 CBaseTexture *texture = CBaseTexture::LoadFromFile(m_original);
258 CLog::Log(LOGDEBUG, "Creating DDS version of: %s", m_original.c_str());
259 bool ret = dds.Create(URIUtils::ReplaceExtension(m_original, ".dds"), texture->GetWidth(), texture->GetHeight(), texture->GetPitch(), texture->GetPixels(), 40);
266 CTextureUseCountJob::CTextureUseCountJob(const std::vector<CTextureDetails> &textures) : m_textures(textures)
270 bool CTextureUseCountJob::operator==(const CJob* job) const
272 if (strcmp(job->GetType(),GetType()) == 0)
274 const CTextureUseCountJob* useJob = dynamic_cast<const CTextureUseCountJob*>(job);
275 if (useJob && useJob->m_textures == m_textures)
281 bool CTextureUseCountJob::DoWork()
286 db.BeginTransaction();
287 for (std::vector<CTextureDetails>::const_iterator i = m_textures.begin(); i != m_textures.end(); ++i)
288 db.IncrementUseCount(*i);
289 db.CommitTransaction();