1/*
2
3Image Cache.
4
5Copyright (c) 2003 OpenBeOS.
6
7Author:
8	Michael Pfeiffer
9
10Permission is hereby granted, free of charge, to any person obtaining a copy of
11this software and associated documentation files (the "Software"), to deal in
12the Software without restriction, including without limitation the rights to
13use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14of the Software, and to permit persons to whom the Software is furnished to do
15so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26THE SOFTWARE.
27
28*/
29
30#include <unistd.h>
31#include <Debug.h>
32#include <OS.h>
33
34#include "Report.h"
35#include "ImageCache.h"
36#include "Image.h"
37#include "Mask.h"
38
39const char* kTemporaryPath =   "/tmp/PDFWriter";
40const char* kCachePath =  NULL;
41const char* kImagePathPrefix = NULL;
42const char* kMaskPathPrefix = NULL;
43
44// Implementation of ImageCache
45
46ImageCache::ImageCache()
47{
48	BString path(kTemporaryPath);
49	path << "/Cache" << (int)find_thread(NULL);
50	kCachePath = strdup(path.String());
51	BString path2(path);
52	path += "/Image";
53	kImagePathPrefix = strdup(path.String());
54	path2 += "/Mask";
55	kMaskPathPrefix = strdup(path2.String());
56	mkdir(kTemporaryPath, 0777);
57	mkdir(kCachePath, 0777);
58}
59
60ImageCache::~ImageCache() {
61	rmdir(kCachePath);
62	free((void*)kCachePath);
63	free((void*)kImagePathPrefix);
64	free((void*)kMaskPathPrefix);
65}
66
67void ImageCache::Flush() {
68	fImageCache.MakeEmpty();
69	fMaskCache.MakeEmpty();
70}
71
72void ImageCache::NextPass() {
73	fImageCache.NextPass();
74	fMaskCache.NextPass();
75}
76
77int ImageCache::GetImage(PDF* pdf, BBitmap* bitmap, int mask) {
78	ImageDescription desc(pdf, bitmap, mask);
79	CacheItem* item = fImageCache.Find(&desc);
80	Image* image = dynamic_cast<Image*>(item);
81	if (image) {
82		return image->ImageID();
83	}
84	REPORT(kError, -1, "Image cache could not find image. Please make sure to have enough disk space available.");
85	return -1;
86}
87
88int ImageCache::GetMask(PDF* pdf, const char* mask, int length, int width, int height, int bpc) {
89	MaskDescription desc(pdf, mask, length, width, height, bpc);
90	CacheItem* item = fMaskCache.Find(&desc);
91	Mask* image = dynamic_cast<Mask*>(item);
92	if (image) {
93		return image->ImageID();
94	}
95	REPORT(kError, -1, "Mask cache could not find image. Please make sure to have enough disk space available.");
96	return -1;
97}
98
99
100