jasper

parent d796697e
//////////////////////////////////////////////////////////////////////////////
// ADT_Jasper.cpp
// Mario Chirinos Colunga
// School of Computer Science and Information Systems.
// Birkbeck, Univerity of London.
// 02 Feb 2008 - 02 Jul 2008
//----------------------------------------------------------------------------
// Jasper[1] implementation
// [1] Copyright (c) 2001-2006 Michael David Adams
// Copyright (c) 1999-2000 Image Power, Inc.
// Copyright (c) 1999-2000 The University of British Columbia
// http://www.ece.uvic.ca/~mdadams/jasper/
//////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <jasper/jasper.h>
#include "ADT_Jasper.h"
using namespace std;
bool ADT_jasper_openImage(unsigned short *&data, unsigned int &width, unsigned int &height, unsigned int &nChannels, const char *name)
{
jas_stream_t *imgStream;
jas_image_t *image;
jas_matrix_t *imgData;
int imgFmtId;
if (jas_init())
{
cerr << "can not initialize jasper library" << endl;
abort();
return EXIT_FAILURE;
}
/* Open the image file. */
if (!(imgStream = jas_stream_fopen(name, "rb")))
{
cerr << "cannot open:" << endl << name << endl;
return false;
}
if ((imgFmtId = jas_image_getfmt(imgStream)) < 0)
{
cerr << "unknown image format" << endl;
return false;
}
/* Decode the image. */
if (!( image = jas_image_decode(imgStream, imgFmtId, 0)))
{
cerr << "cannot load image" << endl;
return false;
}
//void printImgInfo(jas_image_t *image)
//printImgInfo(image, imgStream);
nChannels= jas_image_numcmpts(image);
width = jas_image_cmptwidth(image, 0);
height = jas_image_cmptheight(image, 0);
int depth = jas_image_cmptprec(image, 0);
/* Close the original image file. */
jas_stream_close(imgStream);
if (!(imgData = jas_matrix_create(height, width)))
{
cerr << "internal error: jas_matrix_create" << endl;
return false;
}
data = new unsigned short [nChannels*width*height];
for (unsigned int i=0; i<nChannels; i++)
{
if (width != (unsigned int) jas_image_cmptwidth(image, i)
|| height != (unsigned int) jas_image_cmptheight(image, i)
|| depth != jas_image_cmptprec(image, i))
{
cerr << "image dimensions differ" << endl;
return false;
}
if (jas_image_readcmpt(image, i, 0, 0, (jas_image_coord_t)width, (jas_image_coord_t)height, imgData))
{
cerr << "cannot read component data" << endl;
return false;
}
//memcpy(data + i*width*height, (unsigned char *)imgData->data_, width*height);
for (unsigned int y = 0; y < height; ++y)
{
for (unsigned int x = 0; x < width; ++x)
{
data[nChannels*(y*width+x)+i]= jas_matrix_get(imgData, y, x); //rgbrgbrgb..
}
}
}
jas_matrix_destroy(imgData);
jas_image_destroy(image);
jas_image_clearfmts();
return true;
}
//---------------------------------------------------------------------------
bool ADT_jasper_saveImage(unsigned short *data, unsigned int width, unsigned int height, unsigned int nChannels, const char *name, const char *format)
{
if (jas_init())
{
cerr << "can not initialize jasper library" << endl;
abort();
return false;
}
int depth = 8;
jas_image_cmptparm_t compparms[nChannels];
jas_image_t *image;
jas_matrix_t *imgData[nChannels];
for (unsigned int i = 0; i < nChannels; ++i)
{
compparms[i].tlx = 0;
compparms[i].tly = 0;
compparms[i].hstep = 1;
compparms[i].vstep = 1;
compparms[i].width = width;
compparms[i].height = height;
compparms[i].prec = depth;
compparms[i].sgnd = false;
}
if (!(image = jas_image_create(nChannels, compparms, JAS_CLRSPC_UNKNOWN)))
{
abort();
}
switch (nChannels)
{
case 1:
jas_image_setclrspc(image, JAS_CLRSPC_SGRAY);
jas_image_setcmpttype(image, 0,
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
break;
case 3:
jas_image_setclrspc(image, JAS_CLRSPC_SRGB);
jas_image_setcmpttype(image, 0,
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
jas_image_setcmpttype(image, 1,
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
jas_image_setcmpttype(image, 2,
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
break;
default:
cerr << "Multispectral images are not supported" << endl;
jas_image_destroy(image);
return false;
}
for (unsigned int i = 0; i < nChannels; ++i)
{
if (!(imgData[i] = jas_matrix_create(height, width)))
{
cerr << "internal error" << endl;
return false;
}
}
for (unsigned int i = 0; i < nChannels; ++i)
{
int max = 0;
int min = INT_MAX;
//memcpy(imgData[i]->data_, (int*) data + i*width*height, width*height);
for (unsigned int y = 0; y < height; ++y)
{
for (unsigned int x = 0; x < width; ++x)
{
int v = data[nChannels*(y*width+x)+i];
if(max < v)
max = v;
if(min > v)
min = v;
}
}
//cout << "min :" << min << ", max: " << max << endl;
for (unsigned int y = 0; y < height; ++y)
{
for (unsigned int x = 0; x < width; ++x)
{
//int delta = max-min;
//jas_matrix_set(imgData[i], y, x, (unsigned char)(255* (data[nChannels*(y*width+x)+i]-min)/delta) );
jas_matrix_set(imgData[i], y, x, (unsigned char)(255*data[nChannels*(y*width+x)+i]/(pow(2,12))) );
}
}
}
for (unsigned int i = 0; i < nChannels; ++i)
{
if (jas_image_writecmpt(image, i, 0, 0, width, height, imgData[i]))
{
return false;
}
}
jas_stream_t *outStream;
if (!(outStream = jas_stream_fopen(name, "rwb")))
{
cerr << "cannot open diff stream" << endl;
return false;
}
int fmtid = jas_image_strtofmt((char *)format);
if (jas_image_encode(image, outStream, fmtid, 0))
{
cerr << "cannot save" << endl;
return false;
}
jas_stream_close(outStream);
jas_matrix_destroy(*imgData);
jas_image_destroy(image);
jas_image_clearfmts();
return true;
}
//---------------------------------------------------------------------------
void printImgInfo(jas_image_t *image, jas_stream_t *imgStream)
{
if (jas_init())
{
cerr << "can not initialize jasper library" << endl;
abort();
return;
}
cout << "jas_image_t info..." << endl;
int nChannels=jas_image_numcmpts(image);
int width=jas_image_cmptwidth(image, 0);
int height=jas_image_cmptheight(image, 0);
int depth=jas_image_cmptprec(image, 0);
char format=jas_image_getfmt(imgStream);
int values=jas_image_cmptsgnd(image, 0);
cout << "nChannels= " << nChannels << endl;
cout << "width = " << width << endl;
cout << "height = " << height << endl;
cout << "depth = " << depth << endl;
cout << "format = " << format << endl;
cout << "values = " << values << endl;
}
//////////////////////////////////////////////////////////////////////////////
// ADT_Jasper.h
// Mario Chirinos Colunga
// School of Computer Science and Information Systems.
// Birkbeck, Univerity of London.
// 02 Feb 2008 - 02 Jul 2008
//----------------------------------------------------------------------------
// Jasper[1] implementation
// [1] Copyright (c) 2001-2006 Michael David Adamsx
// Copyright (c) 1999-2000 Image Power, Inc.
// Copyright (c) 1999-2000 The University of British Columbia
// http://www.ece.uvic.ca/~mdadams/jasper/
//////////////////////////////////////////////////////////////////////////////
#ifndef ADT_JASPER_H
#define ADT_JASPER_H
// your public header include
#include <jasper/jasper.h>
bool ADT_jasper_openImage(unsigned short *&data, unsigned int &width, unsigned int &height, unsigned int &nChannels, const char *name);
bool ADT_jasper_saveImage(unsigned short *data, unsigned int width, unsigned int height, unsigned int nChannels, const char *name, const char *format);
void printImgInfo(jas_image_t *image, jas_stream_t *imgStream);
#endif // ADT_JASPER_H
// EOF
////////////////////////////////////////////////////////////////////////////////
/**
* @file main.cpp
* @author Mario Chirinos Colunga
* @date
* @note main file template
*/
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cstdlib>
#include <string>
#include "ADT_Jasper.h"
using namespace std;
//------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
if(argc < 2)
{
cerr << "too few arguments" << endl;
return EXIT_FAILURE;
}
unsigned short *data;
unsigned int width;
unsigned int height;
unsigned int nChannels;
string str(argv[1]);
ADT_jasper_openImage(data, width, height, nChannels, argv[1]);
str = str.substr(0,str.length()-3) + "jpg";
ADT_jasper_saveImage(data,width,height,nChannels, str.c_str(),"jpg");
return 0;
}
//------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#
#
# Notes:
#-------------------------------------------------------------------------------
APPNAME = jp2ToJPGandStretch
#Compiler:
CC=g++
#Compiler flags
CFLAGS=-c -g -Wall
# INCLUIDES=$(shell pkg-config --cflags gtk+-2.0 libgnomeui-2.0)
# LIBS=$(shell pkg-config --libs gtk+-2.0 libgnomeui-2.0)
#Directories
DIRlib= /usr/local/lib
incDIR= /usr/local/include
#main function
mainP= main
#-------------------------------------------------------------------------------
all: Project
Project: mainP.o ADT_Jasper.o
$(CC) -o $(APPNAME) \
ADT_Jasper.o \
$(mainP).o \
-L $(DIRlib) \
-ljasper \
-I $(incDIR)
ADT_Jasper.o: ADT_Jasper.cpp
$(CC) $(CFLAGS) \
ADT_Jasper.cpp
mainP.o: $(mainP).cpp
$(CC) $(CFLAGS) \
$(mainP).cpp
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment