From 51c15c1cade3dbd2bbc84b7abec055c3a1adc8cf Mon Sep 17 00:00:00 2001 From: Matthew Wilber Date: Sun, 29 Feb 2004 19:32:34 +0000 Subject: [PATCH] Changed TGATranslator to use shared translator code instead of duplicating code git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6818 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/add-ons/translators/tgatranslator/Jamfile | 12 +- .../tgatranslator/StreamBuffer.cpp | 225 -------- .../translators/tgatranslator/StreamBuffer.h | 70 --- .../translators/tgatranslator/TGAMain.cpp | 55 +- .../tgatranslator/TGATranslator.cpp | 518 +++--------------- .../translators/tgatranslator/TGATranslator.h | 77 ++- .../tgatranslator/TGATranslatorSettings.cpp | 474 ---------------- .../tgatranslator/TGATranslatorSettings.h | 94 ---- .../translators/tgatranslator/TGAView.cpp | 25 +- .../translators/tgatranslator/TGAView.h | 6 +- .../translators/tgatranslator/TGAWindow.cpp | 69 --- .../translators/tgatranslator/TGAWindow.h | 48 -- 12 files changed, 131 insertions(+), 1542 deletions(-) delete mode 100644 src/add-ons/translators/tgatranslator/StreamBuffer.cpp delete mode 100644 src/add-ons/translators/tgatranslator/StreamBuffer.h delete mode 100644 src/add-ons/translators/tgatranslator/TGATranslatorSettings.cpp delete mode 100644 src/add-ons/translators/tgatranslator/TGATranslatorSettings.h delete mode 100644 src/add-ons/translators/tgatranslator/TGAWindow.cpp delete mode 100644 src/add-ons/translators/tgatranslator/TGAWindow.h diff --git a/src/add-ons/translators/tgatranslator/Jamfile b/src/add-ons/translators/tgatranslator/Jamfile index 96b7523d07..0c5a6dcbe8 100644 --- a/src/add-ons/translators/tgatranslator/Jamfile +++ b/src/add-ons/translators/tgatranslator/Jamfile @@ -1,6 +1,16 @@ SubDir OBOS_TOP src add-ons translators tgatranslator ; -Translator TGATranslator : StreamBuffer.cpp TGATranslatorSettings.cpp TGAMain.cpp TGATranslator.cpp TGAView.cpp TGAWindow.cpp ; +# Include BaseTranslator code from TGATranslator directory +SEARCH_SOURCE += [ FDirName $(OBOS_TOP) src add-ons translators shared ] ; + +Translator TGATranslator : + BaseTranslator.cpp + TranslatorSettings.cpp + TranslatorWindow.cpp + StreamBuffer.cpp + TGAMain.cpp + TGATranslator.cpp + TGAView.cpp ; LinkSharedOSLibs TGATranslator : be translation ; diff --git a/src/add-ons/translators/tgatranslator/StreamBuffer.cpp b/src/add-ons/translators/tgatranslator/StreamBuffer.cpp deleted file mode 100644 index fb01bcf0ed..0000000000 --- a/src/add-ons/translators/tgatranslator/StreamBuffer.cpp +++ /dev/null @@ -1,225 +0,0 @@ -/*****************************************************************************/ -// StreamBuffer -// Written by Michael Wilber, OBOS Translation Kit Team -// -// StreamBuffer.cpp -// -// This class is for buffering data from a BPositionIO object in order to -// improve performance for cases when small amounts of data are frequently -// read from a BPositionIO object. -// -// -// 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 -#include -#include "StreamBuffer.h" - -#define min(x,y) (((x) < (y)) ? (x) : (y)) -#define max(x,y) (((x) > (y)) ? (x) : (y)) - -// --------------------------------------------------------------- -// Constructor -// -// Initializes the StreamBuffer to read from pstream, buffering -// nbuffersize bytes of data at a time. Note that if nbuffersize -// is smaller than MIN_BUFFER_SIZE, MIN_BUFFER_SIZE is used -// as the buffer size. -// -// Preconditions: -// -// Parameters: pstream, the stream to be buffered -// -// nbuffersize, number of bytes to be read from -// pstream at a time -// -// Postconditions: -// -// Returns: -// --------------------------------------------------------------- -StreamBuffer::StreamBuffer(BPositionIO *pstream, size_t nbuffersize, - bool binitialread) -{ - fpStream = pstream; - fpBuffer = NULL; - fnBufferSize = 0; - fnLen = 0; - fnPos = 0; - - if (!pstream) - return; - - fnBufferSize = max(nbuffersize, MIN_BUFFER_SIZE); - fpBuffer = new uint8[fnBufferSize]; - if (fpBuffer && binitialread) - ReadStream(); - // Fill the buffer with data so that - // object is prepared for first call to - // Read() -} - -// --------------------------------------------------------------- -// Destructor -// -// Destroys data allocated for this object -// -// Preconditions: -// -// Parameters: -// -// Postconditions: -// -// Returns: -// --------------------------------------------------------------- -StreamBuffer::~StreamBuffer() -{ - fnBufferSize = 0; - fnLen = 0; - fnPos = 0; - fpStream = NULL; - - delete[] fpBuffer; - fpBuffer = NULL; -} - -// --------------------------------------------------------------- -// InitCheck -// -// Determines whether the constructor failed or not -// -// Preconditions: -// -// Parameters: -// -// Postconditions: -// -// Returns: B_OK if object has been initialized successfully, -// B_ERROR if not -// --------------------------------------------------------------- -status_t -StreamBuffer::InitCheck() -{ - if (fpStream && fpBuffer) - return B_OK; - else - return B_ERROR; -} - -// --------------------------------------------------------------- -// Read -// -// Copies up to nbytes of data from the stream into pinto -// -// Preconditions: ReadStream() must be called once before this -// function is called (the constructor does this) -// -// Parameters: pinto, the buffer to be copied to -// -// nbytes, the maximum number of bytes to copy -// -// Postconditions: -// -// Returns: the number of bytes successfully read or an -// error code returned by BPositionIO::Read() -// --------------------------------------------------------------- -ssize_t -StreamBuffer::Read(uint8 *pinto, size_t nbytes) -{ - ssize_t result = B_ERROR; - size_t rd1 = 0, rd2 = 0; - - rd1 = min(nbytes, fnLen - fnPos); - memcpy(pinto, fpBuffer + fnPos, rd1); - fnPos += rd1; - - if (rd1 < nbytes) { - pinto += rd1; - result = ReadStream(); - if (result > 0) { - rd2 = min(nbytes - rd1, fnLen); - memcpy(pinto, fpBuffer, rd2); - fnPos += rd2; - } else - // return error code or zero - return result; - } - - return rd1 + rd2; -} - -// --------------------------------------------------------------- -// Seek -// -// Seeks the stream to the given position and refreshes the -// read buffer. If the seek operation fails, the read buffer -// will be reset. -// -// Preconditions: fpBuffer must be allocated and fnBufferSize -// must be valid -// -// Parameters: -// -// Postconditions: -// -// Returns: true if the seek was successful, -// false if the seek operation failed -// --------------------------------------------------------------- -bool -StreamBuffer::Seek(off_t position) -{ - fnLen = 0; - fnPos = 0; - - if (fpStream->Seek(position, SEEK_SET) == position) { - ReadStream(); - return true; - } - - return false; -} - -// --------------------------------------------------------------- -// ReadStream -// -// Fills the stream buffer with data read in from the stream -// -// Preconditions: fpBuffer must be allocated and fnBufferSize -// must be valid -// -// Parameters: -// -// Postconditions: -// -// Returns: the number of bytes successfully read or an -// error code returned by BPositionIO::Read() -// --------------------------------------------------------------- -ssize_t -StreamBuffer::ReadStream() -{ - ssize_t rd; - rd = fpStream->Read(fpBuffer, fnBufferSize); - if (rd >= 0) { - fnLen = rd; - fnPos = 0; - } - return rd; -} diff --git a/src/add-ons/translators/tgatranslator/StreamBuffer.h b/src/add-ons/translators/tgatranslator/StreamBuffer.h deleted file mode 100644 index 5df926b1d7..0000000000 --- a/src/add-ons/translators/tgatranslator/StreamBuffer.h +++ /dev/null @@ -1,70 +0,0 @@ -/*****************************************************************************/ -// StreamBuffer -// Written by Michael Wilber, OBOS Translation Kit Team -// -// StreamBuffer.h -// -// This class is for buffering data from a BPositionIO object in order to -// improve performance for cases when small amounts of data are frequently -// read from a BPositionIO object. -// -// -// 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 STREAM_BUFFER_H -#define STREAM_BUFFER_H - -#include - -#define MIN_BUFFER_SIZE 512 - -class StreamBuffer { -public: - StreamBuffer(BPositionIO *pstream, size_t nbuffersize, bool binitialread); - ~StreamBuffer(); - - status_t InitCheck(); - // Determines whether the constructor failed or not - - ssize_t Read(uint8 *pinto, size_t nbytes); - // copy nbytes from the stream into pinto - - bool Seek(off_t position); - // seek the stream to the given position - -private: - ssize_t ReadStream(); - // Load the stream buffer from the stream - - BPositionIO *fpStream; - // stream object this object is buffering - uint8 *fpBuffer; - // buffered data from fpStream - size_t fnBufferSize; - // number of bytes of memory allocated for fpBuffer - size_t fnLen; - // number of bytes of actual data in fpBuffer - size_t fnPos; - // current position in the buffer -}; - -#endif diff --git a/src/add-ons/translators/tgatranslator/TGAMain.cpp b/src/add-ons/translators/tgatranslator/TGAMain.cpp index c17a83eb01..14c005ebd6 100644 --- a/src/add-ons/translators/tgatranslator/TGAMain.cpp +++ b/src/add-ons/translators/tgatranslator/TGAMain.cpp @@ -28,11 +28,8 @@ /*****************************************************************************/ #include -#include -#include #include "TGATranslator.h" -#include "TGAWindow.h" -#include "TGAView.h" +#include "TranslatorWindow.h" // --------------------------------------------------------------- // main @@ -51,48 +48,12 @@ int main() { BApplication app("application/x-vnd.obos-tga-translator"); - TGATranslator *ptranslator = new TGATranslator; - BView *view = NULL; - BRect rect(0, 0, 225, 175); - if (ptranslator->MakeConfigurationView(NULL, &view, &rect)) { - BAlert *err = new BAlert("Error", - "Unable to create the TGATranslator view.", "OK"); - err->Go(); + status_t result; + result = LaunchTranslatorWindow(new TGATranslator, + "TGATranslator", BRect(0, 0, 225, 175)); + 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; - - TGAWindow *wnd = new TGAWindow(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; } diff --git a/src/add-ons/translators/tgatranslator/TGATranslator.cpp b/src/add-ons/translators/tgatranslator/TGATranslator.cpp index aeb4078e2c..0d9002c298 100644 --- a/src/add-ons/translators/tgatranslator/TGATranslator.cpp +++ b/src/add-ons/translators/tgatranslator/TGATranslator.cpp @@ -34,8 +34,6 @@ #include "TGAView.h" #include "StreamBuffer.h" -#define min(a,b) ((a < b) ? (a) : (b)) - // The input formats that this translator supports. translation_format gInputFormats[] = { { @@ -76,6 +74,16 @@ 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}, + {TGA_SETTING_RLE, TRAN_SETTING_BOOL, false}, + // RLE compression is off by default + {TGA_SETTING_IGNORE_ALPHA, TRAN_SETTING_BOOL, false} + // Don't ignore the alpha channel by default +}; + // --------------------------------------------------------------- // make_nth_translator // @@ -102,7 +110,6 @@ BTranslator * make_nth_translator(int32 n, image_id you, uint32 flags, ...) { BTranslator *ptranslator = NULL; - if (!n) ptranslator = new TGATranslator(); @@ -124,17 +131,14 @@ make_nth_translator(int32 n, image_id you, uint32 flags, ...) // Returns: // --------------------------------------------------------------- TGATranslator::TGATranslator() - : BTranslator() + : BaseTranslator("TGA Images", "TGA image translator", + TGA_TRANSLATOR_VERSION, + gInputFormats, sizeof(gInputFormats) / sizeof(translation_format), + gOutputFormats, sizeof(gOutputFormats) / sizeof(translation_format), + "TGATranslator_Settings", + gDefaultSettings, sizeof(gDefaultSettings) / sizeof(TranSetting), + B_TRANSLATOR_BITMAP, B_TGA_FORMAT) { - fpsettings = new TGATranslatorSettings; - fpsettings->LoadSettings(); - // load settings from the TGA Translator settings file - - strcpy(fName, "TGA Images"); - sprintf(fInfo, "TGA image translator v%d.%d.%d %s", - static_cast(TGA_TRANSLATOR_VERSION >> 8), - static_cast((TGA_TRANSLATOR_VERSION >> 4) & 0xf), - static_cast(TGA_TRANSLATOR_VERSION & 0xf), __DATE__); } // --------------------------------------------------------------- @@ -155,222 +159,13 @@ TGATranslator::TGATranslator() // that this destructor will never be called TGATranslator::~TGATranslator() { - 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 * -TGATranslator::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 * -TGATranslator::TranslatorInfo() const -{ - return fInfo; -} - -// --------------------------------------------------------------- -// TranslatorVersion -// -// Returns the integer representation of the current version of -// this translator. -// -// Preconditions: -// -// Parameters: -// -// Postconditions: -// -// Returns: -// --------------------------------------------------------------- -int32 -TGATranslator::TranslatorVersion() const -{ - return TGA_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 array of input formats and the number of input -// formats through the out_count parameter -// --------------------------------------------------------------- -const translation_format * -TGATranslator::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 array of output formats and the number of output -// formats through the out_count parameter -// --------------------------------------------------------------- -const translation_format * -TGATranslator::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 (&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 (TGATranslator)"); - 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; } uint8 -tga_alphabits(TGAFileHeader &filehead, TGAColorMapSpec &mapspec, - TGAImageSpec &imagespec, TGATranslatorSettings &settings) +TGATranslator::tga_alphabits(TGAFileHeader &filehead, TGAColorMapSpec &mapspec, + TGAImageSpec &imagespec) { - if (settings.SetGetIgnoreAlpha()) + if (fSettings->SetGetBool(TGA_SETTING_IGNORE_ALPHA)) return 0; else { uint8 nalpha; @@ -448,15 +243,14 @@ tga_alphabits(TGAFileHeader &filehead, TGAColorMapSpec &mapspec, // --------------------------------------------------------------- status_t identify_tga_header(BPositionIO *inSource, translator_info *outInfo, - ssize_t amtread, uint8 *read, TGAFileHeader *pfileheader = NULL, - TGAColorMapSpec *pmapspec = NULL, TGAImageSpec *pimagespec = NULL) + TGAFileHeader *pfileheader = NULL, TGAColorMapSpec *pmapspec = NULL, + TGAImageSpec *pimagespec = NULL) { uint8 buf[TGA_HEADERS_SIZE]; - memcpy(buf, read, amtread); - // copy portion of TGA headers already read in + // read in the rest of the TGA headers - ssize_t size = TGA_HEADERS_SIZE - amtread; - if (size > 0 && inSource->Read(buf + amtread, size) != size) + ssize_t size = TGA_HEADERS_SIZE; + if (size > 0 && inSource->Read(buf, size) != size) return B_NO_TRANSLATOR; // Read in TGA file header @@ -621,81 +415,12 @@ identify_tga_header(BPositionIO *inSource, translator_info *outInfo, return B_OK; } -// --------------------------------------------------------------- -// Identify -// -// Examines the data from inSource and determines if it is in a -// format that this translator knows how to work with. -// -// Preconditions: -// -// Parameters: inSource, where the data to examine is -// -// inFormat, a hint about the data in inSource, -// it is ignored since it is only a hint -// -// ioExtension, configuration settings for the -// translator -// -// outInfo, information about what data is in -// inSource and how well this translator -// can handle that data is stored here -// -// outType, The format that the user wants -// the data in inSource to be -// converted to -// -// Postconditions: -// -// Returns: B_NO_TRANSLATOR, if this translator can't handle -// the data in inSource -// -// B_ERROR, if there was an error converting the data to the host -// format -// -// B_BAD_VALUE, if the settings in ioExtension are bad -// -// B_OK, if this translator understand the data and there were -// no errors found -// --------------------------------------------------------------- status_t -TGATranslator::Identify(BPositionIO *inSource, +TGATranslator::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_TGA_FORMAT) - return B_NO_TRANSLATOR; - - uint8 ch[4]; - uint32 nbits = B_TRANSLATOR_BITMAP; - - // 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 - if (swap_data(B_UINT32_TYPE, &nbits, 4, B_SWAP_HOST_TO_BENDIAN) != B_OK) - return B_ERROR; - - // Read in the magic number and determine if it - // is a supported type - 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); - // if NOT B_TRANSLATOR_BITMAP, it could be - // an image in the TGA format - // (The TGA format does not have a magic number at the head of the file) - else - return identify_tga_header(inSource, outInfo, 4, ch); + return identify_tga_header(inSource, outInfo); } // Convert width pixels from pbits to TGA format, storing the @@ -1425,8 +1150,6 @@ write_tga_footer(BPositionIO *outDestination) // read, pointer to the data already read from // inSource // -// settings, settings object specifying whether -// RLE will be used, and so on // // outType, the type of data to convert to // @@ -1443,16 +1166,15 @@ write_tga_footer(BPositionIO *outDestination) // B_OK, if successfully translated the data from the bits format // --------------------------------------------------------------- status_t -translate_from_bits(BPositionIO *inSource, ssize_t amtread, uint8 *read, - TGATranslatorSettings &settings, uint32 outType, +TGATranslator::translate_from_bits(BPositionIO *inSource, uint32 outType, BPositionIO *outDestination) { TranslatorBitmap bitsHeader; bool bheaderonly = false, bdataonly = false, brle; - brle = settings.SetGetRLE(); + brle = fSettings->SetGetBool(TGA_SETTING_RLE); 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; @@ -1813,7 +1535,6 @@ pix_tganm_to_bits(uint8 *pbits, uint8 *ptga, // // imagespec, width / height info // -// settings, TGATranslator settings // // // Postconditions: @@ -1823,17 +1544,16 @@ pix_tganm_to_bits(uint8 *pbits, uint8 *ptga, // B_OK, if all went well // --------------------------------------------------------------- status_t -translate_from_tganm_to_bits(BPositionIO *inSource, +TGATranslator::translate_from_tganm_to_bits(BPositionIO *inSource, BPositionIO *outDestination, TGAFileHeader &filehead, - TGAColorMapSpec &mapspec, TGAImageSpec &imagespec, - TGATranslatorSettings &settings) + TGAColorMapSpec &mapspec, TGAImageSpec &imagespec) { bool bvflip; if (imagespec.descriptor & TGA_ORIGIN_VERT_BIT) bvflip = false; else bvflip = true; - uint8 nalpha = tga_alphabits(filehead, mapspec, imagespec, settings); + uint8 nalpha = tga_alphabits(filehead, mapspec, imagespec); int32 bitsRowBytes = imagespec.width * 4; uint8 tgaBytesPerPixel = (imagespec.depth / 8) + ((imagespec.depth % 8) ? 1 : 0); @@ -1911,7 +1631,6 @@ translate_from_tganm_to_bits(BPositionIO *inSource, // // imagespec, width / height info // -// settings, TGATranslator settings // // // Postconditions: @@ -1921,10 +1640,9 @@ translate_from_tganm_to_bits(BPositionIO *inSource, // B_OK, if all went well // --------------------------------------------------------------- status_t -translate_from_tganmrle_to_bits(BPositionIO *inSource, +TGATranslator::translate_from_tganmrle_to_bits(BPositionIO *inSource, BPositionIO *outDestination, TGAFileHeader &filehead, - TGAColorMapSpec &mapspec, TGAImageSpec &imagespec, - TGATranslatorSettings &settings) + TGAColorMapSpec &mapspec, TGAImageSpec &imagespec) { status_t result = B_OK; @@ -1933,7 +1651,7 @@ translate_from_tganmrle_to_bits(BPositionIO *inSource, bvflip = false; else bvflip = true; - uint8 nalpha = tga_alphabits(filehead, mapspec, imagespec, settings); + uint8 nalpha = tga_alphabits(filehead, mapspec, imagespec); int32 bitsRowBytes = imagespec.width * 4; uint8 tgaBytesPerPixel = (imagespec.depth / 8) + ((imagespec.depth % 8) ? 1 : 0); @@ -2215,8 +1933,6 @@ translate_from_tgam_to_bits(BPositionIO *inSource, // // imagespec, width / height info // -// settings, TGATranslator settings -// // pmap, color palette // // @@ -2227,10 +1943,9 @@ translate_from_tgam_to_bits(BPositionIO *inSource, // B_OK, if all went well // --------------------------------------------------------------- status_t -translate_from_tgamrle_to_bits(BPositionIO *inSource, +TGATranslator::translate_from_tgamrle_to_bits(BPositionIO *inSource, BPositionIO *outDestination, TGAFileHeader &filehead, - TGAColorMapSpec &mapspec, TGAImageSpec &imagespec, - TGATranslatorSettings &settings, uint8 *pmap) + TGAColorMapSpec &mapspec, TGAImageSpec &imagespec, uint8 *pmap) { status_t result = B_OK; @@ -2239,7 +1954,7 @@ translate_from_tgamrle_to_bits(BPositionIO *inSource, bvflip = false; else bvflip = true; - uint8 nalpha = tga_alphabits(filehead, mapspec, imagespec, settings); + uint8 nalpha = tga_alphabits(filehead, mapspec, imagespec); int32 bitsRowBytes = imagespec.width * 4; uint8 tgaPalBytesPerPixel = (mapspec.entrysize / 8) + ((mapspec.entrysize % 8) ? 1 : 0); @@ -2353,9 +2068,6 @@ translate_from_tgamrle_to_bits(BPositionIO *inSource, // read, pointer to the data already read from // inSource // -// settings, settings object specifying whether -// RLE will be used, and so on -// // outType, the type of data to convert to // // outDestination, where the output is written to @@ -2371,8 +2083,7 @@ translate_from_tgamrle_to_bits(BPositionIO *inSource, // B_OK, if successfully translated the data from the bits format // --------------------------------------------------------------- status_t -translate_from_tga(BPositionIO *inSource, ssize_t amtread, uint8 *read, - TGATranslatorSettings &settings, uint32 outType, +TGATranslator::translate_from_tga(BPositionIO *inSource, uint32 outType, BPositionIO *outDestination) { TGAFileHeader fileheader; @@ -2381,8 +2092,8 @@ translate_from_tga(BPositionIO *inSource, ssize_t amtread, uint8 *read, bool bheaderonly = false, bdataonly = false; status_t result; - result = identify_tga_header(inSource, NULL, amtread, read, - &fileheader, &mapspec, &imagespec); + result = identify_tga_header(inSource, NULL, &fileheader, &mapspec, + &imagespec); if (result != B_OK) return result; @@ -2439,7 +2150,7 @@ translate_from_tga(BPositionIO *inSource, ssize_t amtread, uint8 *read, bitsHeader.rowBytes = imagespec.width * 4; if (fileheader.imagetype != TGA_NOCOMP_BW && fileheader.imagetype != TGA_RLE_BW && - tga_alphabits(fileheader, mapspec, imagespec, settings)) + tga_alphabits(fileheader, mapspec, imagespec)) bitsHeader.colors = B_RGBA32; else bitsHeader.colors = B_RGB32; @@ -2463,7 +2174,7 @@ translate_from_tga(BPositionIO *inSource, ssize_t amtread, uint8 *read, case TGA_NOCOMP_TRUECOLOR: case TGA_NOCOMP_BW: result = translate_from_tganm_to_bits(inSource, - outDestination, fileheader, mapspec, imagespec, settings); + outDestination, fileheader, mapspec, imagespec); break; case TGA_NOCOMP_COLORMAP: @@ -2474,12 +2185,12 @@ translate_from_tga(BPositionIO *inSource, ssize_t amtread, uint8 *read, case TGA_RLE_TRUECOLOR: case TGA_RLE_BW: result = translate_from_tganmrle_to_bits(inSource, - outDestination, fileheader, mapspec, imagespec, settings); + outDestination, fileheader, mapspec, imagespec); break; case TGA_RLE_COLORMAP: result = translate_from_tgamrle_to_bits(inSource, outDestination, - fileheader, mapspec, imagespec, settings, ptgapalette); + fileheader, mapspec, imagespec, ptgapalette); break; default: @@ -2496,130 +2207,27 @@ translate_from_tga(BPositionIO *inSource, ssize_t amtread, uint8 *read, return B_NO_TRANSLATOR; } -// --------------------------------------------------------------- -// Translate -// -// Translates the data in inSource to the type outType and stores -// the translated data in outDestination. -// -// Preconditions: -// -// Parameters: inSource, the data to be translated -// -// inInfo, hint about the data in inSource (not used) -// -// ioExtension, configuration options for the -// translator -// -// outType, the type to convert inSource to -// -// outDestination, where the translated data is -// put -// -// Postconditions: -// -// Returns: B_BAD_VALUE, if the options in ioExtension are bad -// -// B_NO_TRANSLATOR, if this translator doesn't understand the data -// -// B_ERROR, if there was an error allocating memory or converting -// data -// -// B_OK, if all went well -// --------------------------------------------------------------- -status_t -TGATranslator::Translate(BPositionIO *inSource, - const translator_info *inInfo, BMessage *ioExtension, - uint32 outType, BPositionIO *outDestination) +status_t +TGATranslator::DerivedTranslate(BPositionIO *inSource, + const translator_info *inInfo, BMessage *ioExtension, uint32 outType, + BPositionIO *outDestination, int32 baseType) { - if (!outType) - outType = B_TRANSLATOR_BITMAP; - if (outType != B_TRANSLATOR_BITMAP && outType != B_TGA_FORMAT) - return B_NO_TRANSLATOR; - - inSource->Seek(0, SEEK_SET); - - uint8 ch[4]; - uint32 nbits = B_TRANSLATOR_BITMAP; - - // 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 - 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 - 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 translate_from_bits(inSource, 4, ch, *fpsettings, - outType, outDestination); - // If NOT B_TRANSLATOR_BITMAP type, - // it could be the TGA format - // (The TGA format does not have a magic number at the head of the file) + if (baseType == 1) + // if inSource is in bits format + return translate_from_bits(inSource, outType, outDestination); + else if (baseType == 0) + // if inSource is NOT in bits format + return translate_from_tga(inSource, outType, outDestination); else - return translate_from_tga(inSource, 4, ch, *fpsettings, - outType, outDestination); + // if BaseTranslator did not properly identify the data as + // bits or not bits + return B_NO_TRANSLATOR; } -// returns the current translator settings into ioExtension -status_t -TGATranslator::GetConfigurationMessage(BMessage *ioExtension) +BView * +TGATranslator::NewConfigView(TranslatorSettings *settings) { - return fpsettings->GetConfigurationMessage(ioExtension); + return new TGAView(BRect(0, 0, 225, 175), "TGATranslator Settings", + B_FOLLOW_ALL, B_WILL_DRAW, settings); } -// --------------------------------------------------------------- -// 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: -// --------------------------------------------------------------- -status_t -TGATranslator::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; - - TGAView *view = new TGAView(BRect(0, 0, 225, 175), - "TGATranslator Settings", B_FOLLOW_ALL, B_WILL_DRAW, - AcquireSettings()); - *outView = view; - *outExtent = view->Bounds(); - - return B_OK; -} - -TGATranslatorSettings * -TGATranslator::AcquireSettings() -{ - return fpsettings->Acquire(); -} diff --git a/src/add-ons/translators/tgatranslator/TGATranslator.h b/src/add-ons/translators/tgatranslator/TGATranslator.h index 0c49fe2655..42225be111 100644 --- a/src/add-ons/translators/tgatranslator/TGATranslator.h +++ b/src/add-ons/translators/tgatranslator/TGATranslator.h @@ -38,9 +38,9 @@ #include #include #include -#include "TGATranslatorSettings.h" +#include "BaseTranslator.h" -#define TGA_TRANSLATOR_VERSION 0x100 +#define TGA_TRANSLATOR_VERSION B_TRANSLATION_MAKE_VER(1,0,0) #define TGA_IN_QUALITY 0.7 #define TGA_IN_CAPABILITY 0.8 #define TGA_OUT_QUALITY 0.7 @@ -51,6 +51,10 @@ #define BBT_OUT_QUALITY 0.6 #define BBT_OUT_CAPABILITY 0.8 +// TGA Translator Settings +#define TGA_SETTING_RLE "tga /rle" +#define TGA_SETTING_IGNORE_ALPHA "tga /ignore_alpha" + // TGA files are stored in the Intel byte order :) struct TGAFileHeader { uint8 idlength; @@ -125,52 +129,19 @@ struct TGAImageSpec { #define TGA_STREAM_BUFFER_SIZE 1024 -class TGATranslator : public BTranslator { +class TGATranslator : public BaseTranslator { public: TGATranslator(); - 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 - - TGATranslatorSettings *AcquireSettings(); + virtual BView *NewConfigView(TranslatorSettings *settings); protected: virtual ~TGATranslator(); @@ -179,10 +150,28 @@ protected: // the user private: - TGATranslatorSettings *fpsettings; + uint8 tga_alphabits(TGAFileHeader &filehead, TGAColorMapSpec &mapspec, + TGAImageSpec &imagespec); + + status_t translate_from_bits(BPositionIO *inSource, uint32 outType, + BPositionIO *outDestination); + + status_t translate_from_tganm_to_bits(BPositionIO *inSource, + BPositionIO *outDestination, TGAFileHeader &filehead, + TGAColorMapSpec &mapspec, TGAImageSpec &imagespec); + + status_t translate_from_tganmrle_to_bits(BPositionIO *inSource, + BPositionIO *outDestination, TGAFileHeader &filehead, + TGAColorMapSpec &mapspec, TGAImageSpec &imagespec); + + status_t translate_from_tgamrle_to_bits(BPositionIO *inSource, + BPositionIO *outDestination, TGAFileHeader &filehead, + TGAColorMapSpec &mapspec, TGAImageSpec &imagespec, uint8 *pmap); + + status_t translate_from_tga(BPositionIO *inSource, uint32 outType, + BPositionIO *outDestination); - char fName[30]; - char fInfo[100]; }; #endif // #ifndef TGA_TRANSLATOR_H + diff --git a/src/add-ons/translators/tgatranslator/TGATranslatorSettings.cpp b/src/add-ons/translators/tgatranslator/TGATranslatorSettings.cpp deleted file mode 100644 index 2064335fb9..0000000000 --- a/src/add-ons/translators/tgatranslator/TGATranslatorSettings.cpp +++ /dev/null @@ -1,474 +0,0 @@ -/*****************************************************************************/ -// TGATranslatorSettings -// Written by Michael Wilber, OBOS Translation Kit Team -// -// TGATranslatorSettings.cpp -// -// This class manages (saves/loads/locks/unlocks) the settings -// for the TGATranslator. -// -// -// Copyright (c) 2002 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 -#include -#include - // for B_TRANSLATOR_EXT_* -#include "TGATranslatorSettings.h" - -// --------------------------------------------------------------- -// Constructor -// -// Sets the default settings, location for the settings file -// and sets the reference count to 1 -// -// Preconditions: -// -// Parameters: -// -// Postconditions: -// -// Returns: -// --------------------------------------------------------------- -TGATranslatorSettings::TGATranslatorSettings() - : flock("TGA Settings Lock") -{ - if (find_directory(B_USER_SETTINGS_DIRECTORY, &fsettingsPath)) - fsettingsPath.SetTo("/tmp"); - fsettingsPath.Append(TGA_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.AddBool(TGA_SETTING_RLE, false); - // RLE compression is off by default - fmsgSettings.AddBool(TGA_SETTING_IGNORE_ALPHA, false); - // Don't ignore the alpha channel in TGA files by default -} - -// --------------------------------------------------------------- -// Acquire -// -// Returns a pointer to the TGATranslatorSettings and increments -// the reference count. -// -// Preconditions: -// -// Parameters: -// -// Postconditions: -// -// Returns: pointer to this TGATranslatorSettings object -// --------------------------------------------------------------- -TGATranslatorSettings * -TGATranslatorSettings::Acquire() -{ - TGATranslatorSettings *psettings = NULL; - - flock.Lock(); - frefCount++; - psettings = this; - flock.Unlock(); - - return psettings; -} - -// --------------------------------------------------------------- -// Release -// -// Decrements the reference count and deletes the -// TGATranslatorSettings if the reference count is zero. -// -// Preconditions: -// -// Parameters: -// -// Postconditions: -// -// Returns: pointer to this TGATranslatorSettings object if -// the reference count is greater than zero, returns NULL -// if the reference count is zero and the TGATranslatorSettings -// object has been deleted -// --------------------------------------------------------------- -TGATranslatorSettings * -TGATranslatorSettings::Release() -{ - TGATranslatorSettings *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: -// --------------------------------------------------------------- -TGATranslatorSettings::~TGATranslatorSettings() -{ -} - -// --------------------------------------------------------------- -// 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 -TGATranslatorSettings::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 -TGATranslatorSettings::LoadSettings(BMessage *pmsg) -{ - status_t result = B_BAD_VALUE; - - if (pmsg) { - // Make certain that no TGA settings - // are missing from the file - bool bheaderOnly, bdataOnly, brle, bignore; - - 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->FindBool(TGA_SETTING_RLE, &brle); - if (result != B_OK) - brle = SetGetRLE(); - result = pmsg->FindBool(TGA_SETTING_IGNORE_ALPHA, &bignore); - if (result != B_OK) - bignore = SetGetIgnoreAlpha(); - - 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.ReplaceBool(TGA_SETTING_RLE, brle); - if (result == B_OK) - result = fmsgSettings.ReplaceBool(TGA_SETTING_IGNORE_ALPHA, - bignore); - } - 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 -TGATranslatorSettings::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 -TGATranslatorSettings::GetConfigurationMessage(BMessage *pmsg) -{ - status_t result = B_BAD_VALUE; - - if (pmsg) { - const char *kNames[] = { - B_TRANSLATOR_EXT_HEADER_ONLY, - B_TRANSLATOR_EXT_DATA_ONLY, - TGA_SETTING_RLE, - TGA_SETTING_IGNORE_ALPHA - }; - 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->AddBool(TGA_SETTING_RLE, SetGetRLE()); - if (result == B_OK) - result = pmsg->AddBool(TGA_SETTING_IGNORE_ALPHA, - SetGetIgnoreAlpha()); - - 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 -TGATranslatorSettings::SetGetHeaderOnly(bool *pbHeaderOnly) -{ - bool bprevValue; - - 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 -TGATranslatorSettings::SetGetDataOnly(bool *pbDataOnly) -{ - bool bprevValue; - - flock.Lock(); - fmsgSettings.FindBool(B_TRANSLATOR_EXT_DATA_ONLY, &bprevValue); - if (pbDataOnly) - fmsgSettings.ReplaceBool(B_TRANSLATOR_EXT_DATA_ONLY, *pbDataOnly); - flock.Unlock(); - - return bprevValue; -} - -// --------------------------------------------------------------- -// SetGetRLE -// -// Sets the state of the RLE setting (if pbRLE is not NULL) -// and returns the previous value of the RLE setting. -// -// If the RLE setting is true, TGA images created by the -// TGATranslator will be RLE compressed. -// -// Preconditions: -// -// Parameters: pbRLE pointer to bool which specifies -// the new value for the RLE setting -// -// Postconditions: -// -// Returns: the prior value of the RLE setting -// --------------------------------------------------------------- -bool -TGATranslatorSettings::SetGetRLE(bool *pbRLE) -{ - bool bprevValue; - - flock.Lock(); - fmsgSettings.FindBool(TGA_SETTING_RLE, &bprevValue); - if (pbRLE) - fmsgSettings.ReplaceBool(TGA_SETTING_RLE, *pbRLE); - flock.Unlock(); - - return bprevValue; -} - -// --------------------------------------------------------------- -// SetGetIgnoreAlpha -// -// Sets the state of the ignore alpha setting (if pbIgnoreAlpha -// is not NULL) and returns the previous value of the setting. -// -// If ignore alpha is true, the alpha channel from TGA images -// will not be included when they are converted to BBitmp images. -// -// Preconditions: -// -// Parameters: pbIgnoreAlpha pointer to bool which specifies -// the new ignore alpha setting -// -// Postconditions: -// -// Returns: the prior value of the ignore alpha setting -// --------------------------------------------------------------- -bool -TGATranslatorSettings::SetGetIgnoreAlpha(bool *pbIgnoreAlpha) -{ - bool bprevValue; - - flock.Lock(); - fmsgSettings.FindBool(TGA_SETTING_IGNORE_ALPHA, &bprevValue); - if (pbIgnoreAlpha) - fmsgSettings.ReplaceBool(TGA_SETTING_IGNORE_ALPHA, *pbIgnoreAlpha); - flock.Unlock(); - - return bprevValue; -} diff --git a/src/add-ons/translators/tgatranslator/TGATranslatorSettings.h b/src/add-ons/translators/tgatranslator/TGATranslatorSettings.h deleted file mode 100644 index 6a19091793..0000000000 --- a/src/add-ons/translators/tgatranslator/TGATranslatorSettings.h +++ /dev/null @@ -1,94 +0,0 @@ -/*****************************************************************************/ -// TGATranslatorSettings -// Written by Michael Wilber, OBOS Translation Kit Team -// -// TGATranslatorSettings.h -// -// This class manages (saves/loads/locks/unlocks) the settings -// for the TGATranslator. -// -// -// Copyright (c) 2002 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 TGA_TRANSLATOR_SETTINGS_H -#define TGA_TRANSLATOR_SETTINGS_H - -#include -#include -#include - -#define TGA_SETTINGS_FILENAME "TGATranslator_Settings" - -// TGA Translator Settings -#define TGA_SETTING_RLE "tga /rle" -#define TGA_SETTING_IGNORE_ALPHA "tga /ignore_alpha" - -class TGATranslatorSettings { -public: - TGATranslatorSettings(); - - TGATranslatorSettings *Acquire(); - // increments the reference count, returns this - TGATranslatorSettings *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 - bool SetGetRLE(bool *pbRLE = NULL); - // sets / gets RLE setting - // specifies if RLE compression will be used - // when the TGATranslator creates TGA images - bool SetGetIgnoreAlpha(bool *pbIgnoreAlpha = NULL); - // sets / gets ignore alpha setting - // specifies whether or not TGATranslator uses - // the alpha data from TGA files - -private: - ~TGATranslatorSettings(); - // 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 TGA_TRANSLATOR_SETTTINGS_H diff --git a/src/add-ons/translators/tgatranslator/TGAView.cpp b/src/add-ons/translators/tgatranslator/TGAView.cpp index cedcb72f74..6838239942 100644 --- a/src/add-ons/translators/tgatranslator/TGAView.cpp +++ b/src/add-ons/translators/tgatranslator/TGAView.cpp @@ -47,10 +47,10 @@ // Returns: // --------------------------------------------------------------- TGAView::TGAView(const BRect &frame, const char *name, - uint32 resize, uint32 flags, TGATranslatorSettings *psettings) + uint32 resize, uint32 flags, TranslatorSettings *settings) : BView(frame, name, resize, flags) { - fpsettings = psettings; + fSettings = settings; SetViewColor(220,220,220,0); @@ -61,7 +61,7 @@ TGAView::TGAView(const BRect &frame, const char *name, fpchkIgnoreAlpha = new BCheckBox(BRect(10, 45, 180, 62), "Ignore TGA alpha channel", "Ignore TGA alpha channel", pmsg); - val = (psettings->SetGetIgnoreAlpha()) ? 1 : 0; + val = (fSettings->SetGetBool(TGA_SETTING_IGNORE_ALPHA)) ? 1 : 0; fpchkIgnoreAlpha->SetValue(val); fpchkIgnoreAlpha->SetViewColor(ViewColor()); AddChild(fpchkIgnoreAlpha); @@ -70,7 +70,7 @@ TGAView::TGAView(const BRect &frame, const char *name, fpchkRLE = new BCheckBox(BRect(10, 67, 180, 84), "Save with RLE Compression", "Save with RLE Compression", pmsg); - val = (psettings->SetGetRLE()) ? 1 : 0; + val = (fSettings->SetGetBool(TGA_SETTING_RLE)) ? 1 : 0; fpchkRLE->SetValue(val); fpchkRLE->SetViewColor(ViewColor()); AddChild(fpchkRLE); @@ -91,7 +91,7 @@ TGAView::TGAView(const BRect &frame, const char *name, // --------------------------------------------------------------- TGAView::~TGAView() { - fpsettings->Release(); + fSettings->Release(); } // --------------------------------------------------------------- @@ -138,8 +138,8 @@ TGAView::MessageReceived(BMessage *message) bnewval = true; else bnewval = false; - fpsettings->SetGetIgnoreAlpha(&bnewval); - fpsettings->SaveSettings(); + fSettings->SetGetBool(TGA_SETTING_IGNORE_ALPHA, &bnewval); + fSettings->SaveSettings(); break; case CHANGE_RLE: @@ -147,8 +147,8 @@ TGAView::MessageReceived(BMessage *message) bnewval = true; else bnewval = false; - fpsettings->SetGetRLE(&bnewval); - fpsettings->SaveSettings(); + fSettings->SetGetBool(TGA_SETTING_RLE, &bnewval); + fSettings->SaveSettings(); break; default: @@ -191,9 +191,10 @@ TGAView::Draw(BRect area) char detail[100]; sprintf(detail, "Version %d.%d.%d %s", - static_cast(TGA_TRANSLATOR_VERSION >> 8), - static_cast((TGA_TRANSLATOR_VERSION >> 4) & 0xf), - static_cast(TGA_TRANSLATOR_VERSION & 0xf), __DATE__); + static_cast(B_TRANSLATION_MAJOR_VER(TGA_TRANSLATOR_VERSION)), + static_cast(B_TRANSLATION_MINOR_VER(TGA_TRANSLATOR_VERSION)), + static_cast(B_TRANSLATION_REVSN_VER(TGA_TRANSLATOR_VERSION)), + __DATE__); DrawString(detail, BPoint(xbold, yplain + ybold)); /* char copyright[] = "© 2002 OpenBeOS Project"; DrawString(copyright, BPoint(xbold, yplain * 2 + ybold)); diff --git a/src/add-ons/translators/tgatranslator/TGAView.h b/src/add-ons/translators/tgatranslator/TGAView.h index 65ffaaf564..0f23e47e7f 100644 --- a/src/add-ons/translators/tgatranslator/TGAView.h +++ b/src/add-ons/translators/tgatranslator/TGAView.h @@ -33,12 +33,12 @@ #include #include -#include "TGATranslatorSettings.h" +#include "TranslatorSettings.h" class TGAView : public BView { public: TGAView(const BRect &frame, const char *name, uint32 resize, - uint32 flags, TGATranslatorSettings *psettings); + uint32 flags, TranslatorSettings *settings); // sets up the view ~TGAView(); @@ -55,7 +55,7 @@ private: BCheckBox *fpchkIgnoreAlpha; BCheckBox *fpchkRLE; - TGATranslatorSettings *fpsettings; + TranslatorSettings *fSettings; // the actual settings for the translator, // shared with the translator }; diff --git a/src/add-ons/translators/tgatranslator/TGAWindow.cpp b/src/add-ons/translators/tgatranslator/TGAWindow.cpp deleted file mode 100644 index fe65a9695f..0000000000 --- a/src/add-ons/translators/tgatranslator/TGAWindow.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/*****************************************************************************/ -// TGAWindow -// Written by Michael Wilber, OBOS Translation Kit Team -// -// TGAWindow.cpp -// -// This BWindow based object is used to hold the TGAView object when the -// user runs the TGATranslator as an application. -// -// -// Copyright (c) 2002 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 "TGAWindow.h" - -// --------------------------------------------------------------- -// Constructor -// -// Sets up the BWindow for holding a TGAView -// -// Preconditions: -// -// Parameters: area, The bounds of the window -// -// Postconditions: -// -// Returns: -// --------------------------------------------------------------- -TGAWindow::TGAWindow(BRect area) - : BWindow(area, "TGATranslator", 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: -// --------------------------------------------------------------- -TGAWindow::~TGAWindow() -{ - be_app->PostMessage(B_QUIT_REQUESTED); -} diff --git a/src/add-ons/translators/tgatranslator/TGAWindow.h b/src/add-ons/translators/tgatranslator/TGAWindow.h deleted file mode 100644 index 7b503b9a80..0000000000 --- a/src/add-ons/translators/tgatranslator/TGAWindow.h +++ /dev/null @@ -1,48 +0,0 @@ -/*****************************************************************************/ -// TGAWindow -// Written by Michael Wilber, OBOS Translation Kit Team -// -// TGAWindow.h -// -// This BWindow based object is used to hold the TGAView object when the -// user runs the TGATranslator as an application. -// -// -// Copyright (c) 2002 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 TGAWINDOW_H -#define TGAWINDOW_H - -#include -#include -#include - -class TGAWindow : public BWindow { -public: - TGAWindow(BRect area); - // Sets up a BWindow with bounds area - - ~TGAWindow(); - // Posts a quit message so that the application closes properly -}; - -#endif