Changed to use shared translator code

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6839 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Matthew Wilber 2004-03-01 02:58:18 +00:00
parent b94409e33a
commit 40bf8c0ae5
10 changed files with 105 additions and 1151 deletions

View File

@ -3,13 +3,19 @@ SubDir OBOS_TOP src add-ons translators pngtranslator ;
UseLibraryHeaders zlib ;
UseLibraryHeaders png ;
# Include BaseTranslator code from shared directory
SEARCH_SOURCE += [ FDirName $(OBOS_TOP) src add-ons translators shared ] ;
Translator PNGTranslator :
# PNGTranslator
# shared classes
BaseTranslator.cpp
TranslatorSettings.cpp
TranslatorWindow.cpp
# PNGTranslator classes
PNGMain.cpp
PNGTranslator.cpp
PNGView.cpp
PNGWindow.cpp
PNGTranslatorSettings.cpp ;
PNGView.cpp ;
LinkSharedOSLibs PNGTranslator : be translation libpng.a libz.a ;

View File

@ -33,11 +33,9 @@
/*****************************************************************************/
#include <Application.h>
#include <Screen.h>
#include <Alert.h>
#include "PNGTranslator.h"
#include "PNGWindow.h"
#include "PNGView.h"
#include "TranslatorWindow.h"
// ---------------------------------------------------------------
// main
@ -55,49 +53,13 @@
int
main()
{
BApplication app("application/x-vnd.obos-tiff-translator");
PNGTranslator *ptranslator = new PNGTranslator;
BView *view = NULL;
BRect rect(0, 0, PNG_VIEW_WIDTH, PNG_VIEW_HEIGHT);
if (ptranslator->MakeConfigurationView(NULL, &view, &rect)) {
BAlert *err = new BAlert("Error",
"Unable to create the PNGTranslator view.", "OK");
err->Go();
BApplication app("application/x-vnd.obos-png-translator");
status_t result;
result = LaunchTranslatorWindow(new PNGTranslator,
"PNGTranslator", BRect(0, 0, PNG_VIEW_WIDTH, PNG_VIEW_HEIGHT));
if (result == B_OK) {
app.Run();
return 0;
} else
return 1;
}
// release the translator even though I never really used it anyway
ptranslator->Release();
ptranslator = NULL;
PNGWindow *wnd = new PNGWindow(rect);
view->ResizeTo(rect.Width(), rect.Height());
wnd->AddChild(view);
BPoint wndpt = B_ORIGIN;
{
BScreen scrn;
BRect frame = scrn.Frame();
frame.InsetBy(10, 23);
// if the point is outside of the screen frame,
// use the mouse location to find a better point
if (!frame.Contains(wndpt)) {
uint32 dummy;
view->GetMouse(&wndpt, &dummy, false);
wndpt.x -= rect.Width() / 2;
wndpt.y -= rect.Height() / 2;
// clamp location to screen
if (wndpt.x < frame.left)
wndpt.x = frame.left;
if (wndpt.y < frame.top)
wndpt.y = frame.top;
if (wndpt.x > frame.right)
wndpt.x = frame.right;
if (wndpt.y > frame.bottom)
wndpt.y = frame.bottom;
}
}
wnd->MoveTo(wndpt);
wnd->Show();
app.Run();
return 0;
}

View File

