Added cache for masks.
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3894 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
136966335a
commit
281b14ed54
94
src/add-ons/print/drivers/pdf/source/Cache.cpp
Normal file
94
src/add-ons/print/drivers/pdf/source/Cache.cpp
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
"Special" Cache.
|
||||||
|
|
||||||
|
Copyright (c) 2003 OpenBeOS.
|
||||||
|
|
||||||
|
Author:
|
||||||
|
Michael Pfeiffer
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Report.h"
|
||||||
|
#include "Cache.h"
|
||||||
|
#include <Debug.h>
|
||||||
|
|
||||||
|
class CIReference : public CacheItem {
|
||||||
|
public:
|
||||||
|
CIReference(CacheItem* item) : fItem(item) {
|
||||||
|
ASSERT(dynamic_cast<CIReference*>(item) == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Equals(CIDescription* desc) const {
|
||||||
|
return fItem->Equals(desc);
|
||||||
|
}
|
||||||
|
|
||||||
|
CacheItem* Reference() {
|
||||||
|
return fItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
CacheItem* fItem;
|
||||||
|
};
|
||||||
|
|
||||||
|
Cache::Cache()
|
||||||
|
: fNextID(0)
|
||||||
|
, fPass(0)
|
||||||
|
{};
|
||||||
|
|
||||||
|
void Cache::NextPass() {
|
||||||
|
fPass++;
|
||||||
|
fNextID = 0;
|
||||||
|
// max two passes!
|
||||||
|
ASSERT(fPass == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
CacheItem* Cache::Find(CIDescription* desc) {
|
||||||
|
REPORT(kDebug, -1, "Cache::Find() pass = %d next id = %d", fPass, fNextID);
|
||||||
|
int id = fNextID ++;
|
||||||
|
const int32 n = CountItems();
|
||||||
|
CacheItem* item = ItemAt(id);
|
||||||
|
|
||||||
|
// In 2. pass for each item an entry must exists
|
||||||
|
ASSERT(fPass == 1 && item != NULL);
|
||||||
|
if (fPass == 1) return item->Reference();
|
||||||
|
|
||||||
|
// In 1. pass we create an entry for each bitmap
|
||||||
|
for (int32 i = 0; i < n; i ++) {
|
||||||
|
item = ItemAt(i);
|
||||||
|
// skip references
|
||||||
|
if (item != item->Reference()) continue;
|
||||||
|
if (item->Equals(desc)) {
|
||||||
|
// found item in cache, create a reference to it
|
||||||
|
CacheItem* ref = new CIReference(item->Reference());
|
||||||
|
if (ref == NULL) return NULL;
|
||||||
|
fCache.AddItem(ref);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// item not in cache, create one
|
||||||
|
item = desc->NewItem(id);
|
||||||
|
if (item != NULL) {
|
||||||
|
ASSERT(dynamic_cast<CacheItemReference*>(item) == NULL);
|
||||||
|
fCache.AddItem(item);
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
}
|
75
src/add-ons/print/drivers/pdf/source/Cache.h
Normal file
75
src/add-ons/print/drivers/pdf/source/Cache.h
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
"Special" Cache.
|
||||||
|
|
||||||
|
Copyright (c) 2003 OpenBeOS.
|
||||||
|
|
||||||
|
Author:
|
||||||
|
Michael Pfeiffer
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _CACHE_H
|
||||||
|
#define _CACHE_H
|
||||||
|
|
||||||
|
#include "Utils.h"
|
||||||
|
|
||||||
|
class CacheItem;
|
||||||
|
class CacheItemReference;
|
||||||
|
|
||||||
|
// CacheItemDescription
|
||||||
|
class CIDescription {
|
||||||
|
public:
|
||||||
|
CIDescription() {};
|
||||||
|
virtual ~CIDescription() {};
|
||||||
|
|
||||||
|
virtual CacheItem* NewItem(int id) { return NULL; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class CacheItem {
|
||||||
|
public:
|
||||||
|
CacheItem() {}
|
||||||
|
virtual ~CacheItem() {}
|
||||||
|
|
||||||
|
virtual bool Equals(CIDescription* desc) const = 0;
|
||||||
|
virtual CacheItem* Reference() { return this; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class Cache {
|
||||||
|
public:
|
||||||
|
Cache();
|
||||||
|
virtual ~Cache() {};
|
||||||
|
|
||||||
|
void NextPass();
|
||||||
|
|
||||||
|
// returns the CacheItem at "NextID", returns NULL on error
|
||||||
|
CacheItem* Find(CIDescription* desc);
|
||||||
|
void MakeEmpty() { fCache.MakeEmpty(); }
|
||||||
|
int32 CountItems() const { return fCache.CountItems(); }
|
||||||
|
CacheItem* ItemAt(int32 i) const { return fCache.ItemAt(i); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int8 fPass;
|
||||||
|
int32 fNextID;
|
||||||
|
TList<CacheItem> fCache;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -1,156 +1,99 @@
|
|||||||
#include<unistd.h>
|
/*
|
||||||
#include<sys/stat.h>
|
|
||||||
#include<Translator.h>
|
|
||||||
#include<TranslationUtils.h>
|
|
||||||
#include<TranslatorRoster.h>
|
|
||||||
#include<BitmapStream.h>
|
|
||||||
#include<File.h>
|
|
||||||
#include<Debug.h>
|
|
||||||
|
|
||||||
|
Image Cache.
|
||||||
|
|
||||||
|
Copyright (c) 2003 OpenBeOS.
|
||||||
|
|
||||||
|
Author:
|
||||||
|
Michael Pfeiffer
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <Debug.h>
|
||||||
|
#include <OS.h>
|
||||||
|
|
||||||
|
#include "Report.h"
|
||||||
#include "ImageCache.h"
|
#include "ImageCache.h"
|
||||||
|
#include "Image.h"
|
||||||
|
#include "Mask.h"
|
||||||
|
|
||||||
|
const char* kTemporaryPath = "/tmp/PDFWriter";
|
||||||
// Implementation of Image
|
const char* kCachePath = NULL;
|
||||||
|
const char* kImagePathPrefix = NULL;
|
||||||
Image::Image(int imageID, const char* fileName, int width, int height, color_space colorSpace, int mask)
|
const char* kMaskPathPrefix = NULL;
|
||||||
: fImageID(imageID)
|
|
||||||
, fFileName(fileName)
|
|
||||||
, fWidth(width)
|
|
||||||
, fHeight(height)
|
|
||||||
, fColorSpace(colorSpace)
|
|
||||||
, fMask(mask)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Image::Equals(BBitmap* bitmap) const {
|
|
||||||
BBitmap* bm = BTranslationUtils::GetBitmapFile(FileName());
|
|
||||||
bool equals = false;
|
|
||||||
if (bm) {
|
|
||||||
bm->Lock();
|
|
||||||
bitmap->Lock();
|
|
||||||
|
|
||||||
equals = bm->BitsLength() == bitmap->BitsLength() &&
|
|
||||||
bm->ColorSpace() == bitmap->ColorSpace() &&
|
|
||||||
memcmp(bm->Bits(), bitmap->Bits(), bm->BitsLength()) == 0;
|
|
||||||
|
|
||||||
bitmap->Unlock();
|
|
||||||
bm->Unlock();
|
|
||||||
delete bm;
|
|
||||||
}
|
|
||||||
return equals;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Implementation of ImageReference
|
|
||||||
|
|
||||||
ImageReference::ImageReference(Image* image)
|
|
||||||
: fImage(image)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Implementation of ImageCache
|
// Implementation of ImageCache
|
||||||
|
|
||||||
const char* kTemporaryPath = "/tmp/PDFWriter";
|
|
||||||
const char* kImageCachePath = "/tmp/PDFWriter/Cache";
|
|
||||||
const char* kImagePathPrefix = "/tmp/PDFWriter/Cache/Image";
|
|
||||||
|
|
||||||
ImageCache::ImageCache()
|
ImageCache::ImageCache()
|
||||||
: fNextID(0)
|
|
||||||
{
|
{
|
||||||
|
BString path(kTemporaryPath);
|
||||||
|
path << "/Cache" << (int)find_thread(NULL);
|
||||||
|
kCachePath = strdup(path.String());
|
||||||
|
BString path2(path);
|
||||||
|
path += "/Image";
|
||||||
|
kImagePathPrefix = strdup(path.String());
|
||||||
|
path2 += "/Mask";
|
||||||
|
kMaskPathPrefix = strdup(path2.String());
|
||||||
mkdir(kTemporaryPath, 0777);
|
mkdir(kTemporaryPath, 0777);
|
||||||
mkdir(kImageCachePath, 0777);
|
mkdir(kCachePath, 0777);
|
||||||
}
|
}
|
||||||
|
|
||||||
ImageCache::~ImageCache() {
|
ImageCache::~ImageCache() {
|
||||||
rmdir(kImageCachePath);
|
rmdir(kCachePath);
|
||||||
|
free((void*)kCachePath);
|
||||||
|
free((void*)kImagePathPrefix);
|
||||||
|
free((void*)kMaskPathPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImageCache::Flush(PDF* pdf) {
|
void ImageCache::Flush() {
|
||||||
for (int32 i = 0; i < fCache.CountItems(); i ++) {
|
fImageCache.MakeEmpty();
|
||||||
CachedImage* image = fCache.ItemAt(i);
|
fMaskCache.MakeEmpty();
|
||||||
if (dynamic_cast<Image*>(image) != NULL) {
|
}
|
||||||
PDF_close_image(pdf, image->ImageID());
|
|
||||||
unlink(image->FileName());
|
void ImageCache::NextPass() {
|
||||||
}
|
fImageCache.NextPass();
|
||||||
}
|
fMaskCache.NextPass();
|
||||||
}
|
}
|
||||||
|
|
||||||
int ImageCache::GetImage(PDF* pdf, BBitmap* bitmap, int mask) {
|
int ImageCache::GetImage(PDF* pdf, BBitmap* bitmap, int mask) {
|
||||||
int id = fNextID ++;
|
ImageDescription desc(pdf, bitmap, mask);
|
||||||
CachedImage* image = fCache.ItemAt(id);
|
CacheItem* item = fImageCache.Find(&desc);
|
||||||
// In 2. pass for each bitmap an entry exists
|
Image* image = dynamic_cast<Image*>(item);
|
||||||
if (image != NULL) {
|
if (image) {
|
||||||
return image->ImageID();
|
return image->ImageID();
|
||||||
}
|
}
|
||||||
// In 1. pass we create an entry for each bitmap
|
REPORT(kError, -1, "Image cache could not find image. Please make sure to have enough disk space available.");
|
||||||
image = Find(bitmap, mask);
|
return -1;
|
||||||
// Bitmap not in cache
|
}
|
||||||
if (image == NULL) {
|
|
||||||
image = Store(pdf, id, bitmap, mask);
|
int ImageCache::GetMask(PDF* pdf, const char* mask, int length, int width, int height, int bpc) {
|
||||||
} else {
|
MaskDescription desc(pdf, mask, length, width, height, bpc);
|
||||||
Image* im = dynamic_cast<Image*>(image);
|
CacheItem* item = fMaskCache.Find(&desc);
|
||||||
ASSERT(im != NULL);
|
Mask* image = dynamic_cast<Mask*>(item);
|
||||||
image = new ImageReference(im);
|
if (image) {
|
||||||
|
return image->ImageID();
|
||||||
}
|
}
|
||||||
|
REPORT(kError, -1, "Mask cache could not find image. Please make sure to have enough disk space available.");
|
||||||
if (image == NULL) return -1; // error occured
|
return -1;
|
||||||
|
|
||||||
fCache.AddItem(image);
|
|
||||||
ASSERT(fCache.CountItems() == fNextID);
|
|
||||||
return image->ImageID();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CachedImage* ImageCache::Find(BBitmap* bitmap, int mask) {
|
|
||||||
int w, h;
|
|
||||||
color_space cs;
|
|
||||||
bitmap->Lock();
|
|
||||||
w = bitmap->Bounds().IntegerWidth()+1;
|
|
||||||
h = bitmap->Bounds().IntegerHeight()+1;
|
|
||||||
cs = bitmap->ColorSpace();
|
|
||||||
bitmap->Unlock();
|
|
||||||
|
|
||||||
for (int32 i = 0; i < fCache.CountItems(); i ++) {
|
|
||||||
CachedImage* image = fCache.ItemAt(i);
|
|
||||||
if (dynamic_cast<ImageReference*>(image) != NULL) continue;
|
|
||||||
if (w != image->Width() || h != image->Height() ||
|
|
||||||
cs != image->ColorSpace() ||
|
|
||||||
mask != image->Mask()) continue;
|
|
||||||
if (image->Equals(bitmap)) {
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
CachedImage* ImageCache::Store(PDF* pdf, int id, BBitmap* bitmap, int mask) {
|
|
||||||
BString fileName(kImagePathPrefix);
|
|
||||||
fileName << id << ".png";
|
|
||||||
int w, h;
|
|
||||||
color_space cs;
|
|
||||||
bitmap->Lock();
|
|
||||||
w = bitmap->Bounds().IntegerWidth()+1;
|
|
||||||
h = bitmap->Bounds().IntegerHeight()+1;
|
|
||||||
cs = bitmap->ColorSpace();
|
|
||||||
bitmap->Unlock();
|
|
||||||
|
|
||||||
if (!StoreBitmap(fileName.String(), bitmap)) return NULL;
|
|
||||||
|
|
||||||
int image;
|
|
||||||
image = PDF_open_image_file(pdf, "png", fileName.String(),
|
|
||||||
mask == -1 ? "" : "masked", mask == -1 ? 0 : mask);
|
|
||||||
if (image < 0) return NULL;
|
|
||||||
|
|
||||||
return new Image(image, fileName.String(), w, h, cs, mask);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ImageCache::StoreBitmap(const char* fileName, BBitmap* bitmap) {
|
|
||||||
bool ok;
|
|
||||||
BTranslatorRoster *roster = BTranslatorRoster::Default();
|
|
||||||
BBitmapStream stream(bitmap); // init with contents of bitmap
|
|
||||||
BFile file(fileName, B_CREATE_FILE | B_WRITE_ONLY | B_ERASE_FILE);
|
|
||||||
ok = roster->Translate(&stream, NULL, NULL, &file, B_PNG_FORMAT) == B_OK;
|
|
||||||
BBitmap *bm = NULL; stream.DetachBitmap(&bm); // otherwise bitmap destructor crashes here!
|
|
||||||
ASSERT(bm == bitmap);
|
|
||||||
return ok;
|
|
||||||
}
|
|
@ -1,3 +1,32 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Image Cache.
|
||||||
|
|
||||||
|
Copyright (c) 2003 OpenBeOS.
|
||||||
|
|
||||||
|
Author:
|
||||||
|
Michael Pfeiffer
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef _IMAGE_CACHE_H
|
#ifndef _IMAGE_CACHE_H
|
||||||
#define _IMAGE_CACHE_H
|
#define _IMAGE_CACHE_H
|
||||||
|
|
||||||
@ -7,76 +36,28 @@
|
|||||||
|
|
||||||
#include "pdflib.h"
|
#include "pdflib.h"
|
||||||
#include "Utils.h"
|
#include "Utils.h"
|
||||||
|
#include "Cache.h"
|
||||||
|
|
||||||
/*
|
extern const char* kTemporaryPath;
|
||||||
|
extern const char* kCachePath;
|
||||||
*/
|
extern const char* kImagePathPrefix;
|
||||||
|
extern const char* kMaskPathPrefix;
|
||||||
|
|
||||||
class CachedImage {
|
#define STORE_AS_BBITMAP 1
|
||||||
public:
|
|
||||||
CachedImage() {};
|
|
||||||
virtual ~CachedImage() {};
|
|
||||||
virtual int ImageID() const = 0;
|
|
||||||
virtual const char* FileName() const = 0;
|
|
||||||
virtual int Width() const = 0;
|
|
||||||
virtual int Height() const = 0;
|
|
||||||
virtual color_space ColorSpace() const = 0;
|
|
||||||
virtual int Mask() const = 0;
|
|
||||||
virtual bool Equals(BBitmap* bitmap) const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Image : public CachedImage {
|
|
||||||
public:
|
|
||||||
Image(int imageID, const char* fileName, int width, int height, color_space colorSpace, int mask);
|
|
||||||
|
|
||||||
int ImageID() const { return fImageID; };
|
|
||||||
const char* FileName() const { return fFileName.String(); };
|
|
||||||
int Width() const { return fWidth; };
|
|
||||||
int Height() const { return fHeight; };
|
|
||||||
color_space ColorSpace() const { return fColorSpace; };
|
|
||||||
int Mask() const { return fMask; };
|
|
||||||
bool Equals(BBitmap* bitmap) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
int fImageID;
|
|
||||||
BString fFileName;
|
|
||||||
int fWidth, fHeight;
|
|
||||||
color_space fColorSpace;
|
|
||||||
int fMask;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ImageReference : public CachedImage {
|
|
||||||
public:
|
|
||||||
ImageReference(Image* image);
|
|
||||||
|
|
||||||
int ImageID() const { return fImage->ImageID(); };
|
|
||||||
const char* FileName() const { return fImage->FileName(); };
|
|
||||||
int Width() const { return fImage->Width(); };
|
|
||||||
int Height() const { return fImage->Height(); };
|
|
||||||
color_space ColorSpace() const { return fImage->ColorSpace(); };
|
|
||||||
int Mask() const { return fImage->Mask(); };
|
|
||||||
bool Equals(BBitmap* bitmap) const { return fImage->Equals(bitmap); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
Image* fImage;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ImageCache {
|
class ImageCache {
|
||||||
public:
|
public:
|
||||||
ImageCache();
|
ImageCache();
|
||||||
~ImageCache();
|
~ImageCache();
|
||||||
void Flush(PDF* pdf);
|
void Flush();
|
||||||
|
|
||||||
void ResetID() { fNextID = 0; }
|
void NextPass();
|
||||||
int GetImage(PDF* pdf, BBitmap* bitmap, int mask);
|
int GetImage(PDF* pdf, BBitmap* bitmap, int mask);
|
||||||
|
int GetMask(PDF* pdf, const char* mask, int length, int width, int height, int bpc);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CachedImage* Find(BBitmap* bitmap, int mask);
|
Cache fImageCache;
|
||||||
CachedImage* Store(PDF* pdf, int id, BBitmap* bitmap, int mask);
|
Cache fMaskCache;
|
||||||
bool StoreBitmap(const char* fileName, BBitmap* bitmap);
|
|
||||||
|
|
||||||
int fNextID;
|
|
||||||
TList<CachedImage> fCache;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -10,17 +10,20 @@ Addon
|
|||||||
AdvancedSettingsWindow.cpp
|
AdvancedSettingsWindow.cpp
|
||||||
Bezier.cpp
|
Bezier.cpp
|
||||||
Bookmark.cpp
|
Bookmark.cpp
|
||||||
|
Cache.cpp
|
||||||
DocInfoWindow.cpp
|
DocInfoWindow.cpp
|
||||||
DrawShape.cpp
|
DrawShape.cpp
|
||||||
Driver.cpp
|
Driver.cpp
|
||||||
Fonts.cpp
|
Fonts.cpp
|
||||||
FontsWindow.cpp
|
FontsWindow.cpp
|
||||||
|
Image.cpp
|
||||||
ImageCache.cpp
|
ImageCache.cpp
|
||||||
InterfaceUtils.cpp
|
InterfaceUtils.cpp
|
||||||
JobSetupWindow.cpp
|
JobSetupWindow.cpp
|
||||||
LinePathBuilder.cpp
|
LinePathBuilder.cpp
|
||||||
Link.cpp
|
Link.cpp
|
||||||
MarginView.cpp
|
MarginView.cpp
|
||||||
|
Mask.cpp
|
||||||
PDFLinePathBuilder.cpp
|
PDFLinePathBuilder.cpp
|
||||||
PDFText.cpp
|
PDFText.cpp
|
||||||
PDFWriter.cpp
|
PDFWriter.cpp
|
||||||
|
133
src/add-ons/print/drivers/pdf/source/Mask.cpp
Normal file
133
src/add-ons/print/drivers/pdf/source/Mask.cpp
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Mask Cache Item.
|
||||||
|
|
||||||
|
Copyright (c) 2003 OpenBeOS.
|
||||||
|
|
||||||
|
Author:
|
||||||
|
Michael Pfeiffer
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <File.h>
|
||||||
|
|
||||||
|
#include "Report.h"
|
||||||
|
#include "Mask.h"
|
||||||
|
#include "ImageCache.h"
|
||||||
|
|
||||||
|
// Implementation of MaskDescription
|
||||||
|
|
||||||
|
MaskDescription::MaskDescription(PDF* pdf, const char* mask, int length, int width, int height, int bpc)
|
||||||
|
: fPDF(pdf)
|
||||||
|
, fMask(mask)
|
||||||
|
, fLength(length)
|
||||||
|
, fWidth(width)
|
||||||
|
, fHeight(height)
|
||||||
|
, fBPC(bpc)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
CacheItem* MaskDescription::NewItem(int id) {
|
||||||
|
REPORT(kDebug, -1, "MaskDescription::NewItem called");
|
||||||
|
int imageID;
|
||||||
|
BString fileName(kMaskPathPrefix);
|
||||||
|
const char* name;
|
||||||
|
::Mask* mask = NULL;
|
||||||
|
|
||||||
|
fileName << id;
|
||||||
|
name = fileName.String();
|
||||||
|
|
||||||
|
if (!StoreMask(name)) {
|
||||||
|
REPORT(kError, -1, "Could not store mask in cache.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
imageID = MakePDFMask();
|
||||||
|
|
||||||
|
if (imageID == -1) {
|
||||||
|
REPORT(kError, -1, "Could not embed mask in PDF file.");
|
||||||
|
unlink(name);
|
||||||
|
} else {
|
||||||
|
mask = new ::Mask(fPDF, imageID, name, Length(), Width(), Height(), BPC());
|
||||||
|
}
|
||||||
|
return mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MaskDescription::StoreMask(const char* name) {
|
||||||
|
bool ok;
|
||||||
|
BFile file(name, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
|
||||||
|
if (file.InitCheck() != B_OK) return false;
|
||||||
|
ok = file.Write(Mask(), Length()) == Length();
|
||||||
|
if (!ok) {
|
||||||
|
unlink(name);
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
int MaskDescription::MakePDFMask() {
|
||||||
|
// *maskId = PDF_open_image(fPdf, "raw", "memory", (const char *) mask, length, width, height, 1, bpc, "mask");
|
||||||
|
BString options;
|
||||||
|
int maskID;
|
||||||
|
PDF_create_pvf(fPDF, "mask", 0, Mask(), Length(), NULL);
|
||||||
|
options << "width " << fWidth << " height " << fHeight << " components 1 bpc " << fBPC;
|
||||||
|
maskID = PDF_load_image(fPDF, "raw", "mask", 0, options.String());
|
||||||
|
PDF_delete_pvf(fPDF, "mask", 0);
|
||||||
|
return maskID;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implementation of Mask
|
||||||
|
|
||||||
|
Mask::Mask(PDF* pdf, int imageID, const char* fileName, int length, int width, int height, int bpc)
|
||||||
|
: fPDF(pdf)
|
||||||
|
, fImageID(imageID)
|
||||||
|
, fFileName(fileName)
|
||||||
|
, fLength(length)
|
||||||
|
, fWidth(width)
|
||||||
|
, fHeight(height)
|
||||||
|
, fBPC(bpc)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Mask::~Mask() {
|
||||||
|
PDF_close_image(fPDF, ImageID());
|
||||||
|
unlink(FileName());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Mask::Equals(CIDescription* description) const {
|
||||||
|
REPORT(kDebug, -1, "Mask::Equals called");
|
||||||
|
MaskDescription* desc = dynamic_cast<MaskDescription*>(description);
|
||||||
|
if (desc && Length() == desc->Length() && Width() == desc->Width() && Height() == desc->Height() && BPC() == desc->BPC()) {
|
||||||
|
off_t size;
|
||||||
|
BFile file(FileName(), B_READ_ONLY);
|
||||||
|
if (file.InitCheck() == B_OK && file.GetSize(&size) == B_OK && size == Length()) {
|
||||||
|
char* buffer = new char[Length()];
|
||||||
|
if (buffer == NULL) return false;
|
||||||
|
bool ok = file.Read(buffer, Length()) == Length() &&
|
||||||
|
memcmp(desc->Mask(), buffer, Length()) == 0;
|
||||||
|
delete buffer;
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
85
src/add-ons/print/drivers/pdf/source/Mask.h
Normal file
85
src/add-ons/print/drivers/pdf/source/Mask.h
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Mask Cache Item.
|
||||||
|
|
||||||
|
Copyright (c) 2003 OpenBeOS.
|
||||||
|
|
||||||
|
Author:
|
||||||
|
Michael Pfeiffer
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MASK_CACHE_ITEM_H
|
||||||
|
#define _MASK_CACHE_ITEM_H
|
||||||
|
|
||||||
|
#include <String.h>
|
||||||
|
|
||||||
|
#include "pdflib.h"
|
||||||
|
#include "Cache.h"
|
||||||
|
|
||||||
|
class MaskDescription : public CIDescription {
|
||||||
|
public:
|
||||||
|
MaskDescription(PDF* pdf, const char* mask, int length, int width, int height, int bpc);
|
||||||
|
|
||||||
|
CacheItem* NewItem(int id);
|
||||||
|
|
||||||
|
const char* Mask() const { return fMask; }
|
||||||
|
int Length() const { return fLength; }
|
||||||
|
int Width() const { return fWidth; }
|
||||||
|
int Height() const { return fHeight; }
|
||||||
|
int BPC() const { return fBPC; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool StoreMask(const char* name);
|
||||||
|
int MakePDFMask();
|
||||||
|
|
||||||
|
PDF* fPDF;
|
||||||
|
const char* fMask;
|
||||||
|
int fLength;
|
||||||
|
int fWidth;
|
||||||
|
int fHeight;
|
||||||
|
int fBPC;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Mask : public CacheItem {
|
||||||
|
public:
|
||||||
|
Mask(PDF* pdf, int imageID, const char* fileName, int length, int width, int height, int bpc);
|
||||||
|
~Mask();
|
||||||
|
|
||||||
|
int ImageID() const { return fImageID; };
|
||||||
|
const char* FileName() const { return fFileName.String(); };
|
||||||
|
int Length() const { return fLength; };
|
||||||
|
int Width() const { return fWidth; }
|
||||||
|
int Height() const { return fHeight; }
|
||||||
|
int BPC() const { return fBPC; }
|
||||||
|
bool Equals(CIDescription* desc) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
PDF* fPDF;
|
||||||
|
int fImageID;
|
||||||
|
BString fFileName;
|
||||||
|
int fLength;
|
||||||
|
int fWidth;
|
||||||
|
int fHeight;
|
||||||
|
int fBPC;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -121,7 +121,7 @@ PDFWriter::PrintPage(int32 pageNumber, int32 pageCount)
|
|||||||
REPORT(kDebug, fPage, ">>>>> Collecting patterns...");
|
REPORT(kDebug, fPage, ">>>>> Collecting patterns...");
|
||||||
} else if (MakesPDF()) {
|
} else if (MakesPDF()) {
|
||||||
REPORT(kDebug, fPage, ">>>>> Generating PDF...");
|
REPORT(kDebug, fPage, ">>>>> Generating PDF...");
|
||||||
fImageCache.ResetID();
|
fImageCache.NextPass();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,7 +231,7 @@ PDFWriter::EndJob()
|
|||||||
fprintf(fLog, ": %s\n", rr->Desc());
|
fprintf(fLog, ": %s\n", rr->Desc());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
fImageCache.Flush(fPdf);
|
fImageCache.Flush();
|
||||||
|
|
||||||
PDF_close(fPdf);
|
PDF_close(fPdf);
|
||||||
REPORT(kDebug, 0, ">>>> PDF_close");
|
REPORT(kDebug, 0, ">>>> PDF_close");
|
||||||
@ -800,8 +800,8 @@ PDFWriter::CreatePattern()
|
|||||||
REPORT(kError, fPage, "CreatePattern could not create pattern");
|
REPORT(kError, fPage, "CreatePattern could not create pattern");
|
||||||
#if !USE_IMAGE_CACHE
|
#if !USE_IMAGE_CACHE
|
||||||
PDF_close_image(fPdf, image);
|
PDF_close_image(fPdf, image);
|
||||||
#endif
|
|
||||||
if (mask != -1) PDF_close_image(fPdf, mask);
|
if (mask != -1) PDF_close_image(fPdf, mask);
|
||||||
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PDF_setcolor(fPdf, "both", "rgb", 0, 0, 1, 0);
|
PDF_setcolor(fPdf, "both", "rgb", 0, 0, 1, 0);
|
||||||
@ -809,8 +809,8 @@ PDFWriter::CreatePattern()
|
|||||||
PDF_end_pattern(fPdf);
|
PDF_end_pattern(fPdf);
|
||||||
#if !USE_IMAGE_CACHE
|
#if !USE_IMAGE_CACHE
|
||||||
PDF_close_image(fPdf, image);
|
PDF_close_image(fPdf, image);
|
||||||
#endif
|
|
||||||
if (mask != -1) PDF_close_image(fPdf, mask);
|
if (mask != -1) PDF_close_image(fPdf, mask);
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Pattern* p = new Pattern(fState->pattern0, fState->backgroundColor, fState->foregroundColor, pattern);
|
Pattern* p = new Pattern(fState->pattern0, fState->backgroundColor, fState->foregroundColor, pattern);
|
||||||
@ -1623,19 +1623,26 @@ PDFWriter::GetImages(BRect src, int32 /*width*/, int32 /*height*/, int32 bytesPe
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mask) {
|
if (mask) {
|
||||||
|
// PDFlib deprecated:
|
||||||
// *maskId = PDF_open_image(fPdf, "raw", "memory", (const char *) mask, length, width, height, 1, bpc, "mask");
|
// *maskId = PDF_open_image(fPdf, "raw", "memory", (const char *) mask, length, width, height, 1, bpc, "mask");
|
||||||
|
#if USE_IMAGE_CACHE
|
||||||
|
*maskId = fImageCache.GetMask(fPdf, (char*)mask, length, width, height, bpc);
|
||||||
|
#else
|
||||||
BString options;
|
BString options;
|
||||||
PDF_create_pvf(fPdf, "mask", 0, mask, length, NULL);
|
PDF_create_pvf(fPdf, "mask", 0, mask, length, NULL);
|
||||||
options << "width " << width << " height " << height << " components 1 bpc " << bpc;
|
options << "width " << width << " height " << height << " components 1 bpc " << bpc;
|
||||||
*maskId = PDF_load_image(fPdf, "raw", "mask", 0, options.String());
|
*maskId = PDF_load_image(fPdf, "raw", "mask", 0, options.String());
|
||||||
delete []mask;
|
|
||||||
PDF_delete_pvf(fPdf, "mask", 0);
|
PDF_delete_pvf(fPdf, "mask", 0);
|
||||||
|
#endif
|
||||||
|
delete []mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
BBitmap * bm = ConvertBitmap(src, bytesPerRow, pixelFormat, flags, data);
|
BBitmap * bm = ConvertBitmap(src, bytesPerRow, pixelFormat, flags, data);
|
||||||
if (!bm) {
|
if (!bm) {
|
||||||
REPORT(kError, fPage, "ConvertBitmap failed!");
|
REPORT(kError, fPage, "ConvertBitmap failed!");
|
||||||
|
#if !USE_IMAGE_CACHE
|
||||||
if (*maskId != -1) PDF_close_image(fPdf, *maskId);
|
if (*maskId != -1) PDF_close_image(fPdf, *maskId);
|
||||||
|
#endif
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1650,7 +1657,9 @@ PDFWriter::GetImages(BRect src, int32 /*width*/, int32 /*height*/, int32 bytesPe
|
|||||||
if (!StoreTranslatorBitmap(bm, bitmapFileName, beosFormat)) {
|
if (!StoreTranslatorBitmap(bm, bitmapFileName, beosFormat)) {
|
||||||
delete bm;
|
delete bm;
|
||||||
REPORT(kError, fPage, "StoreTranslatorBitmap failed");
|
REPORT(kError, fPage, "StoreTranslatorBitmap failed");
|
||||||
|
#if !USE_IMAGE_CACHE
|
||||||
if (*maskId != -1) PDF_close_image(fPdf, *maskId);
|
if (*maskId != -1) PDF_close_image(fPdf, *maskId);
|
||||||
|
#endif
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
delete bm;
|
delete bm;
|
||||||
@ -2170,11 +2179,13 @@ PDFWriter::DrawPixels(BRect src, BRect dest, int32 width, int32 height, int32 by
|
|||||||
#if !USE_IMAGE_CACHE
|
#if !USE_IMAGE_CACHE
|
||||||
PDF_close_image(fPdf, image);
|
PDF_close_image(fPdf, image);
|
||||||
#endif
|
#endif
|
||||||
} else
|
} else {
|
||||||
REPORT(kError, fPage, "PDF_open_image_file failed!");
|
REPORT(kError, fPage, "PDF_open_image_file failed!");
|
||||||
|
}
|
||||||
if (maskId != -1) PDF_close_image(fPdf, maskId);
|
|
||||||
|
|
||||||
|
#if !USE_IMAGE_CACHE
|
||||||
|
if (maskId != -1) PDF_close_image(fPdf, maskId);
|
||||||
|
#endif
|
||||||
EndTransparency();
|
EndTransparency();
|
||||||
|
|
||||||
if (needs_scaling) PDF_restore(fPdf);
|
if (needs_scaling) PDF_restore(fPdf);
|
||||||
|
Loading…
Reference in New Issue
Block a user