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"
37 CTextureCacheJob::CTextureCacheJob(const CStdString &url, const CStdString &oldHash)
41 m_cachePath = CTextureCache::GetCacheFile(m_url);
44 CTextureCacheJob::~CTextureCacheJob()
48 bool CTextureCacheJob::operator==(const CJob* job) const
50 if (strcmp(job->GetType(),GetType()) == 0)
52 const CTextureCacheJob* cacheJob = dynamic_cast<const CTextureCacheJob*>(job);
53 if (cacheJob && cacheJob->m_cachePath == m_cachePath)
59 bool CTextureCacheJob::DoWork()
61 if (ShouldCancel(0, 0))
63 if (ShouldCancel(1, 0)) // HACK: second check is because we cancel the job in the first callback, but we don't detect it
64 return false; // until the second
66 // check whether we need cache the job anyway
67 bool needsRecaching = false;
68 CStdString path(CTextureCache::Get().CheckCachedImage(m_url, false, needsRecaching));
69 if (!path.IsEmpty() && !needsRecaching)
71 return CacheTexture();
74 bool CTextureCacheJob::CacheTexture(CBaseTexture **out_texture)
76 // unwrap the URL as required
77 std::string additional_info;
78 unsigned int width, height;
79 CStdString image = DecodeImageURL(m_url, width, height, additional_info);
81 m_details.updateable = additional_info != "music" && UpdateableURL(image);
84 m_details.hash = GetImageHash(image);
85 if (m_details.hash.empty())
87 else if (m_details.hash == m_oldHash)
90 CBaseTexture *texture = LoadImage(image, width, height, additional_info);
93 if (texture->HasAlpha())
94 m_details.file = m_cachePath + ".png";
96 m_details.file = m_cachePath + ".jpg";
98 CLog::Log(LOGDEBUG, "%s image '%s' to '%s':", m_oldHash.IsEmpty() ? "Caching" : "Recaching", image.c_str(), m_details.file.c_str());
100 if (CPicture::CacheTexture(texture, width, height, CTextureCache::GetCachedPath(m_details.file)))
102 m_details.width = width;
103 m_details.height = height;
104 if (out_texture) // caller wants the texture
105 *out_texture = texture;
115 CStdString CTextureCacheJob::DecodeImageURL(const CStdString &url, unsigned int &width, unsigned int &height, std::string &additional_info)
117 // unwrap the URL as required
118 CStdString image(url);
119 additional_info.clear();
121 if (StringUtils::StartsWith(url, "image://"))
123 // format is image://[type@]<url_encoded_path>?options
126 if (!CTextureCache::CanCacheImageURL(thumbURL))
128 if (thumbURL.GetUserName() == "music")
129 additional_info = "music";
131 image = thumbURL.GetHostName();
133 CStdString optionString = thumbURL.GetOptions().Mid(1);
134 optionString.TrimRight('/'); // in case XBMC adds a slash
136 std::vector<CStdString> options;
137 StringUtils::SplitString(optionString, "&", options);
138 for (std::vector<CStdString>::iterator i = options.begin(); i != options.end(); i++)
140 CStdString option, value;
141 int pos = i->Find('=');
144 option = i->Left(pos);
145 value = i->Mid(pos + 1);
152 if (option == "size" && value == "thumb")
154 width = height = g_advancedSettings.GetThumbSize();
156 else if (option == "flipped")
158 additional_info = "flipped";
165 CBaseTexture *CTextureCacheJob::LoadImage(const CStdString &image, unsigned int width, unsigned int height, const std::string &additional_info)
167 if (additional_info == "music")
168 { // special case for embedded music images
169 MUSIC_INFO::EmbeddedArt art;
170 if (CMusicThumbLoader::GetEmbeddedThumb(image, art))
171 return CBaseTexture::LoadFromFileInMemory(&art.data[0], art.size, art.mime, width, height);
174 // Validate file URL to see if it is an image
175 CFileItem file(image, false);
176 file.FillInMimeType();
177 if (!(file.IsPicture() && !(file.IsZIP() || file.IsRAR() || file.IsCBR() || file.IsCBZ() ))
178 && !StringUtils::StartsWithNoCase(file.GetMimeType(), "image/") && !file.GetMimeType().Equals("application/octet-stream")) // ignore non-pictures
181 CBaseTexture *texture = CBaseTexture::LoadFromFile(image, width, height, CSettings::Get().GetBool("pictures.useexifrotation"));
185 // EXIF bits are interpreted as: <flipXY><flipY*flipX><flipX>
186 // where to undo the operation we apply them in reverse order <flipX>*<flipY*flipX>*<flipXY>
187 // When flipped we have an additional <flipX> on the left, which is equivalent to toggling the last bit
188 if (additional_info == "flipped")
189 texture->SetOrientation(texture->GetOrientation() ^ 1);
194 bool CTextureCacheJob::UpdateableURL(const CStdString &url) const
196 // we don't constantly check online images
197 if (StringUtils::StartsWith(url, "http://") ||
198 StringUtils::StartsWith(url, "https://"))
203 CStdString CTextureCacheJob::GetImageHash(const CStdString &url)
206 if (XFILE::CFile::Stat(url, &st) == 0)
208 int64_t time = st.st_mtime;
211 if (time || st.st_size)
214 hash.Format("d%"PRId64"s%"PRId64, time, st.st_size);
218 CLog::Log(LOGDEBUG, "%s - unable to stat url %s", __FUNCTION__, url.c_str());
222 CTextureDDSJob::CTextureDDSJob(const CStdString &original)
224 m_original = original;
227 bool CTextureDDSJob::operator==(const CJob* job) const
229 if (strcmp(job->GetType(),GetType()) == 0)
231 const CTextureDDSJob* ddsJob = dynamic_cast<const CTextureDDSJob*>(job);
232 if (ddsJob && ddsJob->m_original == m_original)
238 bool CTextureDDSJob::DoWork()
240 if (URIUtils::HasExtension(m_original, ".dds"))
242 CBaseTexture *texture = CBaseTexture::LoadFromFile(m_original);
246 CLog::Log(LOGDEBUG, "Creating DDS version of: %s", m_original.c_str());
247 bool ret = dds.Create(URIUtils::ReplaceExtension(m_original, ".dds"), texture->GetWidth(), texture->GetHeight(), texture->GetPitch(), texture->GetPixels(), 40);
254 CTextureUseCountJob::CTextureUseCountJob(const std::vector<CTextureDetails> &textures) : m_textures(textures)
258 bool CTextureUseCountJob::operator==(const CJob* job) const
260 if (strcmp(job->GetType(),GetType()) == 0)
262 const CTextureUseCountJob* useJob = dynamic_cast<const CTextureUseCountJob*>(job);
263 if (useJob && useJob->m_textures == m_textures)
269 bool CTextureUseCountJob::DoWork()
274 db.BeginTransaction();
275 for (std::vector<CTextureDetails>::const_iterator i = m_textures.begin(); i != m_textures.end(); ++i)
276 db.IncrementUseCount(*i);
277 db.CommitTransaction();