⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image.cpp

📁 this is source code for image compression following jpeg2000 staandard
💻 CPP
字号:
/*---------------------------------------------------------------------------*/
// Image Compression Toolbox v1.2
// written by
// Satish Kumar S
// satishkumr@lycos.com
//
// Copyright 1999 Satish Kumar S
//
// Permission is granted to use this software for research purposes as
// long as this notice stays attached to this software.
/*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include "Image.h"

typedef unsigned char BYTE;

//----------------------------------------------
// Implementation of the Image class, a very basic
// framework for loading/saving .RAW image files.
//-----------------------------------------------

int Image::LoadRawFile(char *fname, int twidth, int theight)
{
    FILE *fp;

    fp = fopen(fname, "rb");
    if (!fp)
        return 1;

    int imsize = twidth*theight;
    BYTE *bytedata = new BYTE[imsize];
    double *tdata = new double[imsize];
    if (!bytedata || !tdata)
    {
        fclose(fp);
        if (bytedata) 
            delete[] bytedata;
        if (tdata) 
            delete[] tdata;
        return 1;
    }

    if ((int)fread(bytedata, sizeof(BYTE), imsize, fp) != imsize)
    {
        fclose(fp);
        delete[] bytedata;
        delete[] tdata;
        return 1;
    }

    fclose(fp);
    for(int i=0; i<imsize; i++)
        tdata[i] = (double)bytedata[i];

    if (data)
        delete[] data;

    width = twidth;
    height = theight;
    data = tdata;

    delete[] bytedata;
    return 0;
}

int Image::SaveRawFile(char *fname, int mustscale)
{
    FILE *fp;
    int i;

    fp = fopen(fname, "wb");
    if (!fp)
        return 1;

    int imsize = width * height;
    BYTE *bytedata = new BYTE[imsize];
    if (!bytedata)
    {
        fclose(fp);
        return 1;
    }

    if (mustscale)
    {
        double scalefactor = 1.0;
        double maxvalue = 0, minvalue = 1000000.0;
        for(i=0; i<imsize; i++)
        {
            if (maxvalue < data[i])
                maxvalue = data[i];
            if (minvalue > data[i])
                minvalue = data[i];
        }
        scalefactor = 256.0/(maxvalue - minvalue);
        for(i=0; i<imsize; i++)
            bytedata[i] = (BYTE)((data[i] - minvalue) * scalefactor);
    }
    else
    {
        for(i=0; i<imsize; i++)
            bytedata[i] = (BYTE)(data[i]);
    }

    fwrite(bytedata, imsize, sizeof(BYTE), fp);

    delete[] bytedata;
    fclose(fp);
    return 0;
}

int Image::GetData(int x, int y, int xspan, int yspan, double *tdata)
{
    int i,j;

    // check for legality of the values.
    if (x>width || y>width) 
        return 1;
    if (x+xspan > width)
        return 1;
    if (y+yspan > height)
        return 1;

    double *target = tdata;
    for(i=0; i<yspan; i++)
    {
        // copy each row.
        double *source = (data + (y+i)*width + x);
        j = xspan;
        while(j--)
            *target++ = *source++;
    }
    return 0;
}

int Image::SetData(int x, int y, int xspan, int yspan, double *tdata)
{
    int i,j;

    // check for legality of the values.
    if (x>width || y>width) 
        return 1;
    if (x+xspan > width)
        return 1;
    if (y+yspan > height)
        return 1;

    double *source = tdata;
    for(i=0; i<yspan; i++)
    {
        // copy each row.
        double *target = (data + (y+i)*width + x);
        j = xspan;
        while(j--)
            *target++ = *source++;
    }
    return 0;
}

int Image::SetZeroBelow(double threshold)
{
    int i = width * height;
    int num = 0;
    double *dataptr = data;

    while(i--)
    {
        if (fabs(*dataptr) < threshold)
        {
            *dataptr = 0;
            num++;
        }
        dataptr++;
    }
    return num;
}

void Image::InitEmptyImage(int w, int h)
{
    Image::~Image();

    if (data = new double[w*h])
    {
        width = w;
        height = h;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -