Initial check in for PNGTranslator -- based on libpng library. It probably doesn't build properly with Jam yet, so I've left it out of the Jamfile in the parent directory. Currently, it can read some PNG images, but it falsly identifies everything that is not a Be Bitmap image as a PNG image.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4052 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Matthew Wilber 2003-07-22 02:56:56 +00:00
parent d50af9662a
commit 98fde4e893
8 changed files with 1280 additions and 0 deletions

View File

@ -0,0 +1,9 @@
SubDir OBOS_TOP src add-ons translators pngtranslator ;
Translator PNGTranslator :
PNGMain.cpp
PNGTranslator.cpp
PNGView.cpp
PNGWindow.cpp ;
LinkSharedOSLibs PNGTranslator : be translation png ;

View File

@ -0,0 +1,103 @@
/*****************************************************************************/
// PNGTranslator
// Written by Michael Wilber, OBOS Translation Kit Team
//
// Version:
//
// This translator opens and writes PNG images.
//
//
// This application and all source files used in its construction, except
// where noted, are licensed under the MIT License, and have been written
// and are:
//
// 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 <Application.h>
#include <Screen.h>
#include <Alert.h>
#include "PNGTranslator.h"
#include "PNGWindow.h"
#include "PNGView.h"
// ---------------------------------------------------------------
// main
//
// Creates a BWindow for displaying info about the PNGTranslator
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
int
main()
{
BApplication app("application/x-vnd.obos-tiff-translator");
PNGTranslator *ptranslator = new PNGTranslator;
BView *view = NULL;
BRect rect(0, 0, 225, 175);
if (ptranslator->MakeConfigurationView(NULL, &view, &rect)) {
BAlert *err = new BAlert("Error",
"Unable to create the PNGTranslator view.", "OK");
err->Go();
return 1;
}
// release the translator even though I never really used it anyway
ptranslator->Release();
ptranslator = NULL;
PNGWindow *wnd = new PNGWindow(rect);
view->ResizeTo(rect.Width(), rect.Height());
wnd->AddChild(view);
BPoint wndpt = B_ORIGIN;
{
BScreen scrn;
BRect frame = scrn.Frame();
frame.InsetBy(10, 23);
// if the point is outside of the screen frame,
// use the mouse location to find a better point
if (!frame.Contains(wndpt)) {
uint32 dummy;
view->GetMouse(&wndpt, &dummy, false);
wndpt.x -= rect.Width() / 2;
wndpt.y -= rect.Height() / 2;
// clamp location to screen
if (wndpt.x < frame.left)
wndpt.x = frame.left;
if (wndpt.y < frame.top)
wndpt.y = frame.top;
if (wndpt.x > frame.right)
wndpt.x = frame.right;
if (wndpt.y > frame.bottom)
wndpt.y = frame.bottom;
}
}
wnd->MoveTo(wndpt);
wnd->Show();
app.Run();
return 0;
}

View File

@ -0,0 +1,766 @@
/*****************************************************************************/
// PNGTranslator
// Written by Michael Wilber, OBOS Translation Kit Team
//
// PNGTranslator.cpp
//
// This BTranslator based object is for opening and writing
// PNG images.
//
//
// 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 <stdio.h>
#include <string.h>
#include <OS.h>
#include <png.h>
#include "PNGTranslator.h"
#include "PNGView.h"
// The input formats that this translator supports.
translation_format gInputFormats[] = {
{
B_PNG_FORMAT,
B_TRANSLATOR_BITMAP,
PNG_IN_QUALITY,
PNG_IN_CAPABILITY,
"image/png",
"PNG image"
},
{
B_PNG_FORMAT,
B_TRANSLATOR_BITMAP,
PNG_IN_QUALITY,
PNG_IN_CAPABILITY,
"image/x-png",
"PNG image"
},
{
B_TRANSLATOR_BITMAP,
B_TRANSLATOR_BITMAP,
BBT_IN_QUALITY,
BBT_IN_CAPABILITY,
"image/x-be-bitmap",
"Be Bitmap Format (PNGTranslator)"
}
};
// The output formats that this translator supports.
translation_format gOutputFormats[] = {
{
B_PNG_FORMAT,
B_TRANSLATOR_BITMAP,
PNG_OUT_QUALITY,
PNG_OUT_CAPABILITY,
"image/png",
"PNG image"
},
{
B_TRANSLATOR_BITMAP,
B_TRANSLATOR_BITMAP,
BBT_OUT_QUALITY,
BBT_OUT_CAPABILITY,
"image/x-be-bitmap",
"Be Bitmap Format (PNGTranslator)"
}
};
// ---------------------------------------------------------------
// make_nth_translator
//
// Creates a PNGTranslator object to be used by BTranslatorRoster
//
// Preconditions:
//
// Parameters: n, The translator to return. Since
// PNGTranslator only publishes one
// translator, it only returns a
// PNGTranslator if n == 0
//
// you, The image_id of the add-on that
// contains code (not used).
//
// flags, Has no meaning yet, should be 0.
//
// Postconditions:
//
// Returns: NULL if n is not zero,
// a new PNGTranslator if n is zero
// ---------------------------------------------------------------
BTranslator *
make_nth_translator(int32 n, image_id you, uint32 flags, ...)
{
if (!n)
return new PNGTranslator();
else
return NULL;
}
/* The png_jmpbuf() macro, used in error handling, became available in
* libpng version 1.0.6. If you want to be able to run your code with older
* versions of libpng, you must define the macro yourself (but only if it
* is not already defined by libpng!).
*/
#ifndef png_jmpbuf
# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
#endif
//// libpng Callback functions!
BPositionIO *
get_pio(png_structp ppng)
{
BPositionIO *pio = NULL;
pio = static_cast<BPositionIO *>(png_get_io_ptr(ppng));
if (!pio)
debugger("pio is NULL");
return pio;
}
void
pngcb_read_data(png_structp ppng, png_bytep pdata, png_size_t length)
{
BPositionIO *pio = get_pio(ppng);
pio->Read(pdata, static_cast<size_t>(length));
}
void
pngcb_write_data(png_structp ppng, png_bytep pdata, png_size_t length)
{
BPositionIO *pio = get_pio(ppng);
pio->Write(pdata, static_cast<size_t>(length));
}
void
pngcb_flush_data(png_structp ppng)
{
// I don't think I really need to do anything here
}
// ---------------------------------------------------------------
// Constructor
//
// Sets up the version info and the name of the translator so that
// these values can be returned when they are requested.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
PNGTranslator::PNGTranslator()
: BTranslator()
{
strcpy(fName, "PNG Images");
sprintf(fInfo, "PNG image translator v%d.%d.%d %s",
PNG_TRANSLATOR_VERSION / 100, (PNG_TRANSLATOR_VERSION / 10) % 10,
PNG_TRANSLATOR_VERSION % 10, __DATE__);
}
// ---------------------------------------------------------------
// Destructor
//
// Does nothing
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
PNGTranslator::~PNGTranslator()
{
}
// ---------------------------------------------------------------
// TranslatorName
//
// Returns the short name of the translator.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: a const char * to the short name of the translator
// ---------------------------------------------------------------
const char *
PNGTranslator::TranslatorName() const
{
return fName;
}
// ---------------------------------------------------------------
// TranslatorInfo
//
// Returns a more verbose name for the translator than the one
// TranslatorName() returns. This usually includes version info.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: a const char * to the verbose name of the translator
// ---------------------------------------------------------------
const char *
PNGTranslator::TranslatorInfo() const
{
return fInfo;
}
// ---------------------------------------------------------------
// TranslatorVersion
//
// Returns the integer representation of the current version of
// this translator.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
int32
PNGTranslator::TranslatorVersion() const
{
return PNG_TRANSLATOR_VERSION;
}
// ---------------------------------------------------------------
// InputFormats
//
// Returns a list of input formats supported by this translator.
//
// Preconditions:
//
// Parameters: out_count, The number of input formats
// support is returned here.
//
// Postconditions:
//
// Returns: the list of input formats and the number of input
// formats through the out_count parameter, if out_count is NULL,
// NULL is returned
// ---------------------------------------------------------------
const translation_format *
PNGTranslator::InputFormats(int32 *out_count) const
{
if (out_count) {
*out_count = sizeof(gInputFormats) /
sizeof(translation_format);
return gInputFormats;
} else
return NULL;
}
// ---------------------------------------------------------------
// OutputFormats
//
// Returns a list of output formats supported by this translator.
//
// Preconditions:
//
// Parameters: out_count, The number of output formats
// support is returned here.
//
// Postconditions:
//
// Returns: the list of output formats and the number of output
// formats through the out_count parameter, if out_count is NULL,
// NULL is returned
// ---------------------------------------------------------------
const translation_format *
PNGTranslator::OutputFormats(int32 *out_count) const
{
if (out_count) {
*out_count = sizeof(gOutputFormats) /
sizeof(translation_format);
return gOutputFormats;
} else
return NULL;
}
// ---------------------------------------------------------------
// identify_bits_header
//
// Determines if the data in inSource is in the
// B_TRANSLATOR_BITMAP ('bits') format. If it is, it returns
// info about the data in inSource to outInfo and pheader.
//
// Preconditions:
//
// Parameters: inSource, The source of the image data
//
// outInfo, Information about the translator
// is copied here
//
// amtread, Amount of data read from inSource
// before this function was called
//
// read, Pointer to the data that was read
// in before this function was called
//
// pheader, The bits header is copied here after
// it is read in from inSource
//
// Postconditions:
//
// Returns: B_NO_TRANSLATOR, if the data does not look like
// bits format data
//
// B_ERROR, if the header data could not be converted to host
// format
//
// B_OK, if the data looks like bits data and no errors were
// encountered
// ---------------------------------------------------------------
status_t
identify_bits_header(BPositionIO *inSource, translator_info *outInfo,
ssize_t amtread, uint8 *read, TranslatorBitmap *pheader = NULL)
{
TranslatorBitmap header;
memcpy(&header, read, amtread);
// copy portion of header already read in
// read in the rest of the header
ssize_t size = sizeof(TranslatorBitmap) - amtread;
if (inSource->Read(
(reinterpret_cast<uint8 *> (&header)) + amtread, size) != size)
return B_NO_TRANSLATOR;
// convert to host byte order
if (swap_data(B_UINT32_TYPE, &header, sizeof(TranslatorBitmap),
B_SWAP_BENDIAN_TO_HOST) != B_OK)
return B_ERROR;
// check if header values are reasonable
if (header.colors != B_RGB32 &&
header.colors != B_RGB32_BIG &&
header.colors != B_RGBA32 &&
header.colors != B_RGBA32_BIG &&
header.colors != B_RGB24 &&
header.colors != B_RGB24_BIG &&
header.colors != B_RGB16 &&
header.colors != B_RGB16_BIG &&
header.colors != B_RGB15 &&
header.colors != B_RGB15_BIG &&
header.colors != B_RGBA15 &&
header.colors != B_RGBA15_BIG &&
header.colors != B_CMAP8 &&
header.colors != B_GRAY8 &&
header.colors != B_GRAY1 &&
header.colors != B_CMYK32 &&
header.colors != B_CMY32 &&
header.colors != B_CMYA32 &&
header.colors != B_CMY24)
return B_NO_TRANSLATOR;
if (header.rowBytes * (header.bounds.Height() + 1) != header.dataSize)
return B_NO_TRANSLATOR;
if (outInfo) {
outInfo->type = B_TRANSLATOR_BITMAP;
outInfo->group = B_TRANSLATOR_BITMAP;
outInfo->quality = BBT_IN_QUALITY;
outInfo->capability = BBT_IN_CAPABILITY;
strcpy(outInfo->name, "Be Bitmap Format (PNGTranslator)");
strcpy(outInfo->MIME, "image/x-be-bitmap");
}
if (pheader) {
pheader->magic = header.magic;
pheader->bounds = header.bounds;
pheader->rowBytes = header.rowBytes;
pheader->colors = header.colors;
pheader->dataSize = header.dataSize;
}
return B_OK;
}
status_t
identify_png_header(BPositionIO *inSource, BMessage *ioExtension,
translator_info *outInfo, uint32 outType)
{
// Can only output to bits for now
if (outType != B_TRANSLATOR_BITMAP)
return B_NO_TRANSLATOR;
if (outInfo) {
outInfo->type = B_PNG_FORMAT;
outInfo->group = B_TRANSLATOR_BITMAP;
outInfo->quality = PNG_IN_QUALITY;
outInfo->capability = PNG_IN_CAPABILITY;
strcpy(outInfo->MIME, "image/png");
strcpy(outInfo->name, "PNG image");
}
// LAZY HACK: everything that is not bits is identified as PNG
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 (not used)
//
// 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 understood the data and there were
// no errors found
//
// Other errors if BPositionIO::Read() returned an error value
// ---------------------------------------------------------------
status_t
PNGTranslator::Identify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType)
{
if (!outType)
outType = B_TRANSLATOR_BITMAP;
if (outType != B_TRANSLATOR_BITMAP && outType != B_PNG_FORMAT)
return B_NO_TRANSLATOR;
// Convert the magic numbers to the various byte orders so that
// I won't have to convert the data read in to see whether or not
// it is a supported type
uint32 nbits = B_TRANSLATOR_BITMAP;
if (swap_data(B_UINT32_TYPE, &nbits, sizeof(uint32),
B_SWAP_HOST_TO_BENDIAN) != B_OK)
return B_ERROR;
// Read in the magic number and determine if it
// is a supported type
uint8 ch[4];
if (inSource->Read(ch, 4) != 4)
return B_NO_TRANSLATOR;
// Read settings from ioExtension
bool bheaderonly = false, bdataonly = false;
if (ioExtension) {
if (ioExtension->FindBool(B_TRANSLATOR_EXT_HEADER_ONLY, &bheaderonly))
// if failed, make sure bool is default value
bheaderonly = false;
if (ioExtension->FindBool(B_TRANSLATOR_EXT_DATA_ONLY, &bdataonly))
// if failed, make sure bool is default value
bdataonly = false;
if (bheaderonly && bdataonly)
// can't both "only write the header" and "only write the data"
// at the same time
return B_BAD_VALUE;
}
uint32 n32ch;
memcpy(&n32ch, ch, sizeof(uint32));
// if B_TRANSLATOR_BITMAP type
if (n32ch == nbits)
return identify_bits_header(inSource, outInfo, 4, ch);
// Might be PNG image
else {
return identify_png_header(inSource, ioExtension, outInfo, outType);
}
}
status_t
translate_from_png(BPositionIO *inSource, BMessage *ioExtension,
uint32 outType, BPositionIO *outDestination, bool bheaderonly,
bool bdataonly)
{
status_t result = B_NO_TRANSLATOR;
inSource->Seek(0, SEEK_SET);
png_structp ppng = NULL;
png_infop pinfo = NULL;
while (1) {
// create PNG read pointer with default error handling routines
ppng = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!ppng) {
result = B_ERROR;
break;
}
// alocate / init memory for image information
pinfo = png_create_info_struct(ppng);
if (!pinfo) {
result = B_ERROR;
break;
}
// set error handling
if (setjmp(png_jmpbuf(ppng))) {
result = B_ERROR;
break;
}
// set read callback function
png_set_read_fn(ppng, static_cast<void *>(inSource), pngcb_read_data);
// Read in the whole PNG all at once! (easy way?)
png_read_png(ppng, pinfo,
PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_BGR,
png_voidp_NULL);
png_uint_32 width, height;
int bit_depth, color_type, interlace_type;
png_get_IHDR(ppng, pinfo, &width, &height, &bit_depth, &color_type,
&interlace_type, int_p_NULL, int_p_NULL);
if ((color_type != PNG_COLOR_TYPE_RGB || bit_depth != 8) &&
(color_type != PNG_COLOR_TYPE_RGB_ALPHA || bit_depth != 8)) {
debugger("Unsupported colorspace!");
result = B_NO_TRANSLATOR;
break;
}
// Write out the data to outDestination
// Construct and write Be bitmap header
TranslatorBitmap bitsHeader;
bitsHeader.magic = B_TRANSLATOR_BITMAP;
bitsHeader.bounds.left = 0;
bitsHeader.bounds.top = 0;
bitsHeader.bounds.right = width - 1;
bitsHeader.bounds.bottom = height - 1;
bitsHeader.rowBytes = 4 * width;
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
bitsHeader.colors = B_RGBA32;
else
bitsHeader.colors = B_RGB32;
bitsHeader.dataSize = bitsHeader.rowBytes * height;
if (swap_data(B_UINT32_TYPE, &bitsHeader,
sizeof(TranslatorBitmap), B_SWAP_HOST_TO_BENDIAN) != B_OK) {
result = B_ERROR;
break;
}
outDestination->Write(&bitsHeader, sizeof(TranslatorBitmap));
// write out bitmap data, one row at a time
png_bytep *prows = png_get_rows(ppng, pinfo);
if (!prows) {
result = B_NO_MEMORY;
break;
}
int ncopy;
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
ncopy = 4;
else
ncopy = 3;
for (png_uint_32 i = 0; i < height; i++) {
for (png_uint_32 k = 0; k < width; k++) {
uint8 pixel[4] = { 0xff };
memcpy(pixel, prows[i] + (k * ncopy), ncopy);
outDestination->Write(pixel, 4);
}
}
result = B_OK;
break;
}
if (ppng) {
if (!pinfo)
png_destroy_read_struct(&ppng, png_infopp_NULL, png_infopp_NULL);
else
png_destroy_read_struct(&ppng, &pinfo, png_infopp_NULL);
}
return result;
}
// ---------------------------------------------------------------
// 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
PNGTranslator::Translate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension,
uint32 outType, BPositionIO *outDestination)
{
if (!outType)
outType = B_TRANSLATOR_BITMAP;
if (outType != B_TRANSLATOR_BITMAP && outType != B_PNG_FORMAT)
return B_NO_TRANSLATOR;
// Convert the magic numbers to the various byte orders so that
// I won't have to convert the data read in to see whether or not
// it is a supported type
uint32 nbits = B_TRANSLATOR_BITMAP;
if (swap_data(B_UINT32_TYPE, &nbits, sizeof(uint32),
B_SWAP_HOST_TO_BENDIAN) != B_OK)
return B_ERROR;
// Read in the magic number and determine if it
// is a supported type
uint8 ch[4];
inSource->Seek(0, SEEK_SET);
if (inSource->Read(ch, 4) != 4)
return B_NO_TRANSLATOR;
// Read settings from ioExtension
bool bheaderonly = false, bdataonly = false;
if (ioExtension) {
if (ioExtension->FindBool(B_TRANSLATOR_EXT_HEADER_ONLY, &bheaderonly))
// if failed, make sure bool is default value
bheaderonly = false;
if (ioExtension->FindBool(B_TRANSLATOR_EXT_DATA_ONLY, &bdataonly))
// if failed, make sure bool is default value
bdataonly = false;
if (bheaderonly && bdataonly)
// can't both "only write the header" and "only write the data"
// at the same time
return B_BAD_VALUE;
}
uint32 n32ch;
memcpy(&n32ch, ch, sizeof(uint32));
if (n32ch == nbits) {
// B_TRANSLATOR_BITMAP type
inSource->Seek(0, SEEK_SET);
const size_t kbufsize = 2048;
uint8 buffer[kbufsize];
ssize_t ret = inSource->Read(buffer, kbufsize);
while (ret > 0) {
outDestination->Write(buffer, ret);
ret = inSource->Read(buffer, kbufsize);
}
return B_OK;
} else
// Might be PNG image
return translate_from_png(inSource, ioExtension, outType,
outDestination, bheaderonly, bdataonly);
}
// ---------------------------------------------------------------
// MakeConfigurationView
//
// Makes a BView object for configuring / displaying info about
// this translator.
//
// Preconditions:
//
// Parameters: ioExtension, configuration options for the
// translator
//
// outView, the view to configure the
// translator is stored here
//
// outExtent, the bounds of the view are
// stored here
//
// Postconditions:
//
// Returns: B_BAD_VALUE if outView or outExtent is NULL,
// B_NO_MEMORY if the view couldn't be allocated,
// B_OK if no errors
// ---------------------------------------------------------------
status_t
PNGTranslator::MakeConfigurationView(BMessage *ioExtension, BView **outView,
BRect *outExtent)
{
if (!outView || !outExtent)
return B_BAD_VALUE;
PNGView *view = new PNGView(BRect(0, 0, 225, 175),
"PNGTranslator Settings", B_FOLLOW_ALL, B_WILL_DRAW);
if (!view)
return B_NO_MEMORY;
*outView = view;
*outExtent = view->Bounds();
return B_OK;
}

View File

@ -0,0 +1,112 @@
/*****************************************************************************/
// PNGTranslator
// Written by Michael Wilber, OBOS Translation Kit Team
//
// PNGTranslator.h
//
// This BTranslator based object is for opening and writing
// PNG images.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#ifndef PNG_TRANSLATOR_H
#define PNG_TRANSLATOR_H
#include <Translator.h>
#include <TranslatorFormats.h>
#include <TranslationDefs.h>
#include <GraphicsDefs.h>
#include <InterfaceDefs.h>
#include <DataIO.h>
#include <File.h>
#include <ByteOrder.h>
#include <fs_attr.h>
// IO Extension Names:
#define PNG_TRANSLATOR_VERSION 100
#define PNG_IN_QUALITY 0.6
#define PNG_IN_CAPABILITY 0.2
#define PNG_OUT_QUALITY 0.6
#define PNG_OUT_CAPABILITY 0.2
#define BBT_IN_QUALITY 0.5
#define BBT_IN_CAPABILITY 0.2
#define BBT_OUT_QUALITY 0.5
#define BBT_OUT_CAPABILITY 0.2
class PNGTranslator : public BTranslator {
public:
PNGTranslator();
virtual const char *TranslatorName() const;
// returns the short name of the translator
virtual const char *TranslatorInfo() const;
// returns a verbose name/description for the translator
virtual int32 TranslatorVersion() const;
// returns the version of the translator
virtual const translation_format *InputFormats(int32 *out_count)
const;
// returns the input formats and the count of input formats
// that this translator supports
virtual const translation_format *OutputFormats(int32 *out_count)
const;
// returns the output formats and the count of output formats
// that this translator supports
virtual status_t Identify(BPositionIO *inSource,
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,
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
virtual status_t MakeConfigurationView(BMessage *ioExtension,
BView **outView, BRect *outExtent);
// creates and returns the view for displaying information
// about this translator
protected:
virtual ~PNGTranslator();
// this is protected because the object is deleted by the
// Release() function instead of being deleted directly by
// the user
private:
char fName[30];
char fInfo[100];
};
#endif // #ifndef PNG_TRANSLATOR_H

View File

@ -0,0 +1,122 @@
/*****************************************************************************/
// PNGView
// Written by Michael Wilber, OBOS Translation Kit Team
//
// PNGView.cpp
//
// This BView based object displays information about the PNGTranslator.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include "PNGView.h"
#include "PNGTranslator.h"
// ---------------------------------------------------------------
// Constructor
//
// Sets up the view settings
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
PNGView::PNGView(const BRect &frame, const char *name,
uint32 resize, uint32 flags)
: BView(frame, name, resize, flags)
{
SetViewColor(220,220,220,0);
}
// ---------------------------------------------------------------
// Destructor
//
// Does nothing
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
PNGView::~PNGView()
{
}
// ---------------------------------------------------------------
// Draw
//
// Draws information about the PNGTranslator to this view.
//
// Preconditions:
//
// Parameters: area, not used
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
void
PNGView::Draw(BRect area)
{
SetFont(be_bold_font);
font_height fh;
GetFontHeight(&fh);
float xbold, ybold;
xbold = fh.descent + 1;
ybold = fh.ascent + fh.descent * 2 + fh.leading;
char title[] = "OpenBeOS PNG Image Translator";
DrawString(title, BPoint(xbold, ybold));
SetFont(be_plain_font);
font_height plainh;
GetFontHeight(&plainh);
float yplain;
yplain = plainh.ascent + plainh.descent * 2 + plainh.leading;
char detail[100];
sprintf(detail, "Version %d.%d.%d %s",
PNG_TRANSLATOR_VERSION / 100, (PNG_TRANSLATOR_VERSION / 10) % 10,
PNG_TRANSLATOR_VERSION % 10, __DATE__);
DrawString(detail, BPoint(xbold, yplain + ybold));
int32 lineno = 4;
DrawString("PNG Library:", BPoint(xbold, yplain * lineno + ybold));
lineno += 2;
char libtiff[] = "BEER!";
char *tok = strtok(libtiff, "\n");
while (tok) {
DrawString(tok, BPoint(xbold, yplain * lineno + ybold));
lineno++;
tok = strtok(NULL, "\n");
}
}

View File

@ -0,0 +1,51 @@
/*****************************************************************************/
// PNGView
// Written by Michael Wilber, OBOS Translation Kit Team
//
// PNGView.h
//
// This BView based object displays information about the PNGTranslator.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#ifndef PNGVIEW_H
#define PNGVIEW_H
#include <View.h>
#include <MenuField.h>
#include <MenuItem.h>
class PNGView : public BView {
public:
PNGView(const BRect &frame, const char *name, uint32 resize,
uint32 flags);
// sets up the view
~PNGView();
// does nothing
virtual void Draw(BRect area);
// draws information about the PNGTranslator
};
#endif // #ifndef PNGVIEW_H

View File

@ -0,0 +1,69 @@
/*****************************************************************************/
// PNGWindow
// Written by Michael Wilber, OBOS Translation Kit Team
//
// PNGWindow.cpp
//
// This BWindow based object is used to hold the PNGView object when the
// user runs the PNGTranslator as an application.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#include "PNGWindow.h"
// ---------------------------------------------------------------
// Constructor
//
// Sets up the BWindow for holding a PNGView
//
// Preconditions:
//
// Parameters: area, The bounds of the window
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
PNGWindow::PNGWindow(BRect area)
: BWindow(area, "PNGTranslator", B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
{
}
// ---------------------------------------------------------------
// Destructor
//
// Posts a quit message so that the application is close properly
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
PNGWindow::~PNGWindow()
{
be_app->PostMessage(B_QUIT_REQUESTED);
}

View File

@ -0,0 +1,48 @@
/*****************************************************************************/
// PNGWindow
// Written by Michael Wilber, OBOS Translation Kit Team
//
// PNGWindow.h
//
// This BWindow based object is used to hold the PNGView object when the
// user runs the PNGTranslator as an application.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#ifndef PNGWINDOW_H
#define PNGWINDOW_H
#include <Application.h>
#include <Window.h>
#include <View.h>
class PNGWindow : public BWindow {
public:
PNGWindow(BRect area);
// Sets up a BWindow with bounds area
~PNGWindow();
// Posts a quit message so that the application closes properly
};
#endif // #define PNGWINDOW_H