Skip to content

Commit

Permalink
libgui|Image: Image point ratio parameter
Browse files Browse the repository at this point in the history
An Image can specify a ratio for converting image pixels to layout points.

“@2x” images will automatically get a 0.5 point ratio.
  • Loading branch information
skyjake committed Nov 18, 2018
1 parent 8171ba4 commit be44767
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 22 deletions.
10 changes: 10 additions & 0 deletions doomsday/sdk/libgui/include/de/graphics/image.h
Expand Up @@ -143,6 +143,16 @@ class LIBGUI_PUBLIC Image : public ISerializable

GLPixelFormat glFormat() const;

/**
* Returns the ratio of how many points there are for each image pixel.
* This can be used to scale the image appropriately for the UI.
*
* @return Points per image pixel.
*/
float pointRatio() const;

void setPointRatio(float pointsPerPixel);

// Drawing/editing methods.
Image subImage(Rectanglei const &subArea) const;
void resize(Size const &size);
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libgui/include/de/graphics/imagebank.h
Expand Up @@ -50,7 +50,7 @@ class LIBGUI_PUBLIC ImageBank : public InfoBank
void add(DotPath const &path, String const &imageFilePath);
void addFromInfo(File const &file);

Image const &image(DotPath const &path) const;
const Image &image(DotPath const &path) const;

protected:
ISource *newSourceFromInfo(String const &id);
Expand Down
34 changes: 23 additions & 11 deletions doomsday/sdk/libgui/src/graphics/image.cpp
Expand Up @@ -338,20 +338,22 @@ DENG2_PIMPL(Image)
QImage image;
Block pixels;
ByteRefArray refPixels;
float pointRatio = 1.f;

Impl(Public *i, QImage const &img = QImage())
: Base(i), format(UseQImageFormat), image(img)
{
size = Size(img.width(), img.height());
}

Impl(Public *i, Impl const &other)
: Base (i),
format (other.format),
size (other.size),
image (other.image),
pixels (other.pixels),
refPixels(other.refPixels)
Impl(Public * i, Impl const &other)
: Base(i)
, format(other.format)
, size(other.size)
, image(other.image)
, pixels(other.pixels)
, refPixels(other.refPixels)
, pointRatio(other.pointRatio)
{}

Impl(Public *i, Size const &imgSize, Format imgFormat, IByteArray const &imgPixels)
Expand All @@ -367,7 +369,7 @@ Image::Image() : d(new Impl(this))
{}

Image::Image(Image const &other)
: de::ISerializable(), d(new Impl(this, *other.d))
: d(new Impl(this, *other.d))
{}

Image::Image(QImage const &image) : d(new Impl(this, image))
Expand All @@ -381,7 +383,7 @@ Image::Image(Size const &size, Format format, ByteRefArray const &refPixels)
: d(new Impl(this, size, format, refPixels))
{}

Image &Image::operator = (Image const &other)
Image &Image::operator=(Image const &other)
{
d.reset(new Impl(this, *other.d));
return *this;
Expand Down Expand Up @@ -452,7 +454,7 @@ int Image::stride() const
{
return d->image.bytesPerLine();
}
return depth()/8 * d->size.x;
return depth() / 8 * d->size.x;
}

int Image::byteCount() const
Expand All @@ -465,7 +467,7 @@ int Image::byteCount() const
{
return d->pixels.size();
}
return depth()/8 * d->size.x * d->size.y;
return depth() / 8 * d->size.x * d->size.y;
}

void const *Image::bits() const
Expand Down Expand Up @@ -587,6 +589,16 @@ GLPixelFormat Image::glFormat() const
return glFormat(d->format);
}

float Image::pointRatio() const
{
return d->pointRatio;
}

void Image::setPointRatio(float pointsPerPixel)
{
d->pointRatio = pointsPerPixel;
}

Image Image::subImage(Rectanglei const &subArea) const
{
IMAGE_ASSERT_EDITABLE(d);
Expand Down
25 changes: 17 additions & 8 deletions doomsday/sdk/libgui/src/graphics/imagebank.cpp
Expand Up @@ -18,7 +18,8 @@

#include "de/ImageBank"
#include "de/App"
#include "de/Folder"
#include "de/FileSystem"
#include "de/ImageFile"

#include <de/ScriptedInfo>

Expand All @@ -29,8 +30,12 @@ DENG2_PIMPL_NOREF(ImageBank)
struct ImageSource : public ISource
{
String filePath;
float pointRatio;

ImageSource(String const &path) : filePath(path) {}
ImageSource(String const &path, float pointRatio)
: filePath(path)
, pointRatio(pointRatio)
{}

Time modifiedAt() const
{
Expand All @@ -39,9 +44,12 @@ DENG2_PIMPL_NOREF(ImageBank)

Image load() const
{
Block imageData;
App::rootFolder().locate<File const>(filePath) >> imageData;
return Image::fromData(imageData);
Image img = FS::locate<const ImageFile>(filePath).image();
if (pointRatio > 0)
{
img.setPointRatio(pointRatio);
}
return img;
}
};

Expand All @@ -59,7 +67,7 @@ DENG2_PIMPL_NOREF(ImageBank)

duint sizeInMemory() const
{
return image.byteCount();
return duint(image.byteCount());
}
};
};
Expand All @@ -69,7 +77,7 @@ ImageBank::ImageBank(Flags const &flags) : InfoBank("ImageBank", flags), d(new I

void ImageBank::add(DotPath const &path, String const &imageFilePath)
{
Bank::add(path, new Impl::ImageSource(imageFilePath));
Bank::add(path, new Impl::ImageSource(imageFilePath, 0.f));
}

void ImageBank::addFromInfo(File const &file)
Expand All @@ -87,7 +95,8 @@ Image const &ImageBank::image(DotPath const &path) const
Bank::ISource *ImageBank::newSourceFromInfo(String const &id)
{
Record const &def = info()[id];
return new Impl::ImageSource(absolutePathInContext(def, def["path"]));
return new Impl::ImageSource(absolutePathInContext(def, def["path"]),
def.getf("pointRatio", 0.f));
}

Bank::IData *ImageBank::loadFromSource(ISource &source)
Expand Down
9 changes: 7 additions & 2 deletions doomsday/sdk/libgui/src/graphics/imagefile.cpp
Expand Up @@ -40,7 +40,7 @@ DENG2_PIMPL(ImageFile)

~Impl()
{
qDeleteAll(filtered.values());
qDeleteAll(filtered);
}

ImageFile *makeOrGetFiltered(BuiltInFilter filter)
Expand Down Expand Up @@ -169,7 +169,12 @@ Image ImageFile::image() const
}
else
{
return Image::fromData(*source(), extension());
Image img = Image::fromData(*source(), extension());
if (source()->name().contains("@2x.", Qt::CaseInsensitive))
{
img.setPointRatio(.5f);
}
return img;
}
}

Expand Down
1 change: 1 addition & 0 deletions doomsday/sdk/libgui/src/graphics/packageiconbank.cpp
Expand Up @@ -68,6 +68,7 @@ DENG2_PIMPL_NOREF(PackageIconBank)
img.resize(_displaySize);
}
}
img.setPointRatio(1.f);
return img;
}

Expand Down

0 comments on commit be44767

Please sign in to comment.