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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2012 Werner Schweer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#include <QtCore/QCryptographicHash>
#include "imageStore.h"
#include "score.h"
#include "image.h"
namespace Ms {
ImageStore imageStore; // the global image store
//---------------------------------------------------------
// ImageStoreItem
//---------------------------------------------------------
ImageStoreItem::ImageStoreItem(const QString& p)
{
setPath(p);
}
//---------------------------------------------------------
// dereference
// decrement usage count of image in score
//---------------------------------------------------------
void ImageStoreItem::dereference(Image* image)
{
_references.removeOne(image);
}
//---------------------------------------------------------
// reference
// increment usage count of image in score
//---------------------------------------------------------
void ImageStoreItem::reference(Image* image)
{
_references.append(image);
}
//---------------------------------------------------------
// isUsed
// check if item is used in score
//---------------------------------------------------------
bool ImageStoreItem::isUsed(Score* score) const
{
foreach(Image* image, _references) {
if (image->score() == score)
return true;
}
return false;
}
//---------------------------------------------------------
// load
//---------------------------------------------------------
void ImageStoreItem::load()
{
if (!_buffer.isEmpty())
return;
QFile inFile(_path);
if (!inFile.open(QIODevice::ReadOnly)) {
qDebug("Cannot open picture file");
return;
}
_buffer = inFile.readAll();
inFile.close();
QCryptographicHash h(QCryptographicHash::Md4);
h.addData(_buffer);
_hash = h.result();
}
//---------------------------------------------------------
// hashName
//---------------------------------------------------------
QString ImageStoreItem::hashName() const
{
const char hex[17] = "0123456789abcdef";
char p[33];
for (int i = 0; i < 16; ++i) {
p[i * 2] = hex[(_hash[i] >> 4) & 0xf];
p[i * 2 + 1] = hex[_hash[i] & 0xf];
}
p[32] = 0;
return QString(p) + "." + _type;
}
//---------------------------------------------------------
// setPath
//---------------------------------------------------------
void ImageStoreItem::setPath(const QString& val)
{
_path = val;
QFileInfo fi(_path);
_type = fi.suffix();
}
//---------------------------------------------------------
// toInt
//---------------------------------------------------------
inline static int toInt(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
return c - 'a' + 10;
}
#if 0
//---------------------------------------------------------
// dumpHash
//---------------------------------------------------------
static void dumpHash(const QByteArray& _hash)
{
const char hex[17] = "0123456789abcdef";
char p[33];
for (int i = 0; i < 16; ++i) {
p[i * 2] = hex[(_hash[i] >> 4) & 0xf];
p[i * 2 + 1] = hex[_hash[i] & 0xf];
}
p[32] = 0;
qDebug(" <%s>", p);
}
#endif
//---------------------------------------------------------
// getImage
//---------------------------------------------------------
ImageStoreItem* ImageStore::getImage(const QString& path) const
{
QString s = QFileInfo(path).completeBaseName();
if (s.size() != 32) {
//
// some limited support for backward compatibility
//
foreach(ImageStoreItem* item, *this) {
if (item->path() == path)
return item;
}
qDebug("ImageStore::getImage(%s): bad base name <%s>",
qPrintable(path), qPrintable(s));
for (ImageStoreItem* item : *this)
qDebug(" in store: <%s>", qPrintable(item->path()));
return 0;
}
QByteArray hash(16, 0);
for (int i = 0; i < 16; ++i) {
hash[i] = toInt(s[i * 2].toLatin1()) * 16 + toInt(s[i * 2 + 1].toLatin1());
}
foreach(ImageStoreItem* item, *this) {
if (item->hash() == hash)
return item;
}
qDebug("ImageStore::getImage(): not found <%s>", qPrintable(path));
return 0;
}
//---------------------------------------------------------
// add
//---------------------------------------------------------
ImageStoreItem* ImageStore::add(const QString& path, const QByteArray& ba)
{
QCryptographicHash h(QCryptographicHash::Md4);
h.addData(ba);
QByteArray hash = h.result();
foreach(ImageStoreItem* item, *this) {
if (item->hash() == hash)
return item;
}
ImageStoreItem* item = new ImageStoreItem(path);
item->set(ba, hash);
append(item);
return item;
}
}
|