#include <fstream>
#include <openjpeg.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <gdcm_j2k.h>
#include <gdcm_jp2.h>
#include<iostream>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <fstream>
void error_callback(const char *msg, void *) {
(void)msg;
}
void warning_callback(const char *msg, void *) {
(void)msg;
}
void info_callback(const char *msg, void *) {
(void)msg;
}
{
std::ifstream is;
is.open( filename );
opj_dparameters_t parameters;
opj_event_mgr_t event_mgr;
opj_dinfo_t* dinfo;
opj_cio_t *cio;
opj_image_t *image = NULL;
is.seekg( 0, std::ios::end);
std::streampos buf_size = is.tellg();
char *dummy_buffer = new char[(unsigned int)buf_size];
is.seekg(0, std::ios::beg);
is.read( dummy_buffer, buf_size);
unsigned char *src = (unsigned char*)dummy_buffer;
uint32_t file_length = (uint32_t)buf_size;
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
event_mgr.error_handler = error_callback;
event_mgr.warning_handler = warning_callback;
event_mgr.info_handler = info_callback;
opj_set_default_decoder_parameters(¶meters);
parameters.cp_layer=0;
parameters.cp_reduce= res;
const char jp2magic[] = "\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A";
if( memcmp( src, jp2magic, sizeof(jp2magic) ) == 0 )
{
parameters.decod_format = 1;
}
else
{
assert( 0 );
}
parameters.cod_format = 11;
dinfo = opj_create_decompress(CODEC_JP2);
opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, NULL);
opj_setup_decoder(dinfo, ¶meters);
cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
image = opj_decode(dinfo, cio);
if(!image) {
opj_destroy_decompress(dinfo);
opj_cio_close(cio);
return 1;
}
opj_cp_t * cp = ((opj_jp2_t*)dinfo->jp2_handle)->j2k->cp;
opj_tcp_t *tcp = &cp->tcps[0];
opj_tccp_t *tccp = &tcp->tccps[0];
opj_j2k_t* j2k = NULL;
opj_jp2_t* jp2 = NULL;
jp2 = (opj_jp2_t*)dinfo->jp2_handle;
int reversible = jp2->j2k->cp->tcps->tccps->qmfbid;
int compno = 0;
opj_image_comp_t *comp = &image->comps[compno];
int Dimensions[2];
Dimensions[0]= comp->w;
Dimensions[1] = comp->h;
opj_cio_close(cio);
unsigned long len = Dimensions[0]*Dimensions[1] * image->numcomps;
char *raw = new char[len];
for (unsigned int compno = 0; compno < (unsigned int)image->numcomps; compno++)
{
opj_image_comp_t *comp = &image->comps[compno];
int w = image->comps[compno].w;
int h = image->comps[compno].h;
uint8_t *data8 = (uint8_t*)raw + compno;
for (int i = 0; i < w * h ; i++)
{
int v = image->comps[compno].data[i];
*data8 = (uint8_t)v;
data8 += image->numcomps;
}
}
de.SetByteValue( u, strlen(u) );
de1.SetByteValue( ms.GetString(), strlen(ms.GetString()));
const char mystr[] = "MONOCHROME2 ";
de2.SetByteValue(mystr, strlen(mystr));
if (flag == 1)
{
for (int i=0; i < No_Of_Resolutions; i++)
{
int a = 1;
int b =1;
while(a!=((No_Of_Resolutions)-i))
{
b = b*2;
a = a+1;
}
uint16_t row = (image->y1)/b;
uint16_t col = (image->x1)/b;
}
dea.SetByteValue( u1, strlen(u1) );
deb.SetByteValue( ms1.GetString(), strlen(ms1.GetString()));
const char mystr1[] = "MONOCHROME2 ";
dec.SetByteValue(mystr, strlen(mystr1));
des.SetValue(*sq);
des.SetVLToUndefined();
std::cerr << "unable to write image information" << std::endl;
return 1;
}
}
delete [] raw;
std::cout << "Not able to write";
return 0;
}
else
std::cout<<"\nabletoread";
unsigned short xmax = extent[0];
unsigned short ymax = extent[1];
unsigned short theChunkSize = 4;
unsigned short ychunk = extent[1]/theChunkSize;
unsigned short zmax = extent[2];
std::cout << "\n"<<xmax << "\n" << ymax<<"\n"<<zmax<<"\n" << image->numcomps<<"\n";
if (xmax == 0 || ymax == 0)
{
std::cerr << "Image has no size, unable to write zero-sized image." << std::endl;
return 0;
}
int z, y, nexty;
unsigned long prevLen = 0;
for (z = 0; z < zmax; ++z){
for (y = 0; y < ymax; y += ychunk){
nexty = y + ychunk;
if (nexty > ymax) nexty = ymax;
std::cout << "\n" <<len;
char* finalBuffer = new char[len];
memcpy(finalBuffer, &(raw[prevLen]), len);
std::cout << "\nable to write";
if (!theStreamWriter.
Write(finalBuffer, len)){
std::cerr << "writing failure:" << "output.dcm" << " at y = " << y << " and z= " << z << std::endl;
delete [] raw;
delete [] finalBuffer;
return 1;
}
delete [] finalBuffer;
prevLen += len;
}
}
delete raw;
delete[] src;
if(dinfo) {
opj_destroy_decompress(dinfo);
}
opj_image_destroy(image);
return true;
}
bool Different_Resolution(
gdcm::StreamImageWriter & theStreamWriter,
const char *filename,
int res, std::ostream& of)
{
bool b = true;
int flag = 1;
for(int i = res-1 ; i>=0; --i)
{
b = b && Write_Resolution( theStreamWriter, filename, i, of ,flag,sq,res);
flag = 0;
}
return b;
}
int main(int argc, char *argv[])
{
if( argc < 4 )
{
std::cerr << argv[0] << " input.jp2 output.dcm No. Of Resolutions " << std::endl;
return 1;
}
const char *filename = argv[1];
const char *outfilename = argv[2];
char *resolutions = argv[3];
int res = int((*resolutions)-48);
std::ofstream of;
of.open( outfilename, std::ios::out | std::ios::binary );
if( !Different_Resolution( theStreamWriter, filename,res,of ) ) return 1;
uint16_t firstTag1 = 0xfffe;
uint16_t secondTag1 = 0xe0dd;
uint32_t thirdTag1 = 0x00000000;
const int theBufferSize1 = 2*sizeof(uint16_t)+sizeof(uint32_t);
char* tmpBuffer2 = new char[theBufferSize1];
memcpy(&(tmpBuffer2[0]), &firstTag1, sizeof(uint16_t));
memcpy(&(tmpBuffer2[sizeof(uint16_t)]), &secondTag1, sizeof(uint16_t));
memcpy(&(tmpBuffer2[2*sizeof(uint16_t)]), &thirdTag1, sizeof(uint32_t));
assert( of && !of.eof() && of.good() );
of.write(tmpBuffer2, theBufferSize1);
of.flush();
assert( of );
return 0;
}