📄 xaostranslator.cpp
字号:
/* Module for XaoS image translator. */#include <TranslatorAddOn.h>#include <TranslationKit.h>#include <ByteOrder.h>#include <Message.h>#include <Screen.h>#include <Locker.h>#include <FindDirectory.h>#include <Path.h>#include <PopUpMenu.h>#include <MenuField.h>#include <MenuItem.h>#include <CheckBox.h>#include <Bitmap.h>#include <TextControl.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <fconfig.h>#include <ctype.h>#include <setjmp.h>#include "be_checkfile.h"#include "be_possitionio.h"#include "filter.h"#include "xerror.h"#include "ui_helper.h"#include "xthread.h"//#define CHANGE_ANTIALIAS 'Xaan'/*#include "colorspace.h"*/#define INCH 2.54jmp_buf translatorjmp;char translatorName[] = "XaoS Translator";char translatorInfo[] = "Reads the XaoS Position Files and render them to the image. http://www.paru.cas.cz/~hubicka/XaoS/";int32 translatorVersion = 301; /* format is revision+minor*10+major*100 */int translator;#define XPF_TYPE 'xpf '#define XAF_TYPE 'xaf 'translation_format inputFormats[] = { { XPF_TYPE, B_TRANSLATOR_BITMAP, 0.3, 1.0, "image/x-xaos-position", "XaoS Position File" }, { XAF_TYPE, B_TRANSLATOR_BITMAP, 0.01, 1.0, "image/x-xaos-animation", "XaoS Animation File" }, { 0, 0, 0, 0, "\0", "\0" }};translation_format outputFormats[] = { { B_TRANSLATOR_BITMAP, B_TRANSLATOR_BITMAP, 0.4, 0.5, "image/x-be-bitmap", "Be Bitmap Format (XaoSHandler)" }, { 0, 0, 0, 0, "\0", "\0" }};struct XaoS_settings { color_space out_space; double pixelwidth, pixelheight; int width, height; bool antialiasing; bool settings_touched;};BLocker g_settings_lock("XaoS settings lock");extern BLocker g_calculation_lock;XaoS_settings g_settings;class PrefsLoader {public: PrefsLoader( const char * str) { g_settings_lock.Lock(); /* defaults */ g_settings.out_space = B_NO_COLOR_SPACE; g_settings.width = XSIZE; g_settings.height = YSIZE; g_settings.antialiasing = 0; g_settings.pixelwidth = 0.02979; g_settings.pixelheight = 0.02979; g_settings.settings_touched = false; BPath path; if (find_directory(B_USER_SETTINGS_DIRECTORY, &path)) { path.SetTo("/tmp"); } path.Append(str); FILE * f = fopen(path.Path(), "r"); if (f) { char line[1024]; char name[32]; char * ptr; while (true) { line[0] = 0; fgets(line, 1024, f); if (feof(f)) break; ptr = line; while (isspace(*ptr)) { ptr++; } if (!*ptr) continue; if (*ptr == '#' || !*ptr) { /* comment or blank */ continue; } if (sscanf(ptr, "%31[a-zA-Z_0-9] =", name) != 1) { fprintf(stderr, "unknown XaoSTranslator settings line: %s", line); } else { if (!strcmp(name, "color_space")) { while (*ptr != '=') { ptr++; } ptr++; if (sscanf(ptr, "%d", (int*)&g_settings.out_space) != 1) { fprintf(stderr, "illegal color space in XaoSTranslator settings: %s", ptr); } } else if (!strcmp(name, "antialiasing")) { while (*ptr != '=') { ptr++; } ptr++; int antialiasing = g_settings.antialiasing; if (sscanf(ptr, "%d", &antialiasing) != 1) { fprintf(stderr, "illegal antialiasing value in XaoSTranslator settings: %s", ptr); } else { g_settings.antialiasing = antialiasing; } } else if (!strcmp(name, "size")) { while (*ptr != '=') { ptr++; } ptr++; int width = g_settings.width; int height = g_settings.height; if (sscanf(ptr, "%d,%d", &width, &height) != 2 || width<1 || width>4096 || height<1 || height>4096) { fprintf(stderr, "illegal size value in XaoSTranslator settings: %s", ptr); } else { g_settings.width = width; g_settings.height = height; } } else if (!strcmp(name, "pixelsize")) { while (*ptr != '=') { ptr++; } ptr++; double pixelwidth = g_settings.pixelwidth; double pixelheight = g_settings.pixelheight; if (sscanf(ptr, "%lg,%lg", &pixelwidth, &pixelheight) != 2 || pixelwidth<=0 || pixelheight<=0) { fprintf(stderr, "illegal size value in XaoSTranslator settings: %s", ptr); } else { g_settings.pixelwidth = pixelwidth; g_settings.pixelheight = pixelheight; } } else { fprintf(stderr, "unknown XaoSTranslator setting: %s", line); } } } fclose(f); } g_settings_lock.Unlock(); } ~PrefsLoader() { /* No need writing settings if there aren't any */ if (g_settings.settings_touched) { BPath path; if (find_directory(B_USER_SETTINGS_DIRECTORY, &path)) { path.SetTo("/tmp"); } path.Append("XaoSTranslator_Settings"); FILE * f = fopen(path.Path(), "w"); if (f) { fprintf(f, "# XaoSTranslator settings version %.2f\n", (float)translatorVersion/100.0); fprintf(f, "color_space = %d\n", g_settings.out_space); fprintf(f, "antialiasing = %d\n", g_settings.antialiasing ? 1 : 0); fprintf(f, "size = %d,%d\n", g_settings.width, g_settings.height); fprintf(f, "pixelsize = %g,%g\n", g_settings.pixelwidth, g_settings.pixelheight); fclose(f); } } }};PrefsLoader g_prefs_loader("XaoSTranslator_Settings");static color_space optimaloutspace(color_space out_space){ switch (out_space) { /*The 24-bit versions are not compiled in, because BeOS don't support them in most cases. Use 32 instead. */ case B_RGB24: case B_RGB24_BIG: out_space = B_RGB32; case B_RGB32: break; case B_RGB32_BIG: break; case B_RGBA32: out_space = B_RGB32; break; case B_RGBA32_BIG: out_space = B_RGB32_BIG; break; case B_RGB16: case B_RGB16_BIG: if (B_HOST_IS_LENDIAN) out_space = B_RGB16; else out_space = B_RGB16_BIG; break; case B_RGBA15: case B_RGBA15_BIG: case B_RGB15: case B_RGB15_BIG: if (B_HOST_IS_LENDIAN) out_space = B_RGB15; else out_space = B_RGB15_BIG; break; case B_CMAP8: out_space = B_RGB32; break; /*Support for dithering is fast but don't looks perfectly. It is better to keep it up to client app */ case B_GRAY8: break; case B_GRAY1: break; break; default: /* use best supported colorspace and let client app to convert it*/ out_space=B_RGB32; break; } return out_space;}extern void be_get_imagespecs(int cs, int *imagetype, int *rmask, int *gmask, int *bmask);/* This function is expected to identify input source and say whether it is correct or not. XaoS files don't have any particular headers so this test is done by looking for opening bracket (so we refuse empty files) and if that success interpreting first frame. In case first few commands are XaoS commands, it is very high probability that the rest is correct too. So mistakes are extremly inprobable I believe Hope this behaviour is what user want. */status_tIdentify( /* required */ BPositionIO * inSource, const translation_format * inFormat, /* can beNULL */ BMessage * ioExtension, /* can be NULL */ translator_info * outInfo, uint32 outType){ /* Silence compiler warnings. */ inFormat = inFormat; ioExtension = ioExtension; int r; /* Check that requested format is something we can deal with. */ if (outType == 0) { outType = B_TRANSLATOR_BITMAP; } if (outType != B_TRANSLATOR_BITMAP) { return B_NO_TRANSLATOR; } outInfo->group = B_TRANSLATOR_BITMAP; outInfo->type = B_TRANSLATOR_BITMAP; outInfo->quality = 0.4; outInfo->capability = 0.8; strcpy(outInfo->name, "Be Bitmap Format (XaoSHandler)"); strcpy(outInfo->MIME, "image/x-be-bitmap"); g_settings_lock.Lock(); if(setjmp(translatorjmp)) { /* OOPS something went realy badly. Try to return to app. hopefully it will work. */ xth_uninit(); g_calculation_lock.Unlock(); return 0; } r=XaoSCheckFile(inSource); g_settings_lock.Unlock(); return (r?B_OK:B_NO_TRANSLATOR);}status_tTranslate( BPositionIO * inSource, const translator_info * inInfo, BMessage * ioExtension, uint32 outType, BPositionIO * outDestination){ status_t err=B_OK; bool headeronly=false, dataonly=false; inSource->Seek(0, SEEK_SET); inInfo = inInfo; if (!outType) { outType = B_TRANSLATOR_BITMAP; } if (outType != B_TRANSLATOR_BITMAP && outType != XPF_TYPE && outType != XAF_TYPE) { return B_NO_TRANSLATOR; } color_space out_space, real_out_space; int out_rowbytes; g_settings_lock.Lock(); int width,height; bool anti; double pixelwidth, pixelheight; if (!ioExtension || ioExtension->FindInt32("xpf /width", (int32*)&width)) width=g_settings.width; if (!ioExtension || ioExtension->FindInt32("xpf /height", (int32*)&height)) height=g_settings.height; if (!ioExtension || ioExtension->FindDouble("xpf /pixelwidth", &pixelwidth)) pixelwidth=g_settings.pixelwidth; if (!ioExtension || ioExtension->FindDouble("xpf /pixelheight", &pixelheight)) pixelheight=g_settings.pixelheight; if (!ioExtension || ioExtension->FindBool("xpf /antialiasing", &anti)) anti=g_settings.antialiasing; if (!ioExtension || ioExtension->FindInt32(B_TRANSLATOR_EXT_BITMAP_COLOR_SPACE, (int32*)&out_space)) { out_space = g_settings.out_space; if (out_space == B_NO_COLOR_SPACE) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -