// -*- c++ -*-

/*
 *  Copyright (C) 2003, Richard J. Moore <rich@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 */

#include <qfile.h>
#include <qimage.h>
#include <kdebug.h>

#include "image_imp.h"
#include "image_imp.moc"

namespace KJSEmbed {
namespace Bindings {

Image::Image( QObject *parent, const char *name )
    : QObject(parent, name), img(0), ok(false)
{
}

Image::Image( QObject *parent, const char *name, QImage *i )
    : QObject(parent, name), img(i), ok(true)
{
}

Image::~Image()
{
    delete img;
}

int Image::width() const
{
    return img ? img->width() : 0;
}

int Image::height() const
{
    return img ? img->height() : 0;
}

int Image::depth() const
{
    return img ? img->depth() : 0;
}

bool Image::isOk() const
{
    return img && ok;
}

QString Image::format() const
{
    return fmt;
}

QString Image::filename() const
{
    return nm;
}

void Image::setFormat( const QString &f )
{
    fmt = f;
}

bool Image::load( const QString &filename )
{
    if ( !img )
	img = new QImage;

    nm = filename;
    fmt = QImageIO::imageFormat( filename );
    ok = img->load( filename );
    if ( !ok ) {
	delete img;
	img = 0;
    }
    return ok;
}

bool Image::save( const QString &filename )
{
    if ( !img )
	return false;

    ok = img->save( filename, fmt.latin1() );
    if ( ok )
	nm = filename;

    return ok;
}

void Image::smoothScale( int w, int h )
{
    if ( !img )
	return;
    (*img) = img->smoothScale( w, h );
}

void Image::smoothScaleMin( int w, int h )
{
    if ( !img )
	return;
    (*img) = img->smoothScale( w, h, QImage::ScaleMin );
}

}; // namespace KJSEmbed::Bindings
}; // namespace KJSEmbed

