1/* 2 3Mask Cache Item. 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 <sys/stat.h> 32#include <File.h> 33 34#include "Report.h" 35#include "Mask.h" 36#include "ImageCache.h" 37 38// Implementation of MaskDescription 39 40MaskDescription::MaskDescription(PDF* pdf, const char* mask, int length, int width, int height, int bpc) 41 : fPDF(pdf) 42 , fMask(mask) 43 , fLength(length) 44 , fWidth(width) 45 , fHeight(height) 46 , fBPC(bpc) 47{ 48} 49 50CacheItem* MaskDescription::NewItem(int id) { 51 REPORT(kDebug, -1, "MaskDescription::NewItem called"); 52 int imageID; 53 BString fileName(kMaskPathPrefix); 54 const char* name; 55 ::Mask* mask = NULL; 56 57 fileName << id; 58 name = fileName.String(); 59 60 if (!StoreMask(name)) { 61 REPORT(kError, -1, "Could not store mask in cache."); 62 return NULL; 63 } 64 65 imageID = MakePDFMask(); 66 67 if (imageID == -1) { 68 REPORT(kError, -1, "Could not embed mask in PDF file."); 69 unlink(name); 70 } else { 71 mask = new ::Mask(fPDF, imageID, name, Length(), Width(), Height(), BPC()); 72 } 73 return mask; 74} 75 76bool MaskDescription::StoreMask(const char* name) { 77 bool ok; 78 BFile file(name, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 79 if (file.InitCheck() != B_OK) return false; 80 ok = file.Write(Mask(), Length()) == Length(); 81 if (!ok) { 82 unlink(name); 83 } else { 84 } 85 return ok; 86} 87 88int MaskDescription::MakePDFMask() { 89// *maskId = PDF_open_image(fPdf, "raw", "memory", (const char *) mask, length, width, height, 1, bpc, "mask"); 90 BString options; 91 int maskID; 92 PDF_create_pvf(fPDF, "mask", 0, Mask(), Length(), NULL); 93 options << "width " << fWidth << " height " << fHeight << " components 1 bpc " << fBPC; 94 maskID = PDF_load_image(fPDF, "raw", "mask", 0, options.String()); 95 PDF_delete_pvf(fPDF, "mask", 0); 96 return maskID; 97} 98 99// Implementation of Mask 100 101Mask::Mask(PDF* pdf, int imageID, const char* fileName, int length, int width, int height, int bpc) 102 : fPDF(pdf) 103 , fImageID(imageID) 104 , fFileName(fileName) 105 , fLength(length) 106 , fWidth(width) 107 , fHeight(height) 108 , fBPC(bpc) 109{ 110} 111 112Mask::~Mask() { 113 PDF_close_image(fPDF, ImageID()); 114 unlink(FileName()); 115} 116 117bool Mask::Equals(CIDescription* description) const { 118 REPORT(kDebug, -1, "Mask::Equals called"); 119 MaskDescription* desc = dynamic_cast<MaskDescription*>(description); 120 if (desc && Length() == desc->Length() && Width() == desc->Width() && Height() == desc->Height() && BPC() == desc->BPC()) { 121 off_t size; 122 BFile file(FileName(), B_READ_ONLY); 123 if (file.InitCheck() == B_OK && file.GetSize(&size) == B_OK && size == Length()) { 124 char* buffer = new char[Length()]; 125 if (buffer == NULL) return false; 126 bool ok = file.Read(buffer, Length()) == Length() && 127 memcmp(desc->Mask(), buffer, Length()) == 0; 128 delete[] buffer; 129 return ok; 130 } 131 } 132 return false; 133} 134