Kernels from text files

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Kernels from text files

Post by snibgo »

I would like to create and process morphology kernels just like any other image. Having to specify them as text is clumsy. That would need a major change.

As a simpler partial solution, I propose that kernels should be readable from text files. FileToString() seems suitable for this. The best place to put this seems to be AcquireKernelInfo() in morphology.c.

The following diff (from v6.8.9-6) makes kernels readable from text files.

Code: Select all

--- /home/Alan/imdevsrc/../ImageMagick-6.8.9-6/magick/morphology.c	2014-04-18 02:27:41.000000000 +0100
+++ /home/Alan/imdevsrc/magick/morphology.c	2014-10-30 19:30:37.348558500 +0000
@@ -501,9 +501,30 @@
   const char
     *p;
 
+  char
+    *expanded;
+
+  ExceptionInfo 
+    exception;
+
+
   if (kernel_string == (const char *) NULL)
     return(ParseKernelArray(kernel_string));
-  p=kernel_string;
+
+  expanded = NULL;
+
+  if (*kernel_string == '@') {
+    printf ("expanding file [%s]\n", kernel_string+1);
+    expanded=FileToString(kernel_string+1,~0UL,&exception);
+    if (expanded == (char *) NULL) {
+      printf ("FileToString failed");
+      return((KernelInfo *) NULL);
+    }
+    printf ("Expanded to [%s]\n", expanded);
+    p=expanded;
+  } else {
+    p=kernel_string;
+  }
   kernel=NULL;
 
   while (GetMagickToken(p,NULL,token), *token != '\0')
@@ -538,6 +559,10 @@
       break;
     p++;
   }
+
+  if (expanded != NULL)
+    expanded=DestroyString(expanded);
+
   return(kernel);
 }
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Kernels from text files

Post by fmw42 »

That is a good idea. I concur.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Kernels from text files

Post by magick »

Look for a patch in ImageMagick 6.8.9-10 Beta by sometime tomorrow.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Kernels from text files

Post by snibgo »

Thanks.

The diff contains debugging printf statements. I meant to remove them before posting here, but somehow forgot. Sorry about that.
snibgo's IM pages: im.snibgo.com
Post Reply