Memory allocation error [ SOLVED ]

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
kirash4
Posts: 34
Joined: 2010-04-12T15:30:59-07:00
Authentication code: 8675308

Memory allocation error [ SOLVED ]

Post by kirash4 »

I have a c++ program that processes an image and does several things with it, in the following order:
- if needed, resizes or extents it to 48 pixels high
- flattens it if it has a matte
- creates a copy of the resized image so I have two variables to work with, a thumbnail_Img and process_Img
- writes the thumbnail_Img to disk
- reads the process_Img pixel by pixel and writes out a binary file containing individual RGB values for each pixel
- creates an Arc image from process_Img
- writes that Arc image to disk

Now, when I run this against a bunch of images in a folder, it works fine if I do each image individually, so in bash if I do something like this, it works great:

Code: Select all

$ for file in * ; do ./getpx $file ; done
Processing      001.png :  RESIZING: 2880 x 1800 ->  77 x 48 +MTLW+ 6 | Amps: 0.23 (side),  0.91 (total)
Processing      002.jpg :  RESIZING:  400 x  400 ->  48 x 48 -MTLW+ 8 | Amps: 0.62 (side),  2.50 (total)
Processing      003.jpg :  RESIZING: 1280 x  960 ->  64 x 48 -MTLW+ 8 | Amps: 0.28 (side),  1.13 (total)
Processing bluediag.gif :  RESIZING:  400 x  160 -> 120 x 48 -MTLW+ 4 | Amps: 1.11 (side),  4.43 (total)
Processing     cc02.bmp :        OK!                 44 x 48 -MTLW+10 | Amps: 1.06 (side),  4.25 (total)
Processing  celtic1.png :  RESIZING:  600 x  558 ->  52 x 48 +MTLW+ 8 | Amps: 0.64 (side),  2.57 (total)
Processing   celtic.bmp :        OK!                102 x 48 -MTLW+ 4 | Amps: 1.07 (side),  4.30 (total)
However, if I were to run it as a wildcard, it fails:

Code: Select all

$ ./getpx *
Processing      001.png :  RESIZING: 2880 x 1800 ->  77 x 48 +MTLW+ 6 | Amps: 0.23 (side),  0.91 (total)
Processing      002.jpg :  RESIZING:  400 x  400 ->  48 x 48 +MCaught Magick++ exception: getpx: memory allocation failed `002-thumb.png' @ error/png.c/WriteOnePNGImage/8866
Processing      003.jpg :  RESIZING: 1280 x  960 ->  64 x 48 +MCaught Magick++ exception: getpx: memory allocation failed `003-thumb.png' @ error/png.c/WriteOnePNGImage/8866
Processing     cc02.bmp :        OK!                 44 x 48 +MCaught Magick++ exception: getpx: memory allocation failed `cc02-thumb.png' @ error/png.c/WriteOnePNGImage/8866
Processing bluediag.gif :  RESIZING:  400 x  160 -> 120 x 48 +MCaught Magick++ exception: getpx: memory allocation failed `bluediag-thumb.png' @ error/png.c/WriteOnePNGImage/8866
Processing  celtic1.png :  RESIZING:  600 x  558 ->  52 x 48 +MTLW+ 8 | Amps: 2.31 (side),  9.25 (total)
Processing   celtic.bmp :        OK!                102 x 48 +MCaught Magick++ exception: getpx: memory allocation failed `celtic-thumb.png' @ error/png.c/WriteOnePNGImage/8866
But notice how in the middle of the failures, one of them processed correctly (one before last.) I also notice that it's always failing during the thumbnail writing process. So this makes me wonder if there's something I need to be clearing, some memory space, or variable ... Can anyone suggest what I should start looking for, possibly changing, or adding test code to figure out where or what's causing the failure?
Last edited by kirash4 on 2014-08-26T11:58:52-07:00, edited 1 time in total.
kirash4
Posts: 34
Joined: 2010-04-12T15:30:59-07:00
Authentication code: 8675308

Re: Memory allocation error

Post by kirash4 »

Never mind! Figured it out. I forgot to reset a variable which was causing the routine that checks for a matte to fail, which in turn caused the thumbnail writing routine to blow up. And the way I figured it out was by the feedback the program was giving me. Those letters I had placed as indicator:

Code: Select all

+MTLW+ 6
 ||||
 |||+--- how many times the image repeats when Arc-ed
 ||+---- pixel data written
 |+----- thumbnail written
 +------ +M = image has a matte, -M image does not
Notice how on the individual run, the files are getting identified properly, however in the wildcard run, all of them were coming up as if they have a matte. The failing ones did not have a matte and the files that did process correctly did. Chalk that one up to forethought and adding some sort of debugging feedback.
Post Reply