qjpeg/FloatPlane.cpp
author viric@llimona
Mon, 22 Jan 2007 00:45:57 +0100
changeset 76 9cbf4c7e7986
child 78 a55bf2fa3f74
permissions -rw-r--r--
First classes for the qjpeg project.

#include "FloatPlane.h"
#include <cstring>

extern "C" {
#include <stdio.h>
#include <pam.h>
}

FloatPlane::FloatPlane()
{
    setMaxMinValue();
    ready = false;
}

void FloatPlane::setMaxMinValue(unsigned int max, unsigned int min)
{
	MAXVALUE=max;
	MINVALUE=min;
}

FloatPlane::FloatPlane(float *_ptr)
{
	ptr = _ptr;
    setMaxMinValue();
    ready = true;
}

FloatPlane::FloatPlane(const unsigned int _width, const unsigned int _height)
{
    allocate(_width, _height);

    setMaxMinValue();
    ready = true;
}

void FloatPlane::allocate(const unsigned int _width, const unsigned int _height)
{
    width = _width;
    height = _height;
	ptr = new float[width*height];
    ready = true;
}

void FloatPlane::writePGM(const char * filename)
{
    struct pam outpam;
    unsigned int row;
    tuple *tuplerow;
 
    FILE* file;

    if (!ready)
        return;

    file = fopen(filename, "wb");
    if (file == NULL)
        return;

    outpam.size = sizeof(outpam);
    outpam.len = outpam.size;
    outpam.file = file;
    outpam.format = PGM_FORMAT;
    outpam.plainformat = 0; /* false */
    outpam.width = width;
    outpam.height = height;
    outpam.depth = 1; /* Grayscale - 1 sample per tuple */
    outpam.allocation_depth = 0; /* Same as depth. */
    outpam.maxval = MAXVALUE;
    std::strcpy(outpam.tuple_type, "GRAYSCALE"); /* PGM non transparent */
 
    /* needs size,len,file,format,height,width,depth,maxval, tuple_type */
    pnm_writepaminit(&outpam);
 
    tuplerow = pnm_allocpamrow(&outpam);
 
    for (row = 0; row < height; row++) {
        unsigned int column;
        for (column = 0; column < width; ++column) {
            /* Only one plane */
            /* float to int! */
            tuplerow[column][0] = ptr[column+width*row];
        }
        pnm_writepamrow(&outpam, tuplerow);
    }
 
    pnm_freepamrow(tuplerow);
    fclose(file);
}

unsigned int FloatPlane::getMaxValue()
{
    return MAXVALUE;
}

unsigned int FloatPlane::getWidth()
{
    return width;
}

unsigned int FloatPlane::getHeight()
{
    return height;
}

float * FloatPlane::getPtr()
{
    return ptr;
}

void FloatPlane::free()
{
    delete[] ptr;
}