diff --git a/src/libinit_cl.cpp b/src/libinit_cl.cpp index 9c22e98bd..295c07f66 100644 --- a/src/libinit_cl.cpp +++ b/src/libinit_cl.cpp @@ -200,7 +200,8 @@ void LibInit_cl() const string magick_readKey[]={"RGB","SUB_RECT","MAP",KLISTEND}; new DLibFunRetNew(lib::magick_read,string("MAGICK_READ"),1,magick_readKey); new DLibFunRetNew(lib::magick_readindexes,string("MAGICK_READINDEXES"),1); - new DLibPro(lib::magick_readcolormapRGB,string("MAGICK_READCOLORMAPRGB"),4); + const string magick_readcolormapKey[]={"BACKGROUND_COLOR",KLISTEND}; //0 + new DLibPro(lib::magick_readcolormapRGB,string("MAGICK_READCOLORMAPRGB"),4,magick_readcolormapKey); const string magick_writeKey[]={"RGB",KLISTEND}; new DLibPro(lib::magick_write,string("MAGICK_WRITE"),2,magick_writeKey); new DLibPro(lib::magick_writefile,string("MAGICK_WRITEFILE"),3); @@ -234,7 +235,8 @@ void LibInit_cl() //hmm new DLibPro(lib::magick_display,string("MAGICK_DISPLAY"),1); - + new DLibFunRetNew(lib::magick_type,string("MAGICK_TYPE"),1); + new DLibFunRetNew(lib::magick_class,string("MAGICK_CLASS"),1); // SA: support fot query/ping routines const string magick_pingKey[] = {"INFO", "CHANNELS", "DIMENSIONS", "HAS_PALETTE", "IMAGE_INDEX", "NUM_IMAGES", "PIXEL_TYPE",/* "SUPPORTED_READ", "SUPPORTED_WRITE", */"TYPE",KLISTEND}; diff --git a/src/magick_cl.cpp b/src/magick_cl.cpp index e7b7136c6..0ac160c10 100644 --- a/src/magick_cl.cpp +++ b/src/magick_cl.cpp @@ -41,13 +41,15 @@ #include "magick_cl.hpp" #include "graphicsdevice.hpp" -// If Magick has not been initialized, do it here, instead of initilizing it in the main program (speedup and avoid strange backtraces) +// If Magick has not been initialized, do it here, instead of initializing it in the main program (speedup and avoid strange backtraces) // Also warn about limitations due to local implementation of Magick library. //We should do more, by example hat octave people do in their code (they circumvent some other limitations) #define START_MAGICK \ if (notInitialized ) { \ notInitialized = false; \ - Magick::InitializeMagick(NULL); } + Magick::InitializeMagick(NULL); \ + if ( QuantumDepth < 16) fprintf(stderr, "%% INFO: the %s library support only 8 bit images.\n", MagickPackageName);\ + } namespace lib { @@ -59,6 +61,8 @@ namespace lib { unsigned int gValid[40]; unsigned int gCount = 0; static bool notInitialized = true; + static const std::string imageTypeList[]={"BilevelType","GrayscaleType","GrayscaleMatteType","PaletteType","PaletteMatteType","TrueColorType","TrueColorMatteType","ColorSeparationType","ColorSeparationMatteType","OptimizeType","UnknownType"}; + static const std::string imageClassList[]={"UndefinedClass","DirectClass","PseudoClass"}; __attribute__ ((destructor)) static void destruct(void) { // This is GCC only. Possible memory leak with other compilers. int i; @@ -114,7 +118,7 @@ namespace lib { cerr << warning_.what() << endl; } if ((a->rows() * a->columns()) == 0) e->Throw("Error reading image dimensions!"); - a->flip(); + a->flip(); //necessary unsigned int mid; mid = magick_image(e, a); return new DUIntGDL(mid); @@ -123,7 +127,7 @@ namespace lib { } return NULL; //pacify -Wreturn-type } - +// a=magick_ping(name,type,"INFO", "CHANNELS", "DIMENSIONS", "HAS_PALETTE", "IMAGE_INDEX","NUM_IMAGES", "PIXEL_TYPE",/* "SUPPORTED_READ", "SUPPORTED_WRITE", */"TYPE") BaseGDL * magick_ping(EnvT* e) { // TODO! //if (e->KeywordPresent("SUPPORTED_READ") || e->KeywordPresent("SUPPORTED_WRITE")) @@ -139,7 +143,7 @@ namespace lib { if (filename.length() == 0) return new DLongGDL(0); WordExp(filename); - + Image *a = new Image; try { a->ping(filename); @@ -193,7 +197,7 @@ namespace lib { // TODO! multiple images (using the Magick++ STL interface) DLong image_index, num_images; image_index = 0; - num_images = 1; + num_images = 1; //a->scene()+1; DInt pixel_type; pixel_type = a->depth() == 16 ? 2 : 1; @@ -214,7 +218,7 @@ namespace lib { a->magick(); if (debug == 1) cout << "Type (via a->magick()) : " << type << endl; - + bool addSBit=(a->magick() == "PNG"); //only one type? static int infoIx = e->KeywordIx("INFO"); if (e->KeywordPresent(infoIx)) { e->AssureGlobalKW(infoIx); @@ -225,12 +229,14 @@ namespace lib { SpDLong aLong; SpDInt aInt; SpDLong aLongArr2(dimension(2)); + SpDByte aByteArr2(dimension(5)); info_desc->AddTag("CHANNELS", &aLong); info_desc->AddTag("DIMENSIONS", &aLongArr2); info_desc->AddTag("HAS_PALETTE", &aInt); info_desc->AddTag("IMAGE_INDEX", &aLong); info_desc->AddTag("NUM_IMAGES", &aLong); info_desc->AddTag("PIXEL_TYPE", &aInt); + if (addSBit) info_desc->AddTag("SBIT_VALUES", &aByteArr2); info_desc->AddTag("TYPE", &aString); DStructGDL* info = new DStructGDL(info_desc, dimension()); @@ -361,45 +367,34 @@ namespace lib { e->AssureScalarPar(0, mid); unsigned int columns, rows; Image *image = magick_image(e, mid); - if (image->classType() == DirectClass) - e->Throw("Not an indexed image: " + e->GetParString(0)); + if (image->classType() != PseudoClass) e->Throw("Not an indexed image: " + e->GetParString(0)); columns = image->columns(); rows = image->rows(); - - if (image->matte() == 0) { - - SizeT c[2]; - c[0] = columns; - c[1] = rows; - dimension dim(c, 2); - DByteGDL *bImage = new DByteGDL(dim, BaseGDL::NOZERO); - -// const PixelPacket* pixel; - const IndexPacket* index; -// pixel = image->getPixels(0, 0, columns, rows); - index = image->getIndexes(); - - if (index == NULL) { - string txt = "Warning -- Magick's getIndexes() returned NULL for: "; - string txt2 = ", using unsafe patch."; - //PATCH to get something until we understand what's going on - cerr << (txt + e->GetParString(0) + txt2) << endl; - string map = "R"; - image->write(0, 0, columns, rows, map, CharPixel, &(*bImage)[0]); - return bImage; - } + SizeT c[2]; + c[0] = columns; + c[1] = rows; + dimension dim(c, 2); +#ifdef USE_MAGICK6 + if (image->modulusDepth() <= 8) +#else + if (image->depth() <= 8) +#endif + { + DByteGDL *bImage = new DByteGDL(dim, BaseGDL::ZERO); + PixelPacket *pixel_cache = image->getPixels(0,0,columns,rows); //magick command, without it writePixels do NOTHING! + image->writePixels(IndexQuantum,(unsigned char*)(bImage->DataAddr())); return bImage; - } else { - // we do have to manage an extra channel for transparency - string map = "RA"; - SizeT c[3]; - c[0] = map.length(); // see code "magick_read" below - c[1] = columns; - c[2] = rows; - dimension dim(c, 3); - DByteGDL *bImage = new DByteGDL(dim, BaseGDL::NOZERO); - image->write(0, 0, columns, rows, map, CharPixel, &(*bImage)[0]); + } +#ifdef USE_MAGICK6 + else if (image->modulusDepth() <= 16) +#else + else if (image->depth() <= 16) +#endif + { + DUIntGDL *bImage = new DUIntGDL(dim, BaseGDL::NOZERO); + PixelPacket *pixel_cache = image->getPixels(0,0,columns,rows); //magick command, without it writePixels do NOTHING! + image->writePixels(IndexQuantum,(unsigned char*)(bImage->DataAddr())); //probably not good, impossible to test yet. return bImage; } } catch (Exception &error_) { @@ -416,14 +411,11 @@ namespace lib { DUInt mid; e->AssureScalarPar(0, mid); Image* image = magick_image(e, mid); - if (image->classType() == DirectClass) - e->Throw("Not an indexed image: " + e->GetParString(0)); - + bool bgnotfound=true; + if (image->classType() == PseudoClass) { - unsigned int Quant, scale, i; - if (QuantumDepth == 16) Quant = 65535; - if (QuantumDepth == 8) Quant = 255; - + //retrieve background color + Color bg=image->backgroundColor(); unsigned int cmapsize = image->colorMapSize(); dimension cmap(cmapsize, 1); Color col; @@ -434,23 +426,25 @@ namespace lib { if (image->depth() <= 8) #endif { + DByteGDL* bgIndex=new DByteGDL(0); - scale = MaxRGB; DByteGDL *R, *G, *B; R = new DByteGDL(cmap, BaseGDL::NOZERO); G = new DByteGDL(cmap, BaseGDL::NOZERO); B = new DByteGDL(cmap, BaseGDL::NOZERO); - for (i = 0; i < cmapsize; ++i) { + for (int i = 0; i < cmapsize; ++i) { col = image->colorMap(i); - (*R)[i] = (col.redQuantum()) ; //* scale / Quant; - (*G)[i] = (col.greenQuantum()) ; // * scale / Quant; - (*B)[i] = (col.blueQuantum()) ; //* scale / Quant; + (*R)[i] = (col.redQuantum()) ; + (*G)[i] = (col.greenQuantum()) ; + (*B)[i] = (col.blueQuantum()) ; + if (bgnotfound && (col==bg) ) {bgnotfound=false; (*bgIndex)[0]=i;} } if (nParam > 1) e->SetPar(1, R); if (nParam > 2) e->SetPar(2, G); if (nParam > 3) e->SetPar(3, B); + if (e->KeywordPresent(0)) {e->AssureGlobalKW(0); e->SetKW(0, bgIndex);} } #ifdef USE_MAGICK6 else if (image->modulusDepth() <= 16) @@ -458,21 +452,23 @@ namespace lib { else if (image->depth() <= 16) #endif { - scale = MaxRGB; + DUIntGDL* bgIndex=new DUIntGDL(0); DUIntGDL *R, *G, *B; R = new DUIntGDL(cmap, BaseGDL::NOZERO); G = new DUIntGDL(cmap, BaseGDL::NOZERO); B = new DUIntGDL(cmap, BaseGDL::NOZERO); - for (i = 0; i < cmapsize; ++i) { + for (int i = 0; i < cmapsize; ++i) { col = image->colorMap(i); - (*R)[i] = (col.redQuantum()) ; //* scale / Quant; - (*G)[i] = (col.greenQuantum()) ; //* scale / Quant; - (*B)[i] = (col.blueQuantum()) ; //* scale / Quant; + (*R)[i] = (col.redQuantum()) ; + (*G)[i] = (col.greenQuantum()) ; + (*B)[i] = (col.blueQuantum()) ; + if (bgnotfound && col==bg ) {bgnotfound=false; (*bgIndex)[0]=i;} } if (nParam > 1) e->SetPar(1, R); if (nParam > 2) e->SetPar(2, G); if (nParam > 3) e->SetPar(3, B); + if (e->KeywordPresent(0)) {e->AssureGlobalKW(0); e->SetKW(0, bgIndex);} } else { e->Throw("Unknown Image type, too many colors"); } @@ -612,25 +608,17 @@ namespace lib { if (image->matte()) map = map + "A"; } } - - DByteGDL * bImage = - static_cast (GDLimage->Convert2(GDL_BYTE, BaseGDL::COPY)); - Guard bImageGuard(bImage); - - image->read(columns, rows, map, CharPixel, &(*bImage)[0]); - /* } - else if(image->depth() == 16) - { - DUIntGDL * iImage= - static_cast(GDLimage->Convert2(GDL_UINT,BaseGDL::COPY)); - - image->read(columns,rows,map, ShortPixel,&(*iImage)[0]); - } - else - { - e->Throw("Unsupported bit depth"); - }*/ - + if (image->depth() == 8) { + DByteGDL * bImage = static_cast (GDLimage->Convert2(GDL_BYTE, BaseGDL::COPY)); + Guard bImageGuard(bImage); + image->read(columns, rows, map, CharPixel, &(*bImage)[0]); + } else if (image->depth() == 16) { + DUIntGDL * iImage = static_cast (GDLimage->Convert2(GDL_UINT, BaseGDL::COPY)); + Guard iImageGuard(iImage); + image->read(columns, rows, map, ShortPixel, &(*iImage)[0]); + } else { + e->Throw("Unsupported bit depth"); + } } else { columns = GDLimage->Dim(0); rows = GDLimage->Dim(1); @@ -690,7 +678,7 @@ namespace lib { e->AssureScalarPar(0, mid); Image* image = magick_image(e, mid); size_t nParam = e->NParam(1); - if (nParam == 2) { + if (nParam == 2) { //to be tested, works? DUInt ncol; e->AssureScalarPar(1, ncol); image->colorMapSize(ncol); @@ -918,8 +906,9 @@ namespace lib { DUInt mid; e->AssureScalarPar(0, mid); Image* image = magick_image(e, mid); + image->modifyImage(); //set the number of colors; - DLong ncol = 256; + DLong ncol = (image->depth()<16)?256:65635; if (nParam > 1) e->AssureLongScalarPar(1, ncol); static int TRUECOLORIx = e->KeywordIx("TRUECOLOR"); @@ -936,18 +925,31 @@ namespace lib { else image->quantizeColorSpace(RGBColorspace); -// image->colorMapSize(ncol); -// image->classType(PseudoClass); -// image->type(PaletteType); image->quantizeColors(ncol); image->quantizeDither(dither); image->quantize(); -// image->syncPixels(); //magick_replace(e, mid, image); } catch (Exception &error_) { e->Throw(error_.what()); } } + + //return type + BaseGDL* magick_type(EnvT* e){ + START_MAGICK; + DUInt mid; + e->AssureScalarPar(0, mid); + Image* image = magick_image(e, mid); + return new DStringGDL(imageTypeList[image->type()]); + } + //return class + BaseGDL* magick_class(EnvT* e){ + START_MAGICK; + DUInt mid; + e->AssureScalarPar(0, mid); + Image* image = magick_image(e, mid); + return new DStringGDL(imageClassList[image->classType()]); + } //MAGICK_DISPLAY,mid diff --git a/src/magick_cl.hpp b/src/magick_cl.hpp index 60c466d0f..c5109344b 100644 --- a/src/magick_cl.hpp +++ b/src/magick_cl.hpp @@ -64,6 +64,9 @@ namespace lib { //hmm void magick_display(EnvT* e); + //gd + BaseGDL* magick_type(EnvT* e); + BaseGDL* magick_class(EnvT* e); // SA: query/ping routines support BaseGDL * magick_ping(EnvT* e); diff --git a/src/pro/curvefit.pro b/src/pro/curvefit.pro index 2aed0dc38..d6f905de0 100644 --- a/src/pro/curvefit.pro +++ b/src/pro/curvefit.pro @@ -25,14 +25,15 @@ FUNCTION CURVEFIT, x, y, w, p, sigma, FUNCTION_NAME = fcn, FITA=fita, $ if n_elements(fcn) eq 0 then fcn="FUNCT" if n_elements(w) eq 0 then w=x*0+1. if (n_elements(fita) gt 0) then begin - parinfo=replicate({FIXED:0},7) ; logic is reverse from idl... + parinfo=replicate({FIXED:0},n_elements(p)) ; logic is reverse from idl... ww=where(fita eq 0, count) - if (count gt 0) then parinfo.fixed[ww]=1 + if (count gt 0) then parinfo[ww].fixed=1 yfit = mpcurvefit (x, y, w, p, sigma, function_name=fcn, $ iter=iter, itmax=maxiter, $ chisq=bestnorm, $ /nocovar, yerror=yerror, $ noderivative=noderivative, ftol=tol, $ + parinfo = parinfo, $ STATUS=status, _EXTRA=extra ) endif else yfit = mpcurvefit (x, y, w, p, sigma, function_name=fcn, $ iter=iter, itmax=maxiter, $ diff --git a/src/pro/query_ppm.pro b/src/pro/query_ppm.pro index a52fb483c..d3922eb74 100644 --- a/src/pro/query_ppm.pro +++ b/src/pro/query_ppm.pro @@ -1,6 +1,8 @@ ; ; by Sylwester Arabas -function QUERY_PPM, filename, info, image_index=image_index +; Check PPM & PGM; James Tappin. + +function QUERY_PPM, filename, info, maxval = maxval ; compile_opt idl2, hidden @@ -12,10 +14,15 @@ ON_ERROR, 2 if (MAGICK_EXISTS() EQ 0) then begin MESSAGE, /continue, "GDL was compiled without ImageMagick support." MESSAGE, "You must have ImageMagick support to use this functionaly." -endif + endif + ; ; TODO: MAXVAL keyword ; -return, MAGICK_PING(filename, 'PNM', info=info) +if arg_present(maxval) then message, /continue, $ + "MAXVAL not yet implemented." + +if MAGICK_PING(filename, 'PPM', info = info) then return, 1 +return, magick_ping(filename, 'PGM', info = info) ; end diff --git a/src/pro/read_bmp.pro b/src/pro/read_bmp.pro index d710f8b96..b3a84054a 100644 --- a/src/pro/read_bmp.pro +++ b/src/pro/read_bmp.pro @@ -80,15 +80,11 @@ if (FILE_TEST(filename, /regular) EQ 0) then MESSAGE, "Not a regular File: "+fil if ( ~MAGICK_PING(filename, 'BMP') )then MESSAGE, "File "+filename+" is not in bitmap file format." READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable, /order -if n_elements(colortable) then begin +if (n_elements(colortable) gt 0) then begin red=colortable[*,0] green=colortable[*,1] blue=colortable[*,2] -endif else begin - red=bindgen(256) - green=red - blue=red -endelse +endif return, image ; end diff --git a/src/pro/read_gif.pro b/src/pro/read_gif.pro index 50c72d264..415ff183f 100644 --- a/src/pro/read_gif.pro +++ b/src/pro/read_gif.pro @@ -69,16 +69,16 @@ if (STRLEN(filename) EQ 0) then MESSAGE, "Null filename not allowed." if ((FILE_INFO(filename)).exists EQ 0) then MESSAGE, "Error opening file. File: "+filename if (FILE_TEST(filename, /regular) EQ 0) then MESSAGE, "Not a regular File: "+filename ; -; -if ( ~MAGICK_PING(filename, 'GIF') and ~MAGICK_PING(filename, 'GIF87') )then begin - MESSAGE, "error: Not a GIF file." -endif -READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable, /order +if ( ~MAGICK_PING(filename, 'GIF') and ~MAGICK_PING(filename, 'GIF87') )then MESSAGE, "error: Not a GIF file." +READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable, background_color=bgc +if ( n_elements(colortable) eq 0 ) then Message,"GIF file is not a 8 bit GIF" red=colortable[*,0] green=colortable[*,1] blue=colortable[*,2] - +; it happens that magick sends back a truecolor image, when IDL would +; just send back a 2D index image. Do as IDL does: +if (size(image))[0] eq 3 then image=reform(image[0,*,*]) end diff --git a/src/pro/read_image.pro b/src/pro/read_image.pro index 7932891e1..57a49c0b4 100644 --- a/src/pro/read_image.pro +++ b/src/pro/read_image.pro @@ -73,19 +73,17 @@ if ((N_PARAMS() EQ 0) OR (N_PARAMS() GT 4)) then $ MESSAGE, "Incorrect number of arguments." ; if (N_ELEMENTS(filename) GT 1) then MESSAGE, "Only one file at once !" -if (STRLEN(filename) EQ 0) then MESSAGE, "Null filename not allowed." -if ((FILE_INFO(filename)).exists EQ 0) then MESSAGE, "Error opening file. File: "+filename -if (FILE_TEST(filename, /regular) EQ 0) then MESSAGE, "Not a regular File: "+filename ; ; First, we have to test whether the file is here ; status=QUERY_IMAGE(filename, info) ; if (status EQ 0) then begin - MESSAGE, 'Not a valid image file: '+filename + MESSAGE, /INFO, 'Not a valid image file: '+filename + return endif ; if query_image said it's OK, just use read_anything: -READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable, /order +READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable if n_elements(colortable) gt 0 then begin red=colortable[*,0] green=colortable[*,1] diff --git a/src/pro/read_jpeg.pro b/src/pro/read_jpeg.pro index 2090a7c65..d59da53cb 100644 --- a/src/pro/read_jpeg.pro +++ b/src/pro/read_jpeg.pro @@ -64,19 +64,10 @@ pro READ_JPEG, filename, unit=unit, image, colortable, buffer=buffer, $ colors=ncolors, dither=dither, grayscale=grayscale, order=order, $ - true=true, two_pass_quantize=two_pass_quantize, $ - help=help, test=test, debug=debug - + true=true, two_pass_quantize=two_pass_quantize + compile_opt hidden, idl2 - ON_ERROR, 2 -if KEYWORD_SET(help) then begin - print, 'pro READ_JPEG, filename, unit=unit, image, colortable, buffer=buffer, $' - print, ' colors=ncolors, dither=dither, grayscale=grayscale, order=order, $' - print, ' true=true, two_pass_quantize=two_pass_quantize, $' - print, ' help=help, test=test, debug=debug' - return -endif ; ; Do we have access to ImageMagick functionnalities ?? ; @@ -102,10 +93,12 @@ if (FILE_TEST(filename, /regular) EQ 0) then MESSAGE, "Not a regular File: "+fil if ( ~MAGICK_PING(filename, 'JPEG') and ~MAGICK_PING(filename, 'JNG') )then begin MESSAGE, "JPEG error: Not a JPEG file." endif +; if colortable IS present, irrespective of the presence of colors, +; will get a colormap, so will quantize the jpeg: +if (arg_present(colortable) && ~keyword_set(ncolors)) then ncolors=keyword_set(grayscale)?256:252; IDL default READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable, colors=ncolors, $ dither=dither, grayscale=grayscale, order=order, $ true=true -; end diff --git a/src/pro/read_jpeg2000.pro b/src/pro/read_jpeg2000.pro index 2cc29a2d6..6244ffb28 100644 --- a/src/pro/read_jpeg2000.pro +++ b/src/pro/read_jpeg2000.pro @@ -62,6 +62,11 @@ endif ; if (N_PARAMS() NE 1 and N_PARAMS() ne 4 ) then MESSAGE, "Incorrect number of arguments." ; +if KEYWORD_SET(discard_levels) then MESSAGE,/INF, "DISCARD_LEVEL Keyword ignored." +if KEYWORD_SET(max_layers) then MESSAGE,/INF, "MAX_LAYERS Keyword ignored." +if KEYWORD_SET(region) then MESSAGE, "REGION Keyword not supported." + + if (N_ELEMENTS(filename) GT 1) then MESSAGE, "Only one file at once !" if (STRLEN(filename) EQ 0) then MESSAGE, "Null filename not allowed." ; diff --git a/src/pro/read_png.pro b/src/pro/read_png.pro index b0230cd1d..8bbe9b26c 100644 --- a/src/pro/read_png.pro +++ b/src/pro/read_png.pro @@ -63,13 +63,8 @@ ; ;- ; -function INTERNAL_READ_PNG, filename, red, green, blue, $ - order=order, transparent=transparent, $ - test=test, verbose=verbose -; - +function INTERNAL_READ_PNG, filename, red, green, blue, order=order, transparent=transparent compile_opt hidden, idl2 - ON_ERROR, 2 ; this line allows to compile also in IDL ... @@ -94,84 +89,36 @@ if (FILE_TEST(filename, /regular) EQ 0) then MESSAGE, "Not a regular File: "+fil ; ; testing whether the format is as expected ; -if ~MAGICK_PING(filename, 'PNG') then begin - MESSAGE, /continue, "PNG error: Not a PNG file:" - if MAGICK_PING(filename, 'JPEG') then MESSAGE, "seems to be a JPEG file" - if MAGICK_PING(filename, 'GIF') then MESSAGE, "seems to be a GIF file" - if MAGICK_PING(filename, 'PDF') then MESSAGE, "seems to be a PDF file" - MESSAGE, "unknown/untested format file" -endif +if ~MAGICK_PING(filename, 'PNG') then MESSAGE, "File "+filename+" is not in PNG format." ; mid=MAGICK_OPEN(filename) ; ;;flip if order is set ; -if (KEYWORD_SET(order)) then MAGICK_FLIP, mid +READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable, order=order +if ( n_elements(colortable) gt 0 ) then begin + red=colortable[*,0] + green=colortable[*,1] + blue=colortable[*,2] +endif +transparent=0 ; fake -if (magick_IndexedColor(mid)) then begin - image=MAGICK_READINDEXES(mid) - MAGICK_READCOLORMAPRGB, mid, red, green, blue - colortable=[[red],[green],[blue]] -endif else begin - ;; AC 2012-Feb-02 the effective order of reading was bad ... - ;; now it is OK on all tested PNG images, including images with transparency - image=MAGICK_READ(mid, rgb=1s) ; must be short -endelse -; -MAGICK_CLOSE, mid -; -if KEYWORD_SET(test) then STOP -; return, image ; end ; ; ----------------------------- Procedure ------------------------ ; -pro READ_PNG, filename, image, red, green, blue, $ - order=order, transparent=transparent, $ - help=help, test=test, verbose=verbose -; - +pro READ_PNG, filename, image, red, green, blue, order=order, transparent=transparent, verbose=verbose compile_opt hidden, idl2 - ON_ERROR, 2 -; -if KEYWORD_SET(help) then begin - print, 'pro READ_PNG, filename, red, green, blue, $' - print, ' order=order, transparent=transparent, $' - print, ' help=help, test=test, verbose=verbose' - return -endif -; -image=INTERNAL_READ_PNG(filename, red, green, blue, $ - order=order, transparent=transparent, $ - test=test, verbose=verbose) -; +image=INTERNAL_READ_PNG(filename, red, green, blue, order=order, transparent=transparent) end ; ; ----------------------------- Function ------------------------ ; -function READ_PNG, filename, red, green, blue, $ - order=order, transparent=transparent, $ - help=help, test=test, verbose=verbose -; - +function READ_PNG, filename, red, green, blue,order=order, transparent=transparent, verbose=verbose compile_opt hidden, idl2 - ON_ERROR, 2 -; -if KEYWORD_SET(help) then begin - print, 'function READ_PNG, filename, red, green, blue, $' - print, ' order=order, transparent=transparent, $' - print, ' help=help, test=test, verbose=verbose' - return, -1 -endif -; -image=INTERNAL_READ_PNG(filename, red, green, blue, $ - order=order, transparent=transparent, $ - test=test, verbose=verbose) -; -return, image -; +return,INTERNAL_READ_PNG(filename, red, green, blue, order=order, transparent=transparent) end diff --git a/src/pro/read_ppm.pro b/src/pro/read_ppm.pro new file mode 100644 index 000000000..4adc2bf4d --- /dev/null +++ b/src/pro/read_ppm.pro @@ -0,0 +1,53 @@ +;+ +; +; NAME: READ_PPM +; +; PURPOSE: Reads a PPM or PGM file into memory +; +; CATEGORY: Images (IO) +; +; CALLING SEQUENCE: +; READ_PPM, filename, image [,maxval=maxval] +; +; KEYWORD PARAMETERS: +; MAXVAL: A variable to return the MAX value in the image. +; +; OUTPUTS: always a true color image, so data is a three dimensional array +; with pixel interleaving. +; +; OPTIONAL OUTPUTS: +; +; RESTRICTIONS: +; Requires ImageMagick (tests added) +; +; PROCEDURE: +; Use ImageMagick to read the data as requested +; +; EXAMPLE: +; See "test_read_standard_images.pro" in testsuite/ +; +; MODIFICATION HISTORY: +; Quickly hacked from READ_PNG: James Tappin. 28/9/21. +; +;- +; LICENCE: +; Copyright (C) 2004, 2011, 2012, 2014 +; This program is free software; you can redistribute it and/or modify +; it under the terms of the GNU General Public License as published by +; the Free Software Foundation; either version 2 of the License, or +; (at your option) any later version. +; +;- +; +; ----------------------------- Procedure ------------------------ +; +pro READ_PPM, filename, image, maxval = maxval +; + +compile_opt hidden, idl2 +ON_ERROR, 2 + +if ~MAGICK_PING(filename, 'PPM') && ~MAGICK_PING(filename, 'PGM') then MESSAGE, "File "+filename+" is not a PGM/PPM file." +READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable, /order ; IDL flips, go figure +maxval = max(image) +end diff --git a/src/pro/read_xwd.pro b/src/pro/read_xwd.pro index 1201a11cd..ab72a9a3b 100644 --- a/src/pro/read_xwd.pro +++ b/src/pro/read_xwd.pro @@ -67,21 +67,12 @@ if (N_ELEMENTS(filename) GT 1) then MESSAGE, "Only one file at once !" if (STRLEN(filename) EQ 0) then MESSAGE, "Null filename not allowed." if ((FILE_INFO(filename)).exists EQ 0) then MESSAGE, "Error opening file. File: "+filename if (FILE_TEST(filename, /regular) EQ 0) then MESSAGE, "Not a regular File: "+filename -; -; starting effective reading ! -; -mid=MAGICK_OPEN(filename) -; -print, magick_IndexedColor(mid) -if (MAGICK_INDEXEDCOLOR(mid)) then begin - image=MAGICK_READINDEXES(mid) - MAGICK_READCOLORMAPRGB, mid, red, green, blue -endif else begin - image=MAGICK_READ(mid) -endelse -; -MAGICK_CLOSE, mid -; +READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable +if ( n_elements(colortable) gt 0 ) then begin + red=colortable[*,0] + green=colortable[*,1] + blue=colortable[*,2] +endif return, image ; end diff --git a/src/pro/utilities/read_anygraphicsfilewithmagick.pro b/src/pro/utilities/read_anygraphicsfilewithmagick.pro index 7894017fe..9c1abff0e 100644 --- a/src/pro/utilities/read_anygraphicsfilewithmagick.pro +++ b/src/pro/utilities/read_anygraphicsfilewithmagick.pro @@ -51,12 +51,10 @@ ;- ; -pro READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable, $ - colors=ncolors, dither=dither, grayscale=grayscale, order=order, $ - true=true +pro READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable, colors=ncolors, dither=dither, grayscale=grayscale,background_color=bgc, order=order, true=true compile_opt hidden, idl2 - + ON_ERROR, 2 CATCH, Error_status IF Error_status NE 0 THEN BEGIN @@ -65,51 +63,40 @@ pro READ_ANYGRAPHICSFILEWITHMAGICK, filename, image, colortable, $ RETALL ENDIF ; -mid=MAGICK_OPEN(filename) + mid=MAGICK_OPEN(filename) + + if (keyword_set(order)) then magick_flip,mid -; to get something in colortable, colors need to be defined -if (KEYWORD_SET(ncolors)) then begin - ncolors = ncolors < 252 > 1 ; > 8 - if (~KEYWORD_SET(grayscale)) then ncolors = ncolors < 32 > 1 ; to be refined. not clear. - MAGICK_QUANTIZE, mid, ncolors, grayscale=grayscale, dither=dither -endif else if (KEYWORD_SET(grayscale)) then begin - MAGICK_QUANTIZE, mid, /GRAYSCALE, dither=dither -endif -image=MAGICK_READ(mid); [3,n,m] -indexed=(magick_colormapsize(mid) gt 0) ; not the case with -;;flip if order is set -if (KEYWORD_SET(order)) then MAGICK_FLIP, mid -; -if (indexed) then begin - MAGICK_READCOLORMAPRGB,mid,r,g,b - colortable=[[r],[g],[b]] - ; apply palette compression, using r - index=intarr(256) - for i=0,n_elements(r)-1 do index[r[i]]=i - image=reform(image[0,*,*]) - image[*]=index[image[*]] -endif + if (KEYWORD_SET(ncolors)) then begin + if (KEYWORD_SET(grayscale)) then ncolors = ncolors < 256 > 1 else ncolors = ncolors < 252 > 1 ; 252 IDL default + MAGICK_QUANTIZE, mid, ncolors, grayscale=grayscale, dither=dither + endif else if (KEYWORD_SET(grayscale)) then begin + MAGICK_QUANTIZE, mid, /GRAYSCALE, dither=dither + endif + indexed=(magick_class(mid) eq "PseudoClass") ; new command + if (indexed) then begin + image=MAGICK_READINDEXES(mid) ; may be [2,n,m] --> unsupported by IDL + if ((size(image))[0] eq 3) then image=reform(image[0,*,*]) ; get only index image, not alpha channel + MAGICK_READCOLORMAPRGB,mid,r,g,b,background_color=bgc + colortable=[[r],[g],[b]] + endif else begin + image=MAGICK_READ(mid) ; just read! [3,n,m] or [4,n,m] + endelse + ; no more use of MAGICK? close it: -MAGICK_CLOSE,mid - -; if 16-bit (unsigned short int) image convert to byte -sz = SIZE(image) -type = sz[sz[0]+1] -if ((type EQ 2) OR (type EQ 12)) then begin - print, 'Converting 16-bit image to byte' - image = image / 256 - image = BYTE(image) -endif - -if (sz[0] EQ 3) then begin - if KEYWORD_SET(TRUE) then begin - if (TRUE eq 1) then t=[0,1,2] - if (TRUE eq 2) then t=[1,0,2] - if (TRUE eq 3) then t=[1,2,0] - ;; - image=TRANSPOSE(image, t) - endif -endif + MAGICK_CLOSE,mid + + sz = SIZE(image) + + if (sz[0] EQ 3) then begin + if KEYWORD_SET(TRUE) then begin + if (TRUE eq 1) then t=[0,1,2] + if (TRUE eq 2) then t=[1,0,2] + if (TRUE eq 3) then t=[1,2,0] + ;; + image=TRANSPOSE(image, t) + endif + endif ; end diff --git a/src/pro/write_ppm.pro b/src/pro/write_ppm.pro new file mode 100644 index 000000000..4721e12af --- /dev/null +++ b/src/pro/write_ppm.pro @@ -0,0 +1,55 @@ +;+ +; WRITE_PPM +; Write a PPM or PGM image file. +; +; Usage: +; write_ppm, file, image +; +; Arguments: +; file string The file to write +; image byte The image to write, must be n×m (PGM) or 3×n×m +; (PPM) +; +; Notes: +; The ASCII keyword is not (yet) implemented. +; +; History: +; Original (using write_jpeg as a template); 28/9/21; SJT +;- + +pro write_ppm, file, image, help = help + +; +; this line allows to compile also in IDL ... + forward_function magick_exists, magick_ping, magick_read +; + on_error, 2 + + if keyword_set(help) then begin + print, 'pro write_ppm, filename, image' + return + endif + +; Do we have access to ImageMagick functionnalities ?? + + if ~magick_exists() then begin + message, /continue, "GDL was compiled without ImageMagick support." + message, "You must have ImageMagick support to use this functionaly." + endif + + sz = size(image) + if sz[0] eq 2 then begin ; PGM + mid = magick_create(sz[1], sz[2]) + cmap = indgen(256) + magick_writecolortable, mid, cmap, cmap, cmap + magick_write, mid, reverse(image, 2) + magick_writefile, mid, file, 'PGM' + endif else if sz[0] eq 3 && sz[1] eq 3 then begin ; PPM + mid = magick_create(sz[2], sz[3]) + magick_write, mid, reverse(image, 3), rgb = 1s + magick_writefile, mid, file, 'PPM' + endif else message, "IMAGE must be n×m (PGM) or 3×n×m (PPM)" + + magick_close, mid + +end