* Applied the changes I made to the JPEG translator to the JPEG2000 translator as well.

* Further cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19141 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2006-10-29 12:45:02 +00:00
parent 69b17409a0
commit 117da2d75f
4 changed files with 975 additions and 1120 deletions
+28 -74
View File
@@ -105,15 +105,14 @@ SaveSettings(jpeg_settings *settings)
{ {
// Make path to settings file // Make path to settings file
BPath path; BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) { if (find_directory(B_USER_SETTINGS_DIRECTORY, &path, true) != B_OK)
path.SetTo(SETTINGS_PATH); return;
path.Append(SETTINGS_FILE);
} else path.Append(SETTINGS_FILE);
path.Append(SETTINGS_FILE);
// Open settings file (create it if there's no file) and write settings // Open settings file (create it if there's no file) and write settings
FILE *file = NULL; FILE *file = NULL;
if ((file = fopen( path.Path(), "wb+"))) { if ((file = fopen(path.Path(), "wb+"))) {
fwrite(settings, sizeof(jpeg_settings), 1, file); fwrite(settings, sizeof(jpeg_settings), 1, file);
fclose(file); fclose(file);
} }
@@ -155,10 +154,11 @@ LoadSettings(jpeg_settings *settings)
// Make path to settings file // Make path to settings file
BPath path; BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) { if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) {
path.SetTo(SETTINGS_PATH); LoadDefaultSettings(settings);
path.Append(SETTINGS_FILE); return;
} else }
path.Append(SETTINGS_FILE);
path.Append(SETTINGS_FILE);
// Open settings file (create it if there's no file) and write settings // Open settings file (create it if there's no file) and write settings
FILE *file = NULL; FILE *file = NULL;
@@ -485,7 +485,7 @@ SSlider::UpdateText() const
void void
SSlider::ResizeToPreferred() SSlider::ResizeToPreferred()
{ {
int32 width = (int32)ceil(StringWidth( Label()) + StringWidth("9999")); int32 width = (int32)ceil(StringWidth(Label()) + StringWidth("9999"));
if (width < 230) if (width < 230)
width = 230; width = 230;
@@ -503,7 +503,7 @@ TranslatorReadView::TranslatorReadView(const char *name, jpeg_settings *settings
: SView(name, x, y), : SView(name, x, y),
fSettings(settings) fSettings(settings)
{ {
fAlwaysRGB32 = new BCheckBox( BRect(10, GetPreferredHeight(), 10, fAlwaysRGB32 = new BCheckBox(BRect(10, GetPreferredHeight(), 10,
GetPreferredHeight()), "alwaysrgb32", VIEW_LABEL_ALWAYSRGB32, GetPreferredHeight()), "alwaysrgb32", VIEW_LABEL_ALWAYSRGB32,
new BMessage(VIEW_MSG_SET_ALWAYSRGB32)); new BMessage(VIEW_MSG_SET_ALWAYSRGB32));
fAlwaysRGB32->SetFont(be_plain_font); fAlwaysRGB32->SetFont(be_plain_font);
@@ -512,7 +512,7 @@ TranslatorReadView::TranslatorReadView(const char *name, jpeg_settings *settings
AddChild(fAlwaysRGB32); AddChild(fAlwaysRGB32);
fPhotoshopCMYK = new BCheckBox( BRect(10, GetPreferredHeight(), 10, fPhotoshopCMYK = new BCheckBox(BRect(10, GetPreferredHeight(), 10,
GetPreferredHeight()), "photoshopCMYK", VIEW_LABEL_PHOTOSHOPCMYK, GetPreferredHeight()), "photoshopCMYK", VIEW_LABEL_PHOTOSHOPCMYK,
new BMessage(VIEW_MSG_SET_PHOTOSHOPCMYK)); new BMessage(VIEW_MSG_SET_PHOTOSHOPCMYK));
fPhotoshopCMYK->SetFont(be_plain_font); fPhotoshopCMYK->SetFont(be_plain_font);
@@ -521,7 +521,7 @@ TranslatorReadView::TranslatorReadView(const char *name, jpeg_settings *settings
AddChild(fPhotoshopCMYK); AddChild(fPhotoshopCMYK);
fShowErrorBox = new BCheckBox( BRect(10, GetPreferredHeight(), 10, fShowErrorBox = new BCheckBox(BRect(10, GetPreferredHeight(), 10,
GetPreferredHeight()), "error", VIEW_LABEL_SHOWREADERRORBOX, GetPreferredHeight()), "error", VIEW_LABEL_SHOWREADERRORBOX,
new BMessage(VIEW_MSG_SET_SHOWREADERRORBOX)); new BMessage(VIEW_MSG_SET_SHOWREADERRORBOX));
fShowErrorBox->SetFont(be_plain_font); fShowErrorBox->SetFont(be_plain_font);
@@ -609,7 +609,7 @@ TranslatorWriteView::TranslatorWriteView(const char *name, jpeg_settings *settin
fSmoothingSlider->SetValue(fSettings->Smoothing); fSmoothingSlider->SetValue(fSettings->Smoothing);
AddChild(fSmoothingSlider); AddChild(fSmoothingSlider);
fProgress = new BCheckBox( BRect(10, GetPreferredHeight()+10, 10, fProgress = new BCheckBox(BRect(10, GetPreferredHeight()+10, 10,
GetPreferredHeight()), "progress", VIEW_LABEL_PROGRESSIVE, GetPreferredHeight()), "progress", VIEW_LABEL_PROGRESSIVE,
new BMessage(VIEW_MSG_SET_PROGRESSIVE)); new BMessage(VIEW_MSG_SET_PROGRESSIVE));
fProgress->SetFont(be_plain_font); fProgress->SetFont(be_plain_font);
@@ -618,8 +618,8 @@ TranslatorWriteView::TranslatorWriteView(const char *name, jpeg_settings *settin
AddChild(fProgress); AddChild(fProgress);
fOptimizeColors = new BCheckBox( BRect(10, GetPreferredHeight()+5, 10, fOptimizeColors = new BCheckBox(BRect(10, GetPreferredHeight()+5, 10,
GetPreferredHeight()+5), "optimizecolors", VIEW_LABEL_OPTIMIZECOLORS, GetPreferredHeight() + 5), "optimizecolors", VIEW_LABEL_OPTIMIZECOLORS,
new BMessage(VIEW_MSG_SET_OPTIMIZECOLORS)); new BMessage(VIEW_MSG_SET_OPTIMIZECOLORS));
fOptimizeColors->SetFont(be_plain_font); fOptimizeColors->SetFont(be_plain_font);
if (fSettings->OptimizeColors) if (fSettings->OptimizeColors)
@@ -627,8 +627,8 @@ TranslatorWriteView::TranslatorWriteView(const char *name, jpeg_settings *settin
AddChild(fOptimizeColors); AddChild(fOptimizeColors);
fSmallerFile = new BCheckBox( BRect(25, GetPreferredHeight()+5, 25, fSmallerFile = new BCheckBox(BRect(25, GetPreferredHeight()+5, 25,
GetPreferredHeight()+5), "smallerfile", VIEW_LABEL_SMALLERFILE, GetPreferredHeight() + 5), "smallerfile", VIEW_LABEL_SMALLERFILE,
new BMessage(VIEW_MSG_SET_SMALLERFILE)); new BMessage(VIEW_MSG_SET_SMALLERFILE));
fSmallerFile->SetFont(be_plain_font); fSmallerFile->SetFont(be_plain_font);
if (fSettings->SmallerFile) if (fSettings->SmallerFile)
@@ -638,7 +638,7 @@ TranslatorWriteView::TranslatorWriteView(const char *name, jpeg_settings *settin
AddChild(fSmallerFile); AddChild(fSmallerFile);
fGrayAsRGB24 = new BCheckBox( BRect(10, GetPreferredHeight()+5, 25, fGrayAsRGB24 = new BCheckBox(BRect(10, GetPreferredHeight()+5, 25,
GetPreferredHeight()+5), "gray1asrgb24", VIEW_LABEL_GRAY1ASRGB24, GetPreferredHeight()+5), "gray1asrgb24", VIEW_LABEL_GRAY1ASRGB24,
new BMessage(VIEW_MSG_SET_GRAY1ASRGB24)); new BMessage(VIEW_MSG_SET_GRAY1ASRGB24));
fGrayAsRGB24->SetFont(be_plain_font); fGrayAsRGB24->SetFont(be_plain_font);
@@ -861,54 +861,6 @@ TranslatorView::AttachedToWindow()
while ((child = ChildAt(index++)) != NULL) while ((child = ChildAt(index++)) != NULL)
child->Hide(); child->Hide();
#if 0
// Hack for DataTranslations which doesn't resize visible area to requested by view
// which makes some parts of bigger than usual translationviews out of visible area
// so if it was loaded to DataTranslations resize window if needed
BWindow *window = Window();
if (!strcmp(window->Name(), "DataTranslations")) {
BView *view = Parent();
if (view) {
BRect frame = view->Frame();
if (frame.Width() < GetPreferredWidth()
|| (frame.Height()-48) < GetPreferredHeight()) {
float x = ceil(GetPreferredWidth() - frame.Width());
float y = ceil(GetPreferredHeight() - (frame.Height()-48));
if (x < 0)
x = 0;
if (y < 0)
y = 0;
// DataTranslations has main view called "Background"
// change it's resizing mode so it will always resize with window
// also make sure view will be redrawed after resize
view = window->FindView("Background");
if (view) {
view->SetResizingMode(B_FOLLOW_ALL);
view->SetFlags(B_FULL_UPDATE_ON_RESIZE);
}
// The same with "Info..." button, except redrawing, which isn't needed
view = window->FindView("Info…");
if (view)
view->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
window->ResizeBy(x, y);
// Let user resize window if resizing option is not already there...
uint32 flags = window->Flags();
if (flags & B_NOT_RESIZABLE) {
// ...but first prevent too small window (so "Info..." button will not look strange ;)
// max will be 800x600 which should be enough for now
window->SetSizeLimits(400, 800, 66, 600);
flags ^= B_NOT_RESIZABLE;
window->SetFlags(flags);
}
}
}
}
#endif
} }
@@ -1000,7 +952,8 @@ TranslatorView::MouseDown(BPoint where)
TranslatorWindow::TranslatorWindow(bool quitOnClose) TranslatorWindow::TranslatorWindow(bool quitOnClose)
: BWindow(BRect(100, 100, 100, 100), "JPEG Settings", B_TITLED_WINDOW, B_NOT_ZOOMABLE) : BWindow(BRect(100, 100, 100, 100), "JPEG Settings", B_TITLED_WINDOW,
B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS)
{ {
BRect extent(0, 0, 0, 0); BRect extent(0, 0, 0, 0);
BView *config = NULL; BView *config = NULL;
@@ -1424,7 +1377,7 @@ Decompress(BPositionIO *in, BPositionIO *out)
jpeg_start_decompress(&cinfo); jpeg_start_decompress(&cinfo);
// !!! Initialize this bounds rect to the size of your image // !!! Initialize this bounds rect to the size of your image
BRect bounds( 0, 0, cinfo.output_width-1, cinfo.output_height-1); BRect bounds(0, 0, cinfo.output_width-1, cinfo.output_height-1);
// Bytes count in one line of image (scanline) // Bytes count in one line of image (scanline)
int64 row_bytes = cinfo.output_width * out_color_components; int64 row_bytes = cinfo.output_width * out_color_components;
@@ -1504,12 +1457,13 @@ Error(j_common_ptr cinfo, status_t error)
int int
main() { main(int, char**)
BApplication app("application/x-vnd.Shard.JPEGTranslator"); {
BApplication app("application/x-vnd.Haiku-JPEGTranslator");
TranslatorWindow *window = new TranslatorWindow(); TranslatorWindow *window = new TranslatorWindow();
window->Show(); window->Show();
app.Run(); app.Run();
return 0; return 0;
} }
+5 -14
View File
@@ -50,8 +50,7 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Settings // Settings
#define SETTINGS_FILE "OpenJPEGTranslator" #define SETTINGS_FILE "JPEGTranslator"
#define SETTINGS_PATH "/boot/home/config/settings"
// View messages // View messages
#define VIEW_MSG_SET_QUALITY 'JSCQ' #define VIEW_MSG_SET_QUALITY 'JSCQ'
@@ -111,7 +110,6 @@ class SSlider : public BSlider {
mutable char fStatusLabel[12]; mutable char fStatusLabel[12];
}; };
//! Basic view class with resizing to needed size //! Basic view class with resizing to needed size
class SView : public BView { class SView : public BView {
public: public:
@@ -133,7 +131,6 @@ class SView : public BView {
float fPreferredHeight; float fPreferredHeight;
}; };
//! Configuration view for reading settings //! Configuration view for reading settings
class TranslatorReadView : public SView { class TranslatorReadView : public SView {
public: public:
@@ -150,8 +147,7 @@ class TranslatorReadView : public SView {
BCheckBox* fShowErrorBox; BCheckBox* fShowErrorBox;
}; };
//! Configuration view for writing settings
// !Configuration view for writing settings
class TranslatorWriteView : public SView { class TranslatorWriteView : public SView {
public: public:
TranslatorWriteView(const char* name, jpeg_settings* settings, TranslatorWriteView(const char* name, jpeg_settings* settings,
@@ -170,13 +166,11 @@ class TranslatorWriteView : public SView {
BCheckBox* fGrayAsRGB24; BCheckBox* fGrayAsRGB24;
}; };
class TranslatorAboutView : public SView { class TranslatorAboutView : public SView {
public: public:
TranslatorAboutView(const char* name, float x = 0, float y = 0); TranslatorAboutView(const char* name, float x = 0, float y = 0);
}; };
//! Configuration view //! Configuration view
class TranslatorView : public SView { class TranslatorView : public SView {
public: public:
@@ -197,9 +191,7 @@ class TranslatorView : public SView {
int32 fActiveChild; int32 fActiveChild;
}; };
//--------------------------------------------------- //! Window used for configuration
// Window used for configuration
//---------------------------------------------------
class TranslatorWindow : public BWindow { class TranslatorWindow : public BWindow {
public: public:
TranslatorWindow(bool quitOnClose = true); TranslatorWindow(bool quitOnClose = true);
@@ -222,9 +214,8 @@ EXTERN(void) be_jpeg_stdio_dest(j_compress_ptr cinfo, BPositionIO *outfile); //
//--------------------------------------------------- //---------------------------------------------------
EXTERN(struct jpeg_error_mgr *) be_jpeg_std_error (struct jpeg_error_mgr * err, jpeg_settings * settings); // from "be_jerror.cpp" EXTERN(struct jpeg_error_mgr *) be_jpeg_std_error (struct jpeg_error_mgr * err, jpeg_settings * settings); // from "be_jerror.cpp"
//---------------------------------------------------
// Main functions of translator :) // Main functions of translator :)
//---------------------------------------------------
status_t Copy(BPositionIO *in, BPositionIO *out); status_t Copy(BPositionIO *in, BPositionIO *out);
status_t Compress(BPositionIO *in, BPositionIO *out); status_t Compress(BPositionIO *in, BPositionIO *out);
status_t Decompress(BPositionIO *in, BPositionIO *out); status_t Decompress(BPositionIO *in, BPositionIO *out);
File diff suppressed because it is too large Load Diff
@@ -33,12 +33,6 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define _JP2TRANSLATOR_H_ #define _JP2TRANSLATOR_H_
//----------------------------------------------------------------------------
//
// Include
//
//----------------------------------------------------------------------------
#include <Alert.h> #include <Alert.h>
#include <Application.h> #include <Application.h>
#include <CheckBox.h> #include <CheckBox.h>
@@ -55,15 +49,9 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "libjasper/jasper.h" #include "libjasper/jasper.h"
//----------------------------------------------------------------------------
//
// Define & Global variables declaration
//
//----------------------------------------------------------------------------
// Settings // Settings
#define SETTINGS_FILE "OpenJPEG2000Translator" #define SETTINGS_FILE "JPEG2000Translator"
#define SETTINGS_PATH "/boot/home/config/settings"
// View messages // View messages
#define VIEW_MSG_SET_QUALITY 'JSCQ' #define VIEW_MSG_SET_QUALITY 'JSCQ'
@@ -77,20 +65,9 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define VIEW_LABEL_JPC "Output only codestream (.jpc)" #define VIEW_LABEL_JPC "Output only codestream (.jpc)"
#define VIEW_LABEL_GRAYASRGB32 "Read Greyscale images as RGB32" #define VIEW_LABEL_GRAYASRGB32 "Read Greyscale images as RGB32"
// This will be used true if Settings are running, else false
extern bool AreSettingsRunning;
//---------------------------------------------------------------------------- //! Settings storage structure
// struct jpeg_settings {
// Classes
//
//----------------------------------------------------------------------------
//---------------------------------------------------
// Settings storage structure
//---------------------------------------------------
struct SETTINGS
{
// compression // compression
jpr_uchar_t Quality; // default: 25 jpr_uchar_t Quality; // default: 25
bool JPC; // default: false // compress to JPC or JP2? bool JPC; // default: false // compress to JPC or JP2?
@@ -99,595 +76,113 @@ struct SETTINGS
bool B_GRAY8_as_B_RGB32; // default: true bool B_GRAY8_as_B_RGB32; // default: true
}; };
//---------------------------------------------------
// Slider used in TranslatorView /*!
// With status showing actual value Slider used in TranslatorView
//--------------------------------------------------- With status showing actual value
class SSlider : public BSlider */
{ class SSlider : public BSlider {
public: public:
SSlider(BRect frame, const char *name, const char *label, BMessage *message, int32 minValue, int32 maxValue, orientation posture = B_HORIZONTAL, thumb_style thumbType = B_BLOCK_THUMB, uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP, uint32 flags = B_NAVIGABLE | B_WILL_DRAW | B_FRAME_EVENTS); SSlider(BRect frame, const char *name, const char *label,
char* UpdateText() const; BMessage *message, int32 minValue, int32 maxValue,
void ResizeToPreferred(); orientation posture = B_HORIZONTAL,
thumb_style thumbType = B_BLOCK_THUMB,
uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP,
uint32 flags = B_NAVIGABLE | B_WILL_DRAW | B_FRAME_EVENTS);
char* UpdateText() const;
void ResizeToPreferred();
private: private:
char statusLabel[12]; mutable char fStatusLabel[12];
}; };
//! Basic view class with resizing to needed size
//--------------------------------------------------- class SView : public BView {
// Basic view class with resizing to needed size
//---------------------------------------------------
class SView : public BView
{
public: public:
SView(const char *name, float x = 0, float y = 0) SView(const char* name, float x = 0, float y = 0);
:BView( BRect(x,y,x,y), name, B_FOLLOW_NONE, B_WILL_DRAW)
{ virtual void GetPreferredSize(float* _width, float* _height);
preferredWidth = 0; virtual void ResizeToPreferred();
preferredHeight = 0;
SetViewColor( ui_color(B_PANEL_BACKGROUND_COLOR)); void AddChild(BView* child, BView* before = NULL);
SetFont(be_plain_font);
}; float GetPreferredWidth()
void GetPreferredSize(float *width, float *height) { return fPreferredWidth; }
{ float GetPreferredHeight()
*width = preferredWidth; { return fPreferredHeight; }
*height = preferredHeight; void ResizePreferredBy(float width, float height);
}
inline float GetPreferredWidth() { return preferredWidth; };
inline float GetPreferredHeight() { return preferredHeight; };
inline void ResizePreferredBy(float width, float height) { preferredWidth += width; preferredHeight += height; };
inline void ResizeToPreferred() { ResizeTo(preferredWidth, preferredHeight); };
void AddChild(BView *child, BView *before = NULL)
{
BView::AddChild(child, before);
child->ResizeToPreferred();
BRect frame = child->Frame();
if (frame.right > preferredWidth)
preferredWidth = frame.right;
if (frame.bottom > preferredHeight)
preferredHeight = frame.bottom;
}
private: private:
float preferredWidth; float fPreferredWidth;
float preferredHeight; float fPreferredHeight;
}; };
//--------------------------------------------------- //! Configuration view for reading settings
// Configuration view for reading settings class TranslatorReadView : public SView {
//---------------------------------------------------
class TranslatorReadView : public SView
{
public: public:
TranslatorReadView(const char *name, SETTINGS *settings, float x = 0, float y = 0); TranslatorReadView(const char* name, jpeg_settings* settings,
void AttachedToWindow(); float x = 0, float y = 0);
void MessageReceived(BMessage *message);
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage* message);
private: private:
SETTINGS *Settings; jpeg_settings* fSettings;
BCheckBox *grayasrgb32; BCheckBox* fGrayAsRGB32;
}; };
//--------------------------------------------------- //! Configuration view for writing settings
// Configuration view for writing settings class TranslatorWriteView : public SView {
//---------------------------------------------------
class TranslatorWriteView : public SView
{
public: public:
TranslatorWriteView(const char *name, SETTINGS *settings, float x = 0, float y = 0); TranslatorWriteView(const char* name, jpeg_settings* settings,
void AttachedToWindow(); float x = 0, float y = 0);
void MessageReceived(BMessage *message);
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage* message);
private: private:
SETTINGS *Settings; jpeg_settings* fSettings;
SSlider *quality; SSlider* fQualitySlider;
BCheckBox *gray1asrgb24; BCheckBox* fGrayAsRGB24;
BCheckBox *jpc; BCheckBox* fCodeStreamOnly;
}; };
//--------------------------------------------------- class TranslatorAboutView : public SView {
// About view
//---------------------------------------------------
class TranslatorAboutView : public SView
{
public: public:
TranslatorAboutView(const char *name, float x = 0, float y = 0); TranslatorAboutView(const char* name, float x = 0, float y = 0);
}; };
//--------------------------------------------------- //! Configuration view
// Configuration view class TranslatorView : public SView {
//---------------------------------------------------
class TranslatorView : public SView
{
public: public:
TranslatorView(const char *name); TranslatorView(const char *name);
~TranslatorView() { AreSettingsRunning = false; }; virtual ~TranslatorView();
void AttachedToWindow();
void Draw(BRect updateRect); virtual void AttachedToWindow();
void MouseDown(BPoint where); virtual void Draw(BRect updateRect);
virtual void MouseDown(BPoint where);
private: private:
SETTINGS Settings; BRect _TabFrame(int32 index) const;
int32 tabWidth;
int32 tabHeight; jpeg_settings fSettings;
int32 activeChild; BList fTabs;
int32 fTabWidth;
int32 fTabHeight;
int32 fActiveChild;
}; };
//--------------------------------------------------- //! Window used for configuration
// Window used for configuration class TranslatorWindow : public BWindow {
//---------------------------------------------------
class TranslatorWindow : public BWindow
{
public: public:
TranslatorWindow(bool quit_on_close = true); TranslatorWindow(bool quitOnClose = true);
}; };
//---------------------------------------------------------------------------- // Main functions of translator :)
//
// Functions :: Settings
//
//----------------------------------------------------------------------------
//---------------------------------------------------
// Make Settings to defaults
//---------------------------------------------------
inline void
LoadDefaultSettings(SETTINGS *Settings)
{
Settings->Quality = 25;
Settings->JPC = false;
Settings->B_GRAY1_as_B_RGB24 = false;
Settings->B_GRAY8_as_B_RGB32 = true;
}
//---------------------------------------------------
// Save Settings to config file
//---------------------------------------------------
inline void
SaveSettings(SETTINGS *Settings)
{
// Make path to settings file
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) {
path.SetTo(SETTINGS_PATH);
path.Append(SETTINGS_FILE);
} else
path.Append(SETTINGS_FILE);
// Open settings file (create it if there's no file) and write settings
FILE *file = NULL;
if ((file = fopen( path.Path(), "wb+"))) {
fwrite(Settings, sizeof(SETTINGS), 1, file);
fclose(file);
}
}
//---------------------------------------------------
// Return true if Settings were run, false if not
//---------------------------------------------------
inline bool
SettingsChangedAlert()
{
// If settings view wasn't already initialized (settings not running)
// and user wants to run settings
if (!AreSettingsRunning && (new BAlert("Different settings file", "JPEG2000 settings were set to default because of incompatible settings file.", "Configure settings", "OK", NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT))->Go() == 0) {
// Create settings window (with no quit on close!), launch it and wait until it's closed
status_t err;
TranslatorWindow *window = new TranslatorWindow(false);
window->Show();
wait_for_thread(window->Thread(), &err);
return true;
}
return false;
}
//---------------------------------------------------
// Load settings from config file
// If can't find it make them default and try to save
//---------------------------------------------------
inline void
LoadSettings(SETTINGS *Settings)
{
// Make path to settings file
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) {
path.SetTo(SETTINGS_PATH);
path.Append(SETTINGS_FILE);
} else
path.Append(SETTINGS_FILE);
// Open settings file (create it if there's no file) and write settings
FILE *file = NULL;
if ((file = fopen( path.Path(), "rb"))) {
if ( !fread(Settings, sizeof(SETTINGS), 1, file)) {
// Settings struct has changed size
// Load default settings, and Save them
fclose(file);
LoadDefaultSettings(Settings);
SaveSettings(Settings);
// Tell user settings were changed to default, and ask to run settings panel or not
if (SettingsChangedAlert())
// User configured settings, load them again
LoadSettings(Settings);
} else
fclose(file);
} else if ((file = fopen( path.Path(), "wb+"))) {
LoadDefaultSettings(Settings);
fwrite(Settings, sizeof(SETTINGS), 1, file);
fclose(file);
// Tell user settings were changed to default, and ask to run settings panel or not
if (SettingsChangedAlert())
// User configured settings, load them again
LoadSettings(Settings);
}
}
//----------------------------------------------------------------------------
//
// Functions
//
//----------------------------------------------------------------------------
//---------------------------------------------------
// Main functions of translator :)
//---------------------------------------------------
status_t Copy(BPositionIO *in, BPositionIO *out); status_t Copy(BPositionIO *in, BPositionIO *out);
status_t Compress(BPositionIO *in, BPositionIO *out); status_t Compress(BPositionIO *in, BPositionIO *out);
status_t Decompress(BPositionIO *in, BPositionIO *out); status_t Decompress(BPositionIO *in, BPositionIO *out);
status_t Error(jas_stream_t *stream, jas_image_t *image, jas_matrix_t **pixels, int32 pixels_count, jpr_uchar_t *scanline, status_t error = B_ERROR); status_t Error(jas_stream_t *stream, jas_image_t *image, jas_matrix_t **pixels, int32 pixels_count, jpr_uchar_t *scanline, status_t error = B_ERROR);
//---------------------------------------------------
// Make RGB32 scanline from *pixels[3]
//---------------------------------------------------
inline void
read_rgb24_to_rgb32(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 index = 0;
int32 x = 0;
while (x < width)
{
scanline[index++] = (jpr_uchar_t)jas_matrix_getv(pixels[2], x);
scanline[index++] = (jpr_uchar_t)jas_matrix_getv(pixels[1], x);
scanline[index++] = (jpr_uchar_t)jas_matrix_getv(pixels[0], x);
scanline[index++] = 255;
x++;
}
}
//---------------------------------------------------
// Make gray scanline from *pixels[1]
//---------------------------------------------------
inline void
read_gray_to_rgb32(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 index = 0;
int32 x = 0;
jpr_uchar_t color = 0;
while (x < width)
{
color = (jpr_uchar_t)jas_matrix_getv(pixels[0], x++);
scanline[index++] = color;
scanline[index++] = color;
scanline[index++] = color;
scanline[index++] = 255;
}
}
//---------------------------------------------------
// Make RGBA32 scanline from *pixels[4]
// (just read data to scanline)
//---------------------------------------------------
inline void
read_rgba32(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 index = 0;
int32 x = 0;
while (x < width)
{
scanline[index++] = (jpr_uchar_t)jas_matrix_getv(pixels[2], x);
scanline[index++] = (jpr_uchar_t)jas_matrix_getv(pixels[1], x);
scanline[index++] = (jpr_uchar_t)jas_matrix_getv(pixels[0], x);
scanline[index++] = (jpr_uchar_t)jas_matrix_getv(pixels[3], x);
x++;
}
}
//---------------------------------------------------
// Make gray scanline from *pixels[1]
// (just read data to scanline)
//---------------------------------------------------
inline void
read_gray(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 x = 0;
while (x < width)
{
scanline[x] = (jpr_uchar_t)jas_matrix_getv(pixels[0], x);
x++;
}
}
//---------------------------------------------------
// Make *pixels[1] from gray1 scanline
//---------------------------------------------------
inline void
write_gray1_to_gray(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 x = 0;
int32 index = 0;
while (x < (width/8))
{
unsigned char c = scanline[x++];
for (int b = 128; b; b = b >> 1) {
if (c & b)
jas_matrix_setv(pixels[0], index++, 0);
else
jas_matrix_setv(pixels[0], index++, 255);
}
}
}
//---------------------------------------------------
// Make *pixels[3] from gray1 scanline
//---------------------------------------------------
inline void
write_gray1_to_rgb24(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 x = 0;
int32 index = 0;
while (x < (width/8))
{
unsigned char c = scanline[x++];
for (int b = 128; b; b = b >> 1) {
if (c & b) {
jas_matrix_setv(pixels[0], index, 0);
jas_matrix_setv(pixels[1], index, 0);
jas_matrix_setv(pixels[2], index, 0);
}
else {
jas_matrix_setv(pixels[0], index, 255);
jas_matrix_setv(pixels[1], index, 255);
jas_matrix_setv(pixels[2], index, 255);
}
index++;
}
}
}
//---------------------------------------------------
// Make *pixels[3] from cmap8 scanline
//---------------------------------------------------
inline void
write_cmap8_to_rgb24(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
const color_map *map = system_colors();
int32 x = 0;
while (x < width)
{
rgb_color color = map->color_list[scanline[x]];
jas_matrix_setv(pixels[0], x, color.red);
jas_matrix_setv(pixels[1], x, color.green);
jas_matrix_setv(pixels[2], x, color.blue);
x++;
}
}
//---------------------------------------------------
// Make *pixels[1] from gray scanline
// (just write data to pixels)
//---------------------------------------------------
inline void
write_gray(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 x = 0;
while (x < width)
{
jas_matrix_setv(pixels[0], x, scanline[x]);
x++;
}
}
//---------------------------------------------------
// Make *pixels[3] from RGB15/RGBA15 scanline
// (just write data to pixels)
//---------------------------------------------------
inline void
write_rgb15_to_rgb24(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 x = 0;
int32 index = 0;
int16 in_pixel;
while (x < width) {
in_pixel = scanline[index] | (scanline[index+1] << 8);
index += 2;
jas_matrix_setv(pixels[0], x, (char)(((in_pixel & 0x7c00)) >> 7) | (((in_pixel & 0x7c00)) >> 12));
jas_matrix_setv(pixels[1], x, (char)(((in_pixel & 0x3e0)) >> 2) | (((in_pixel & 0x3e0)) >> 7));
jas_matrix_setv(pixels[2], x, (char)(((in_pixel & 0x1f)) << 3) | (((in_pixel & 0x1f)) >> 2));
x++;
}
}
//---------------------------------------------------
// Make *pixels[3] from RGB15/RGBA15 bigendian scanline
// (just write data to pixels)
//---------------------------------------------------
inline void
write_rgb15b_to_rgb24(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 x = 0;
int32 index = 0;
int16 in_pixel;
while (x < width) {
in_pixel = scanline[index+1] | (scanline[index] << 8);
index += 2;
jas_matrix_setv(pixels[0], x, (char)(((in_pixel & 0x7c00)) >> 7) | (((in_pixel & 0x7c00)) >> 12));
jas_matrix_setv(pixels[1], x, (char)(((in_pixel & 0x3e0)) >> 2) | (((in_pixel & 0x3e0)) >> 7));
jas_matrix_setv(pixels[2], x, (char)(((in_pixel & 0x1f)) << 3) | (((in_pixel & 0x1f)) >> 2));
x++;
}
}
//---------------------------------------------------
// Make *pixels[3] from RGB16/RGBA16 scanline
// (just write data to pixels)
//---------------------------------------------------
inline void
write_rgb16_to_rgb24(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 x = 0;
int32 index = 0;
int16 in_pixel;
while (x < width) {
in_pixel = scanline[index] | (scanline[index+1] << 8);
index += 2;
jas_matrix_setv(pixels[0], x, (char)(((in_pixel & 0xf800)) >> 8) | (((in_pixel & 0x7c00)) >> 12));
jas_matrix_setv(pixels[1], x, (char)(((in_pixel & 0x7e0)) >> 3) | (((in_pixel & 0x7e0)) >> 9));
jas_matrix_setv(pixels[2], x, (char)(((in_pixel & 0x1f)) << 3) | (((in_pixel & 0x1f)) >> 2));
x++;
}
}
//---------------------------------------------------
// Make *pixels[3] from RGB16/RGBA16 bigendian scanline
// (just write data to pixels)
//---------------------------------------------------
inline void
write_rgb16b_to_rgb24(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 x = 0;
int32 index = 0;
int16 in_pixel;
while (x < width) {
in_pixel = scanline[index+1] | (scanline[index] << 8);
index += 2;
jas_matrix_setv(pixels[0], x, (char)(((in_pixel & 0xf800)) >> 8) | (((in_pixel & 0xf800)) >> 13));
jas_matrix_setv(pixels[1], x, (char)(((in_pixel & 0x7e0)) >> 3) | (((in_pixel & 0x7e0)) >> 9));
jas_matrix_setv(pixels[2], x, (char)(((in_pixel & 0x1f)) << 3) | (((in_pixel & 0x1f)) >> 2));
x++;
}
}
//---------------------------------------------------
// Make *pixels[3] from RGB24 scanline
// (just write data to pixels)
//---------------------------------------------------
inline void
write_rgb24(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 index = 0;
int32 x = 0;
while (x < width)
{
jas_matrix_setv(pixels[2], x, scanline[index++]);
jas_matrix_setv(pixels[1], x, scanline[index++]);
jas_matrix_setv(pixels[0], x, scanline[index++]);
x++;
}
}
//---------------------------------------------------
// Make *pixels[3] from RGB24 bigendian scanline
// (just write data to pixels)
//---------------------------------------------------
inline void
write_rgb24b(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 index = 0;
int32 x = 0;
while (x < width)
{
jas_matrix_setv(pixels[0], x, scanline[index++]);
jas_matrix_setv(pixels[1], x, scanline[index++]);
jas_matrix_setv(pixels[2], x, scanline[index++]);
x++;
}
}
//---------------------------------------------------
// Make *pixels[3] from RGB32 scanline
// (just write data to pixels)
//---------------------------------------------------
inline void
write_rgb32_to_rgb24(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 index = 0;
int32 x = 0;
while (x < width)
{
jas_matrix_setv(pixels[2], x, scanline[index++]);
jas_matrix_setv(pixels[1], x, scanline[index++]);
jas_matrix_setv(pixels[0], x, scanline[index++]);
index++;
x++;
}
}
//---------------------------------------------------
// Make *pixels[3] from RGB32 bigendian scanline
// (just write data to pixels)
//---------------------------------------------------
inline void
write_rgb32b_to_rgb24(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 index = 0;
int32 x = 0;
while (x < width)
{
index++;
jas_matrix_setv(pixels[0], x, scanline[index++]);
jas_matrix_setv(pixels[1], x, scanline[index++]);
jas_matrix_setv(pixels[2], x, scanline[index++]);
x++;
}
}
//---------------------------------------------------
// Make *pixels[4] from RGBA32 scanline
// (just write data to pixels)
// !!! UNTESTED !!!
//---------------------------------------------------
inline void
write_rgba32(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 index = 0;
int32 x = 0;
while (x < width)
{
jas_matrix_setv(pixels[3], x, scanline[index++]);
jas_matrix_setv(pixels[2], x, scanline[index++]);
jas_matrix_setv(pixels[1], x, scanline[index++]);
jas_matrix_setv(pixels[0], x, scanline[index++]);
x++;
}
}
//---------------------------------------------------
// Make *pixels[4] from RGBA32 bigendian scanline
// (just write data to pixels)
// !!! UNTESTED !!!
//---------------------------------------------------
inline void
write_rgba32b(jas_matrix_t **pixels, jpr_uchar_t *scanline, int width)
{
int32 index = 0;
int32 x = 0;
while (x < width)
{
jas_matrix_setv(pixels[0], x, scanline[index++]);
jas_matrix_setv(pixels[1], x, scanline[index++]);
jas_matrix_setv(pixels[2], x, scanline[index++]);
jas_matrix_setv(pixels[3], x, scanline[index++]);
x++;
}
}
#endif // _JP2TRANSLATOR_H_ #endif // _JP2TRANSLATOR_H_