<commit_before>//
// This file is part of the Marble Project.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2006-2007 <NAME> <<EMAIL>>"
// Copyright 2007      <NAME>  <<EMAIL>>"
//

#include "TileCreator.h"

#include <cmath>

#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QRect>
#include <QtCore/QSize>
#include <QtCore/QVector>
#include <QtGui/QApplication>
#include <QtGui/QImage>
#include <QtGui/QImageReader>
#include <QtGui/QPainter>

#include "global.h"
#include "MarbleDirs.h"
#include "TileLoaderHelper.h"

// FIXME: This shouldn't be defined here, but centrally somewhere
const uint  tileSize = 675;


TileCreator::TileCreator(const QString& sourceDir, const QString& installmap,
                         const QString& dem, const QString& targetDir) 
    : QThread(0),
      m_sourceDir(sourceDir),
      m_installmap(installmap),
      m_dem(dem),
      m_targetDir(targetDir),
      m_cancelled( false )
{
    setTerminationEnabled( true );
}

void TileCreator::cancelTileCreation()
{
    m_cancelled = true;
}

void TileCreator::run()
{
    qDebug() << "Prefix: " << m_sourceDir << "installmap:" << m_installmap;

    // If the sourceDir starts with a '/' assume an absolute path.
    // Otherwise assume a relative marble data path
    QString m_sourcePath;
    if ( m_sourceDir.startsWith('/') ) {
        m_sourcePath = m_sourceDir + '/' + m_installmap;
        qDebug() << "Trying absolulte path:" << m_sourcePath;
    }
    else {
        m_sourcePath = MarbleDirs::path( "maps/" + m_sourceDir +
    '/' + m_installmap );
        qDebug() << "Trying relative path:" << "maps/" + m_sourceDir + '/' + m_installmap;
    }
    if ( m_targetDir.isNull() )
        m_targetDir = MarbleDirs::localPath() + "/maps/" + m_sourcePath.section( '/', -3, -2 ) + '/';
    if ( !m_targetDir.endsWith('/') )
        m_targetDir += '/';

    qDebug() << "Creating tiles from: " << m_sourcePath;
    qDebug() << "Installing tiles to: " << m_targetDir;
    QImageReader testImage( m_sourcePath );

    QVector<QRgb> grayScalePalette;
    for ( int cnt = 0; cnt <= 255; ++cnt ) {
        grayScalePalette.insert(cnt, qRgb(cnt, cnt, cnt));
    }

    uint  imageWidth  = testImage.size().width();
    uint  imageHeight = testImage.size().height();

    qDebug() << QString( "TileCreator::createTiles() image dimensions %1 x %2").arg(imageWidth).arg(imageHeight);

    if ( imageWidth > 21600 || imageHeight > 10800 ) {
        qDebug("Install map too large!");
        return;
    } 

    // Calculating Maximum Tile Level
    float approxMaxTileLevel = std::log( imageWidth / ( 2.0 * tileSize ) ) / std::log( 2.0 );

    int  maxTileLevel = 0;
    if ( approxMaxTileLevel == int( approxMaxTileLevel ) )
        maxTileLevel = static_cast<int>( approxMaxTileLevel );
    else
        maxTileLevel = static_cast<int>( approxMaxTileLevel + 1 );

    if ( maxTileLevel < 0 ) {
        qDebug() 
        << QString( "TileCreator::createTiles(): Invalid Maximum Tile Level: %1" )
        .arg( maxTileLevel );
    }
    qDebug() << "Maximum Tile Level: " << maxTileLevel;

    uint maxRows = TileLoaderHelper::levelToRow( maxTileLevel );

    // If the image size of the image source does not match the expected 
    // geometry we need to smooth-scale the image in advance to match
    // the required size 
    bool needsScaling = ( imageWidth != 2 * maxRows * tileSize
                          ||  imageHeight != maxRows * tileSize );<commit_msg>QImage::width() and QImage::height() return a signed integer - respect this and return when an invalid image (empty) is loaded<commit_after>//
// This file is part of the Marble Project.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2006-2007 <NAME> <<EMAIL>>"
// Copyright 2007      <NAME>  <<EMAIL>>"
//

#include "TileCreator.h"

#include <cmath>

#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QRect>
#include <QtCore/QSize>
#include <QtCore/QVector>
#include <QtGui/QApplication>
#include <QtGui/QImage>
#include <QtGui/QImageReader>
#include <QtGui/QPainter>

#include "global.h"
#include "MarbleDirs.h"
#include "TileLoaderHelper.h"

// FIXME: This shouldn't be defined here, but centrally somewhere
const uint  tileSize = 675;


TileCreator::TileCreator(const QString& sourceDir, const QString& installmap,
                         const QString& dem, const QString& targetDir) 
    : QThread(0),
      m_sourceDir(sourceDir),
      m_installmap(installmap),
      m_dem(dem),
      m_targetDir(targetDir),
      m_cancelled( false )
{
    setTerminationEnabled( true );
}

void TileCreator::cancelTileCreation()
{
    m_cancelled = true;
}

void TileCreator::run()
{
    qDebug() << "Prefix: " << m_sourceDir << "installmap:" << m_installmap;

    // If the sourceDir starts with a '/' assume an absolute path.
    // Otherwise assume a relative marble data path
    QString m_sourcePath;
    if ( m_sourceDir.startsWith('/') ) {
        m_sourcePath = m_sourceDir + '/' + m_installmap;
        qDebug() << "Trying absolulte path:" << m_sourcePath;
    }
    else {
        m_sourcePath = MarbleDirs::path( "maps/" + m_sourceDir +
    '/' + m_installmap );
        qDebug() << "Trying relative path:" << "maps/" + m_sourceDir + '/' + m_installmap;
    }
    if ( m_targetDir.isNull() )
        m_targetDir = MarbleDirs::localPath() + "/maps/" + m_sourcePath.section( '/', -3, -2 ) + '/';
    if ( !m_targetDir.endsWith('/') )
        m_targetDir += '/';

    qDebug() << "Creating tiles from: " << m_sourcePath;
    qDebug() << "Installing tiles to: " << m_targetDir;
    QImageReader testImage( m_sourcePath );

    QVector<QRgb> grayScalePalette;
    for ( int cnt = 0; cnt <= 255; ++cnt ) {
        grayScalePalette.insert(cnt, qRgb(cnt, cnt, cnt));
    }

    int  imageWidth  = testImage.size().width();
    int  imageHeight = testImage.size().height();

    qDebug() << QString( "TileCreator::createTiles() image dimensions %1 x %2").arg(imageWidth).arg(imageHeight);

    if ( imageWidth < 1 || imageHeight < 1 ) {
        qDebug("Invalid imagemap!");
        return;
    }
    if ( imageWidth > 21600 || imageHeight > 10800 ) {
        qDebug("Install map too large!");
        return;
    }

    // Calculating Maximum Tile Level
    float approxMaxTileLevel = std::log( imageWidth / ( 2.0 * tileSize ) ) / std::log( 2.0 );

    int  maxTileLevel = 0;
    if ( approxMaxTileLevel == int( approxMaxTileLevel ) )
        maxTileLevel = static_cast<int>( approxMaxTileLevel );
    else
        maxTileLevel = static_cast<int>( approxMaxTileLevel + 1 );

    if ( maxTileLevel < 0 ) {
        qDebug() 
        << QString( "TileCreator::createTiles(): Invalid Maximum Tile Level: %1" )
        .arg( maxTileLevel );
    }
    qDebug() << "Maximum Tile Level: " << maxTileLevel;

    uint maxRows = TileLoaderHelper::levelToRow( maxTileLevel );

    // If the image size of the image source does not match the expected 
    // geometry we need to smooth-scale the image in advance to match
    // the required size 
    bool needsScaling = ( imageWidth != 2 * maxRows * tileSize
                          ||  imageHeight != maxRows * tileSize );
{'fraction_non_alphanumeric': 0.13428803026209132, 'fraction_numerical': 0.011888678735476898, 'mean_word_length': 2.967309753483387, 'pattern_counts': {'":': 0, '<': 111, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2064310', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2354, 'n_words': 637}