@ -84,6 +84,14 @@ translation_format gOutputFormats[] = {
}
};
// Default settings for the Translator
TranSetting gDefaultSettings[] = {
{B_TRANSLATOR_EXT_HEADER_ONLY, TRAN_SETTING_BOOL, false},
{B_TRANSLATOR_EXT_DATA_ONLY, TRAN_SETTING_BOOL, false},
{PNG_SETTING_INTERLACE, TRAN_SETTING_INT32, PNG_INTERLACE_NONE}
// interlacing is off by default
};
// ---------------------------------------------------------------
// make_nth_translator
//
@ -173,17 +181,14 @@ pngcb_flush_data(png_structp ppng)
// Returns:
// ---------------------------------------------------------------
PNGTranslator::PNGTranslator()
: BTranslator()
: BaseTranslator("PNG Images", "PNG image translator",
PNG_TRANSLATOR_VERSION,
gInputFormats, sizeof(gInputFormats) / sizeof(translation_format),
gOutputFormats, sizeof(gOutputFormats) / sizeof(translation_format),
"PNGTranslator_Settings",
gDefaultSettings, sizeof(gDefaultSettings) / sizeof(TranSetting),
B_TRANSLATOR_BITMAP, B_PNG_FORMAT)
{
fpsettings = new PNGTranslatorSettings;
fpsettings->LoadSettings();
// load settings from the PNGTranslator settings file
strcpy(fName, "PNG Images");
sprintf(fInfo, "PNG image translator v%d.%d.%d %s",
static_cast<int>(PNG_TRANSLATOR_VERSION >> 8),
static_cast<int>((PNG_TRANSLATOR_VERSION >> 4) & 0xf),
static_cast<int>(PNG_TRANSLATOR_VERSION & 0xf), __DATE__);
}
// ---------------------------------------------------------------
@ -201,226 +206,16 @@ PNGTranslator::PNGTranslator()
// ---------------------------------------------------------------
PNGTranslator::~PNGTranslator()
{
fpsettings->Release();
}
// ---------------------------------------------------------------
// TranslatorName
//
// Returns the short name of the translator.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: a const char * to the short name of the translator
// ---------------------------------------------------------------
const char *
PNGTranslator::TranslatorName() const
{
return fName;
}
// ---------------------------------------------------------------
// TranslatorInfo
//
// Returns a more verbose name for the translator than the one
// TranslatorName() returns. This usually includes version info.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: a const char * to the verbose name of the translator
// ---------------------------------------------------------------
const char *
PNGTranslator::TranslatorInfo() const
{
return fInfo;
}
// ---------------------------------------------------------------
// TranslatorVersion
//
// Returns the integer representation of the current version of
// this translator.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
int32
PNGTranslator::TranslatorVersion() const
{
return PNG_TRANSLATOR_VERSION;
}
// ---------------------------------------------------------------
// InputFormats
//
// Returns a list of input formats supported by this translator.
//
// Preconditions:
//
// Parameters: out_count, The number of input formats
// support is returned here.
//
// Postconditions:
//
// Returns: the list of input formats and the number of input
// formats through the out_count parameter, if out_count is NULL,
// NULL is returned
// ---------------------------------------------------------------
const translation_format *
PNGTranslator::InputFormats(int32 *out_count) const
{
if (out_count) {
*out_count = sizeof(gInputFormats) /
sizeof(translation_format);
return gInputFormats;
} else
return NULL;
}
// ---------------------------------------------------------------
// OutputFormats
//
// Returns a list of output formats supported by this translator.
//
// Preconditions:
//
// Parameters: out_count, The number of output formats
// support is returned here.
//
// Postconditions:
//
// Returns: the list of output formats and the number of output
// formats through the out_count parameter, if out_count is NULL,
// NULL is returned
// ---------------------------------------------------------------
const translation_format *
PNGTranslator::OutputFormats(int32 *out_count) const
{
if (out_count) {
*out_count = sizeof(gOutputFormats) /
sizeof(translation_format);
return gOutputFormats;
} else
return NULL;
}
// ---------------------------------------------------------------
// identify_bits_header
//
// Determines if the data in inSource is in the
// B_TRANSLATOR_BITMAP ('bits') format. If it is, it returns
// info about the data in inSource to outInfo and pheader.
//
// Preconditions:
//
// Parameters: inSource, The source of the image data
//
// outInfo, Information about the translator
// is copied here
//
// amtread, Amount of data read from inSource
// before this function was called
//
// read, Pointer to the data that was read
// in before this function was called
//
// pheader, The bits header is copied here after
// it is read in from inSource
//
// Postconditions:
//
// Returns: B_NO_TRANSLATOR, if the data does not look like
// bits format data
//
// B_ERROR, if the header data could not be converted to host
// format
//
// B_OK, if the data looks like bits data and no errors were
// encountered
// ---------------------------------------------------------------
status_t
identify_bits_header(BPositionIO *inSource, translator_info *outInfo,
ssize_t amtread, uint8 *read, TranslatorBitmap *pheader = NULL)
{
TranslatorBitmap header;
memcpy(&header, read, amtread);
// copy portion of header already read in
// read in the rest of the header
ssize_t size = sizeof(TranslatorBitmap) - amtread;
if (inSource->Read(
(reinterpret_cast<uint8 *> (&header)) + amtread, size) != size)
return B_NO_TRANSLATOR;
// convert to host byte order
if (swap_data(B_UINT32_TYPE, &header, sizeof(TranslatorBitmap),
B_SWAP_BENDIAN_TO_HOST) != B_OK)
return B_ERROR;
// check if header values are reasonable
if (header.colors != B_RGB32 &&
header.colors != B_RGB32_BIG &&
header.colors != B_RGBA32 &&
header.colors != B_RGBA32_BIG &&
header.colors != B_RGB24 &&
header.colors != B_RGB24_BIG &&
header.colors != B_RGB16 &&
header.colors != B_RGB16_BIG &&
header.colors != B_RGB15 &&
header.colors != B_RGB15_BIG &&
header.colors != B_RGBA15 &&
header.colors != B_RGBA15_BIG &&
header.colors != B_CMAP8 &&
header.colors != B_GRAY8 &&
header.colors != B_GRAY1 &&
header.colors != B_CMYK32 &&
header.colors != B_CMY32 &&
header.colors != B_CMYA32 &&
header.colors != B_CMY24)
return B_NO_TRANSLATOR;
if (header.rowBytes * (header.bounds.Height() + 1) != header.dataSize)
return B_NO_TRANSLATOR;
if (outInfo) {
outInfo->type = B_TRANSLATOR_BITMAP;
outInfo->group = B_TRANSLATOR_BITMAP;
outInfo->quality = BBT_IN_QUALITY;
outInfo->capability = BBT_IN_CAPABILITY;
strcpy(outInfo->name, "Be Bitmap Format (PNGTranslator)");
strcpy(outInfo->MIME, "image/x-be-bitmap");
}
if (pheader) {
pheader->magic = header.magic;
pheader->bounds = header.bounds;
pheader->rowBytes = header.rowBytes;
pheader->colors = header.colors;
pheader->dataSize = header.dataSize;
}
return B_OK;
}
status_t
identify_png_header(BPositionIO *inSource, BMessage *ioExtension,
translator_info *outInfo, uint32 outType, ssize_t amtread, uint8 *read)
identify_png_header(BPositionIO *inSource, translator_info *outInfo)
{
if (amtread != 8)
return B_ERROR;
if (!png_check_sig(read, amtread))
const int32 kSigSize = 8;
uint8 buf[kSigSize];
if (inSource->Read(buf, kSigSize) != kSigSize)
return B_NO_TRANSLATOR;
if (!png_check_sig(buf, kSigSize))
// if first 8 bytes of stream don't match PNG signature bail
return B_NO_TRANSLATOR;
@ -437,7 +232,7 @@ identify_png_header(BPositionIO *inSource, BMessage *ioExtension,
}
// ---------------------------------------------------------------
// Identify
// DerivedIdentify
//
// Examines the data from inSource and determines if it is in a
// format that this translator knows how to work with.
@ -476,45 +271,11 @@ identify_png_header(BPositionIO *inSource, BMessage *ioExtension,
// Other errors if BPositionIO::Read() returned an error value
// ---------------------------------------------------------------
status_t
PNGTranslator::Identify(BPositionIO *inSource,
PNGTranslator::DerivedIdentify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType)
{
if (!outType)
outType = B_TRANSLATOR_BITMAP;
if (outType != B_TRANSLATOR_BITMAP && outType != B_PNG_FORMAT)
return B_NO_TRANSLATOR;
// Convert the magic numbers to the various byte orders so that
// I won't have to convert the data read in to see whether or not
// it is a supported type
uint32 nbits = B_TRANSLATOR_BITMAP;
if (swap_data(B_UINT32_TYPE, &nbits, sizeof(uint32),
B_SWAP_HOST_TO_BENDIAN) != B_OK)
return B_ERROR;
// Read in the magic number and determine if it
// is a supported type
uint8 ch[8];
if (inSource->Read(ch, 4) != 4)
return B_NO_TRANSLATOR;
// Read settings from ioExtension
if (ioExtension && fpsettings->LoadSettings(ioExtension) != B_OK)
return B_BAD_VALUE;
uint32 n32ch;
memcpy(&n32ch, ch, sizeof(uint32));
// if B_TRANSLATOR_BITMAP type
if (n32ch == nbits)
return identify_bits_header(inSource, outInfo, 4, ch);
// Might be PNG image
else {
if (inSource->Read(ch + 4, 4) != 4)
return B_NO_TRANSLATOR;
return identify_png_header(inSource, ioExtension, outInfo, outType, 8, ch);
}
return identify_png_header(inSource, outInfo);
}
void
@ -530,8 +291,8 @@ translate_direct_copy(BPositionIO *inSource, BPositionIO *outDestination)
}
status_t
translate_from_png_to_bits(BPositionIO *inSource, BPositionIO *outDestination,
PNGTranslatorSettings &settings)
PNGTranslator::translate_from_png_to_bits(BPositionIO *inSource,
BPositionIO *outDestination)
{
status_t result = B_ERROR;
// if a libpng errors before this is set
@ -720,19 +481,13 @@ translate_from_png_to_bits(BPositionIO *inSource, BPositionIO *outDestination,
}
status_t
translate_from_png(BPositionIO *inSource, BMessage *ioExtension,
uint32 outType, BPositionIO *outDestination, ssize_t amtread, uint8 *read,
PNGTranslatorSettings &settings)
PNGTranslator::translate_from_png(BPositionIO *inSource, uint32 outType,
BPositionIO *outDestination)
{
if (amtread != 8)
return B_ERROR;
if (outType == B_TRANSLATOR_BITMAP)
return translate_from_png_to_bits(inSource, outDestination,
settings);
return translate_from_png_to_bits(inSource, outDestination);
else {
// Translate from PNG to PNG
outDestination->Write(read, amtread);
translate_direct_copy(inSource, outDestination);
return B_OK;
@ -939,14 +694,14 @@ pix_bits_to_png(uint8 *pbits, uint8 *ppng, color_space fromspace,
}
status_t
translate_from_bits_to_png(BPositionIO *inSource, BPositionIO *outDestination,
ssize_t amtread, uint8 *read, PNGTranslatorSettings &settings)
PNGTranslator::translate_from_bits_to_png(BPositionIO *inSource,
BPositionIO *outDestination)
{
TranslatorBitmap bitsHeader;
status_t result;
result = identify_bits_header(inSource, NULL, amtread, read, &bitsHeader);
result = identify_bits_header(inSource, NULL, &bitsHeader);
if (result != B_OK)
return result;
@ -999,7 +754,7 @@ translate_from_bits_to_png(BPositionIO *inSource, BPositionIO *outDestination,
default:
return B_NO_TRANSLATOR;
}
interlace_type = settings.SetGetInterlace();
interlace_type = fSettings->SetGetInt32(PNG_SETTING_INTERLACE);
int32 bitsBytesPerPixel = 0;
switch (bitsHeader.colors) {
@ -1161,7 +916,7 @@ translate_from_bits_to_png(BPositionIO *inSource, BPositionIO *outDestination,
}
// ---------------------------------------------------------------
// Translate
// DerivedTranslate
//
// Translates the data in inSource to the type outType and stores
// the translated data in outDestination.
@ -1180,6 +935,10 @@ translate_from_bits_to_png(BPositionIO *inSource, BPositionIO *outDestination,
// outDestination, where the translated data is
// put
//
// baseType, indicates whether inSource is in the
// bits format, not in the bits format or
// is unknown
//
// Postconditions:
//
// Returns: B_BAD_VALUE, if the options in ioExtension are bad
@ -1192,114 +951,24 @@ translate_from_bits_to_png(BPositionIO *inSource, BPositionIO *outDestination,
// B_OK, if all went well
// ---------------------------------------------------------------
status_t
PNGTranslator::Translate(BPositionIO *inSource, const translator_info *inInfo,
BMessage *ioExtension, uint32 outType, BPositionIO *outDestination)
{
if (!outType)
outType = B_TRANSLATOR_BITMAP;
if (outType != B_TRANSLATOR_BITMAP && outType != B_PNG_FORMAT)
PNGTranslator::DerivedTranslate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension, uint32 outType,
BPositionIO *outDestination, int32 baseType)
{
if (baseType == 1)
// if inSource is in bits format
return translate_from_bits_to_png(inSource, outDestination);
else if (baseType == 0)
// if inSource is NOT in bits format
return translate_from_png(inSource, outType, outDestination);
else
return B_NO_TRANSLATOR;
// Convert the magic numbers to the various byte orders so that
// I won't have to convert the data read in to see whether or not
// it is a supported type
uint32 nbits = B_TRANSLATOR_BITMAP;
if (swap_data(B_UINT32_TYPE, &nbits, sizeof(uint32),
B_SWAP_HOST_TO_BENDIAN) != B_OK)
return B_ERROR;
// Read in the magic number and determine if it
// is a supported type
uint8 ch[8];
inSource->Seek(0, SEEK_SET);
if (inSource->Read(ch, 4) != 4)
return B_NO_TRANSLATOR;
// Read settings from ioExtension
if (ioExtension && fpsettings->LoadSettings(ioExtension) != B_OK)
return B_BAD_VALUE;
uint32 n32ch;
memcpy(&n32ch, ch, sizeof(uint32));
if (n32ch == nbits) {
// B_TRANSLATOR_BITMAP type
if (outType == B_TRANSLATOR_BITMAP) {
outDestination->Write(ch, 4);
translate_direct_copy(inSource, outDestination);
return B_OK;
} else
// Output to PNG
return translate_from_bits_to_png(inSource, outDestination,
4, ch, *fpsettings);
} else {
// Might be PNG image, read in the rest of
// the signature and check it
if (inSource->Read(ch + 4, 4) != 4)
return B_NO_TRANSLATOR;
if (!png_check_sig(ch, 8))
return B_NO_TRANSLATOR;
return translate_from_png(inSource, ioExtension, outType,
outDestination, 8, ch, *fpsettings);
}
}
// returns the current translator settings into ioExtension
status_t
PNGTranslator::GetConfigurationMessage(BMessage *ioExtension)
BView *
PNGTranslator::NewConfigView(TranslatorSettings *settings)
{
return fpsettings->GetConfigurationMessage(ioExtension);
}
// ---------------------------------------------------------------
// MakeConfigurationView
//
// Makes a BView object for configuring / displaying info about
// this translator.
//
// Preconditions:
//
// Parameters: ioExtension, configuration options for the
// translator
//
// outView, the view to configure the
// translator is stored here
//
// outExtent, the bounds of the view are
// stored here
//
// Postconditions:
//
// Returns: B_BAD_VALUE if outView or outExtent is NULL,
// B_NO_MEMORY if the view couldn't be allocated,
// B_OK if no errors
// ---------------------------------------------------------------
status_t
PNGTranslator::MakeConfigurationView(BMessage *ioExtension, BView **outView,
BRect *outExtent)
{
if (!outView || !outExtent)
return B_BAD_VALUE;
if (ioExtension && fpsettings->LoadSettings(ioExtension) != B_OK)
return B_BAD_VALUE;
PNGView *view = new PNGView(BRect(0, 0, PNG_VIEW_WIDTH, PNG_VIEW_HEIGHT),
"PNGTranslator Settings", B_FOLLOW_ALL, B_WILL_DRAW,
AcquireSettings());
if (!view)
return B_NO_MEMORY;
*outView = view;
*outExtent = view->Bounds();
return B_OK;
}
PNGTranslatorSettings *
PNGTranslator::AcquireSettings()
{
return fpsettings->Acquire();
return new PNGView(BRect(0, 0, PNG_VIEW_WIDTH, PNG_VIEW_HEIGHT),
"PNGTranslator Settings", B_FOLLOW_ALL, B_WILL_DRAW, settings);
}

View File

@ -41,11 +41,12 @@
#include <File.h>
#include <ByteOrder.h>
#include <fs_attr.h>
#include "PNGTranslatorSettings.h"
#include "BaseTranslator.h"
// IO Extension Names:
// PNG Translator Settings
#define PNG_SETTING_INTERLACE "png /interlace"
#define PNG_TRANSLATOR_VERSION 0x100
#define PNG_TRANSLATOR_VERSION B_TRANSLATION_MAKE_VER(1,0,0)
#define PNG_IN_QUALITY 0.8
#define PNG_IN_CAPABILITY 0.8
@ -58,52 +59,19 @@
#define BBT_OUT_CAPABILITY 0.4
class PNGTranslator : public BTranslator {
class PNGTranslator : public BaseTranslator {
public:
PNGTranslator();
virtual const char *TranslatorName() const;
// returns the short name of the translator
virtual const char *TranslatorInfo() const;
// returns a verbose name/description for the translator
virtual int32 TranslatorVersion() const;
// returns the version of the translator
virtual const translation_format *InputFormats(int32 *out_count)
const;
// returns the input formats and the count of input formats
// that this translator supports
virtual const translation_format *OutputFormats(int32 *out_count)
const;
// returns the output formats and the count of output formats
// that this translator supports
virtual status_t Identify(BPositionIO *inSource,
virtual status_t DerivedIdentify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType);
// determines whether or not this translator can convert the
// data in inSource to the type outType
virtual status_t Translate(BPositionIO *inSource,
virtual status_t DerivedTranslate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension,
uint32 outType, BPositionIO *outDestination);
// this function is the whole point of the Translation Kit,
// it translates the data in inSource to outDestination
// using the format outType
uint32 outType, BPositionIO *outDestination, int32 baseType);
virtual status_t GetConfigurationMessage(BMessage *ioExtension);
// write the current state of the translator into
// the supplied BMessage object
virtual status_t MakeConfigurationView(BMessage *ioExtension,
BView **outView, BRect *outExtent);
// creates and returns the view for displaying information
// about this translator
PNGTranslatorSettings *AcquireSettings();
virtual BView *NewConfigView(TranslatorSettings *settings);
protected:
virtual ~PNGTranslator();
@ -112,10 +80,14 @@ protected:
// the user
private:
PNGTranslatorSettings *fpsettings;
char fName[30];
char fInfo[100];
status_t translate_from_png_to_bits(BPositionIO *inSource,
BPositionIO *outDestination);
status_t translate_from_png(BPositionIO *inSource, uint32 outType,
BPositionIO *outDestination);
status_t translate_from_bits_to_png(BPositionIO *inSource,
BPositionIO *outDestination);
};
#endif // #ifndef PNG_TRANSLATOR_H

View File

@ -1,449 +0,0 @@
/*****************************************************************************/
// PNGTranslatorSettings
// Written by Michael Wilber
//
// PNGTranslatorSettings.cpp
//
// This class manages (saves/loads/locks/unlocks) the settings
// for the PNGTranslator.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// 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 <png.h>
#include <File.h>
#include <FindDirectory.h>
#include <TranslatorFormats.h>
// for B_TRANSLATOR_EXT_*
#include "PNGTranslatorSettings.h"
// ---------------------------------------------------------------
// Constructor
//
// Sets the default settings, location for the settings file
// and sets the reference count to 1
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
PNGTranslatorSettings::PNGTranslatorSettings()
: flock("PNG Settings Lock")
{
if (find_directory(B_USER_SETTINGS_DIRECTORY, &fsettingsPath))
fsettingsPath.SetTo("/tmp");
fsettingsPath.Append(PNG_SETTINGS_FILENAME);
frefCount = 1;
// Default Settings
// (Used when loading from the settings file or from
// a BMessage fails)
fmsgSettings.AddBool(B_TRANSLATOR_EXT_HEADER_ONLY, false);
fmsgSettings.AddBool(B_TRANSLATOR_EXT_DATA_ONLY, false);
fmsgSettings.AddInt32(PNG_SETTING_INTERLACE, PNG_INTERLACE_NONE);
// interlacing is off by default
}
// ---------------------------------------------------------------
// Acquire
//
// Returns a pointer to the PNGTranslatorSettings and increments
// the reference count.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: pointer to this PNGTranslatorSettings object
// ---------------------------------------------------------------
PNGTranslatorSettings *
PNGTranslatorSettings::Acquire()
{
PNGTranslatorSettings *psettings = NULL;
flock.Lock();
frefCount++;
psettings = this;
flock.Unlock();
return psettings;
}
// ---------------------------------------------------------------
// Release
//
// Decrements the reference count and deletes the
// PNGTranslatorSettings if the reference count is zero.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: pointer to this PNGTranslatorSettings object if
// the reference count is greater than zero, returns NULL
// if the reference count is zero and the PNGTranslatorSettings
// object has been deleted
// ---------------------------------------------------------------
PNGTranslatorSettings *
PNGTranslatorSettings::Release()
{
PNGTranslatorSettings *psettings = NULL;
flock.Lock();
frefCount--;
if (frefCount > 0) {
psettings = this;
flock.Unlock();
} else
delete this;
// delete this object and
// release locks
return psettings;
}
// ---------------------------------------------------------------
// Destructor
//
// Does nothing!
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
PNGTranslatorSettings::~PNGTranslatorSettings()
{
}
// ---------------------------------------------------------------
// LoadSettings
//
// Loads the settings by reading them from the default
// settings file.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: B_OK if there were no errors or an error code from
// BFile::SetTo() or BMessage::Unflatten() if there were errors
// ---------------------------------------------------------------
status_t
PNGTranslatorSettings::LoadSettings()
{
status_t result;
flock.Lock();
BFile settingsFile;
result = settingsFile.SetTo(fsettingsPath.Path(), B_READ_ONLY);
if (result == B_OK) {
BMessage msg;
result = msg.Unflatten(&settingsFile);
if (result == B_OK)
result = LoadSettings(&msg);
}
flock.Unlock();
return result;
}
// ---------------------------------------------------------------
// LoadSettings
//
// Loads the settings from a BMessage passed to the function.
//
// Preconditions:
//
// Parameters: pmsg pointer to BMessage that contains the
// settings
//
// Postconditions:
//
// Returns: B_BAD_VALUE if pmsg is NULL or invalid options
// have been found, B_OK if there were no
// errors or an error code from BMessage::FindBool() or
// BMessage::ReplaceBool() if there were other errors
// ---------------------------------------------------------------
status_t
PNGTranslatorSettings::LoadSettings(BMessage *pmsg)
{
status_t result = B_BAD_VALUE;
if (pmsg) {
// Make certain that no PNG settings
// are missing from the file
int32 interlace;
bool bheaderOnly, bdataOnly;
flock.Lock();
result = pmsg->FindBool(B_TRANSLATOR_EXT_HEADER_ONLY, &bheaderOnly);
if (result != B_OK)
bheaderOnly = SetGetHeaderOnly();
result = pmsg->FindBool(B_TRANSLATOR_EXT_DATA_ONLY, &bdataOnly);
if (result != B_OK)
bdataOnly = SetGetDataOnly();
result = pmsg->FindInt32(PNG_SETTING_INTERLACE, &interlace);
if (result != B_OK)
interlace = SetGetInterlace();
if (bheaderOnly && bdataOnly)
// "write header only" and "write data only"
// are mutually exclusive
result = B_BAD_VALUE;
else {
result = B_OK;
result = fmsgSettings.ReplaceBool(
B_TRANSLATOR_EXT_HEADER_ONLY, bheaderOnly);
if (result == B_OK)
result = fmsgSettings.ReplaceBool(
B_TRANSLATOR_EXT_DATA_ONLY, bdataOnly);
if (result == B_OK)
result = fmsgSettings.ReplaceInt32(PNG_SETTING_INTERLACE,
interlace);
}
flock.Unlock();
}
return result;
}
// ---------------------------------------------------------------
// SaveSettings
//
// Saves the settings as a flattened BMessage to the default
// settings file
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: B_OK if no errors or an error code from BFile::SetTo()
// or BMessage::Flatten() if there were errors
// ---------------------------------------------------------------
status_t
PNGTranslatorSettings::SaveSettings()
{
status_t result;
flock.Lock();
BFile settingsFile;
result = settingsFile.SetTo(fsettingsPath.Path(),
B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (result == B_OK)
result = fmsgSettings.Flatten(&settingsFile);
flock.Unlock();
return result;
}
// ---------------------------------------------------------------
// GetConfigurationMessage
//
// Saves the current settings to the BMessage passed to the
// function
//
// Preconditions:
//
// Parameters: pmsg pointer to BMessage where the settings
// will be stored
//
// Postconditions:
//
// Returns: B_OK if there were no errors or an error code from
// BMessage::RemoveName() or BMessage::AddBool() if there were
// errors
// ---------------------------------------------------------------
status_t
PNGTranslatorSettings::GetConfigurationMessage(BMessage *pmsg)
{
status_t result = B_BAD_VALUE;
if (pmsg) {
const char *kNames[] = {
B_TRANSLATOR_EXT_HEADER_ONLY,
B_TRANSLATOR_EXT_DATA_ONLY,
PNG_SETTING_INTERLACE
};
const int32 klen = sizeof(kNames) / sizeof(const char *);
int32 i;
for (i = 0; i < klen; i++) {
result = pmsg->RemoveName(kNames[i]);
if (result != B_OK && result != B_NAME_NOT_FOUND)
break;
}
if (i == klen) {
flock.Lock();
result = B_OK;
result = pmsg->AddBool(B_TRANSLATOR_EXT_HEADER_ONLY,
SetGetHeaderOnly());
if (result == B_OK)
result = pmsg->AddBool(B_TRANSLATOR_EXT_DATA_ONLY,
SetGetDataOnly());
if (result == B_OK)
result = pmsg->AddInt32(PNG_SETTING_INTERLACE,
SetGetInterlace());
flock.Unlock();
}
}
return result;
}
// ---------------------------------------------------------------
// SetGetHeaderOnly
//
// Sets the state of the HeaderOnly setting (if pbHeaderOnly
// is not NULL) and returns the previous value of the
// HeaderOnly setting.
//
// If the HeaderOnly setting is true, only the header of
// the image will be output; the data will not be output.
//
// Preconditions:
//
// Parameters: pbHeaderOnly pointer to a bool specifying
// the new value of the
// HeaderOnly setting
//
// Postconditions:
//
// Returns: the prior value of the HeaderOnly setting
// ---------------------------------------------------------------
bool
PNGTranslatorSettings::SetGetHeaderOnly(bool *pbHeaderOnly)
{
bool bprevValue = false;
// set to default value in case FindBool fails
flock.Lock();
fmsgSettings.FindBool(B_TRANSLATOR_EXT_HEADER_ONLY, &bprevValue);
if (pbHeaderOnly)
fmsgSettings.ReplaceBool(B_TRANSLATOR_EXT_HEADER_ONLY, *pbHeaderOnly);
flock.Unlock();
return bprevValue;
}
// ---------------------------------------------------------------
// SetGetDataOnly
//
// Sets the state of the DataOnly setting (if pbDataOnly
// is not NULL) and returns the previous value of the
// DataOnly setting.
//
// If the DataOnly setting is true, only the data of
// the image will be output; the header will not be output.
//
// Preconditions:
//
// Parameters: pbDataOnly pointer to a bool specifying
// the new value of the
// DataOnly setting
//
// Postconditions:
//
// Returns: the prior value of the DataOnly setting
// ---------------------------------------------------------------
bool
PNGTranslatorSettings::SetGetDataOnly(bool *pbDataOnly)
{
bool bprevValue = false;
// set to default value in case FindBool fails
flock.Lock();
fmsgSettings.FindBool(B_TRANSLATOR_EXT_DATA_ONLY, &bprevValue);
if (pbDataOnly)
fmsgSettings.ReplaceBool(B_TRANSLATOR_EXT_DATA_ONLY, *pbDataOnly);
flock.Unlock();
return bprevValue;
}
// ---------------------------------------------------------------
// SetGetInterlace
//
// Sets the state of the interlace setting (if pinterlace is
// not NULL) and returns the previous value of the interlace
// setting.
//
// If the interlace setting is PNG_INTERLACE_ADAM7, PNG images
// created by the PNGTranslator will be interlaced using the
// Adam7 method. If the setting is PNG_INTERLACE_NONE, PNG
// images created by the PNGTranslator will NOT be interlaced.
//
// Preconditions:
//
// Parameters: pinterlace pointer to int32 which specifies the
// new value for the interlace setting
//
// Postconditions:
//
// Returns: the prior value of the interlace setting
// ---------------------------------------------------------------
int32
PNGTranslatorSettings::SetGetInterlace(int32 *pinterlace)
{
int32 nprevValue = PNG_INTERLACE_NONE;
// set to default value in case FindBool fails
flock.Lock();
fmsgSettings.FindInt32(PNG_SETTING_INTERLACE, &nprevValue);
if (pinterlace && (*pinterlace == PNG_INTERLACE_NONE ||
*pinterlace == PNG_INTERLACE_ADAM7))
fmsgSettings.ReplaceInt32(PNG_SETTING_INTERLACE, *pinterlace);
flock.Unlock();
return nprevValue;
}

View File

@ -1,90 +0,0 @@
/*****************************************************************************/
// PNGTranslatorSettings
// Written by Michael Wilber
//
// PNGTranslatorSettings.h
//
// This class manages (saves/loads/locks/unlocks) the settings
// for the PNGTranslator.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// 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 PNG_TRANSLATOR_SETTINGS_H
#define PNG_TRANSLATOR_SETTINGS_H
#include <Locker.h>
#include <Path.h>
#include <Message.h>
#define PNG_SETTINGS_FILENAME "PNGTranslator_Settings"
// PNG Translator Settings
#define PNG_SETTING_INTERLACE "png /interlace"
class PNGTranslatorSettings {
public:
PNGTranslatorSettings();
PNGTranslatorSettings *Acquire();
// increments the reference count, returns this
PNGTranslatorSettings *Release();
// decrements the reference count, deletes this
// when count reaches zero, returns this when
// ref count is greater than zero, NULL when
// ref count is zero
status_t LoadSettings();
status_t LoadSettings(BMessage *pmsg);
status_t SaveSettings();
status_t GetConfigurationMessage(BMessage *pmsg);
bool SetGetHeaderOnly(bool *pbHeaderOnly = NULL);
// sets / gets HeaderOnly setting
// specifies if only the image header should be
// outputted
bool SetGetDataOnly(bool *pbDataOnly = NULL);
// sets / gets DataOnly setting
// specifiees if only the image data should be
// outputted
int32 SetGetInterlace(int32 *pinterlace = NULL);
// sets /gets Interlace setting
// specifies if interlacing will be used
// when the PNGTranslator creates PNG images
private:
~PNGTranslatorSettings();
// private so that Release() must be used
// to delete the object
BLocker flock;
int32 frefCount;
BPath fsettingsPath;
// where the settings file will be loaded from /
// saved to
BMessage fmsgSettings;
// the actual settings
};
#endif // #ifndef PNG_TRANSLATOR_SETTTINGS_H

View File

@ -57,10 +57,10 @@ interlace_msg(int option)
// Returns:
// ---------------------------------------------------------------
PNGView::PNGView(const BRect &frame, const char *name,
uint32 resize, uint32 flags, PNGTranslatorSettings *psettings)
uint32 resize, uint32 flags, TranslatorSettings *settings)
: BView(frame, name, resize, flags)
{
fpsettings = psettings;
fSettings = settings;
SetViewColor(220,220,220,0);
@ -72,13 +72,13 @@ PNGView::PNGView(const BRect &frame, const char *name,
fpmnuInterlace->AddItem(pitmNone);
fpmnuInterlace->AddItem(pitmAdam7);
fpfldInterlace = new BMenuField(BRect(20, 50, 200, 70), "PNG Interlace Menu",
"Interlacing Type", fpmnuInterlace);
fpfldInterlace = new BMenuField(BRect(20, 50, 200, 70),
"PNG Interlace Menu", "Interlacing Type", fpmnuInterlace);
fpfldInterlace->SetViewColor(ViewColor());
AddChild(fpfldInterlace);
// set Interlace option to show the current configuration
if (fpsettings->SetGetInterlace() == PNG_INTERLACE_NONE)
if (fSettings->SetGetInt32(PNG_SETTING_INTERLACE) == PNG_INTERLACE_NONE)
pitmNone->SetMarked(true);
else
pitmAdam7->SetMarked(true);
@ -99,7 +99,7 @@ PNGView::PNGView(const BRect &frame, const char *name,
// ---------------------------------------------------------------
PNGView::~PNGView()
{
fpsettings->Release();
fSettings->Release();
}
void
@ -152,9 +152,10 @@ PNGView::Draw(BRect area)
char detail[100];
sprintf(detail, "Version %d.%d.%d %s",
static_cast<int>(PNG_TRANSLATOR_VERSION >> 8),
static_cast<int>((PNG_TRANSLATOR_VERSION >> 4) & 0xf),
static_cast<int>(PNG_TRANSLATOR_VERSION & 0xf), __DATE__);
static_cast<int>(B_TRANSLATION_MAJOR_VER(PNG_TRANSLATOR_VERSION)),
static_cast<int>(B_TRANSLATION_MAJOR_VER(PNG_TRANSLATOR_VERSION)),
static_cast<int>(B_TRANSLATION_MAJOR_VER(PNG_TRANSLATOR_VERSION)),
__DATE__);
DrawString(detail, BPoint(xbold, yplain + ybold));
int32 lineno = 6;
@ -179,8 +180,8 @@ PNGView::MessageReceived(BMessage *pmsg)
// change setting for interlace option
int32 option;
if (pmsg->FindInt32(PNG_SETTING_INTERLACE, &option) == B_OK) {
fpsettings->SetGetInterlace(&option);
fpsettings->SaveSettings();
fSettings->SetGetInt32(PNG_SETTING_INTERLACE, &option);
fSettings->SaveSettings();
}
} else
BView::MessageReceived(pmsg);

View File

@ -35,7 +35,7 @@
#include <MenuField.h>
#include <MenuItem.h>
#include <PopUpMenu.h>
#include "PNGTranslatorSettings.h"
#include "TranslatorSettings.h"
#define PNG_VIEW_WIDTH 300
#define PNG_VIEW_HEIGHT 250
@ -47,7 +47,7 @@
class PNGView : public BView {
public:
PNGView(const BRect &frame, const char *name, uint32 resize,
uint32 flags, PNGTranslatorSettings *psettings);
uint32 flags, TranslatorSettings *settings);
// sets up the view
~PNGView();
@ -62,7 +62,7 @@ private:
BPopUpMenu *fpmnuInterlace;
BMenuField *fpfldInterlace;
PNGTranslatorSettings *fpsettings;
TranslatorSettings *fSettings;
// the actual settings for the translator,
// shared with the translator
};

View File

@ -1,69 +0,0 @@
/*****************************************************************************/
// PNGWindow
// Written by Michael Wilber, OBOS Translation Kit Team
//
// PNGWindow.cpp
//
// This BWindow based object is used to hold the PNGView object when the
// user runs the PNGTranslator as an application.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// 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 "PNGWindow.h"
// ---------------------------------------------------------------
// Constructor
//
// Sets up the BWindow for holding a PNGView
//
// Preconditions:
//
// Parameters: area, The bounds of the window
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
PNGWindow::PNGWindow(BRect area)
: BWindow(area, "PNGTranslator", B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
{
}
// ---------------------------------------------------------------
// Destructor
//
// Posts a quit message so that the application is close properly
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
PNGWindow::~PNGWindow()
{
be_app->PostMessage(B_QUIT_REQUESTED);
}

View File

@ -1,48 +0,0 @@
/*****************************************************************************/
// PNGWindow
// Written by Michael Wilber, OBOS Translation Kit Team
//
// PNGWindow.h
//
// This BWindow based object is used to hold the PNGView object when the
// user runs the PNGTranslator as an application.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// 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 PNGWINDOW_H
#define PNGWINDOW_H
#include <Application.h>
#include <Window.h>
#include <View.h>
class PNGWindow : public BWindow {
public:
PNGWindow(BRect area);
// Sets up a BWindow with bounds area
~PNGWindow();
// Posts a quit message so that the application closes properly
};
#endif // #define PNGWINDOW_H