Skip to content

Commit 03811ea

Browse files
author
Cristy
committed
Framework for supporting the -region option
1 parent d4feacc commit 03811ea

File tree

3 files changed

+148
-13
lines changed

3 files changed

+148
-13
lines changed

MagickCore/image.c

Lines changed: 128 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,84 @@ MagickExport void SetImageInfoFile(ImageInfo *image_info,FILE *file)
29082908
% %
29092909
% %
29102910
% %
2911+
% S e t I m a g e A l p h a %
2912+
% %
2913+
% %
2914+
% %
2915+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2916+
%
2917+
% SetImageAlpha() sets the alpha levels of the image.
2918+
%
2919+
% The format of the SetImageAlpha method is:
2920+
%
2921+
% MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
2922+
% ExceptionInfo *exception)
2923+
%
2924+
% A description of each parameter follows:
2925+
%
2926+
% o image: the image.
2927+
%
2928+
% o Alpha: the level of transparency: 0 is fully opaque and QuantumRange is
2929+
% fully transparent.
2930+
%
2931+
*/
2932+
MagickExport MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
2933+
ExceptionInfo *exception)
2934+
{
2935+
CacheView
2936+
*image_view;
2937+
2938+
MagickBooleanType
2939+
status;
2940+
2941+
ssize_t
2942+
y;
2943+
2944+
assert(image != (Image *) NULL);
2945+
if (image->debug != MagickFalse)
2946+
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2947+
assert(image->signature == MagickCoreSignature);
2948+
image->alpha_trait=BlendPixelTrait;
2949+
status=MagickTrue;
2950+
image_view=AcquireAuthenticCacheView(image,exception);
2951+
#if defined(MAGICKCORE_OPENMP_SUPPORT)
2952+
#pragma omp parallel for schedule(static,4) shared(status) \
2953+
magick_threads(image,image,image->rows,1)
2954+
#endif
2955+
for (y=0; y < (ssize_t) image->rows; y++)
2956+
{
2957+
register Quantum
2958+
*magick_restrict q;
2959+
2960+
register ssize_t
2961+
x;
2962+
2963+
if (status == MagickFalse)
2964+
continue;
2965+
q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2966+
if (q == (Quantum *) NULL)
2967+
{
2968+
status=MagickFalse;
2969+
continue;
2970+
}
2971+
for (x=0; x < (ssize_t) image->columns; x++)
2972+
{
2973+
if (GetPixelReadMask(image,q) != 0)
2974+
SetPixelAlpha(image,alpha,q);
2975+
q+=GetPixelChannels(image);
2976+
}
2977+
if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2978+
status=MagickFalse;
2979+
}
2980+
image_view=DestroyCacheView(image_view);
2981+
return(status);
2982+
}
2983+
2984+
/*
2985+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2986+
% %
2987+
% %
2988+
% %
29112989
% S e t I m a g e M a s k %
29122990
% %
29132991
% %
@@ -3031,29 +3109,33 @@ MagickExport MagickBooleanType SetImageMask(Image *image,const PixelMask type,
30313109
% %
30323110
% %
30333111
% %
3034-
% S e t I m a g e A l p h a %
3112+
% S e t I m a g e R e g i o n M a s k %
30353113
% %
30363114
% %
30373115
% %
30383116
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30393117
%
3040-
% SetImageAlpha() sets the alpha levels of the image.
3118+
% SetImageRegionMask() associates a mask as defined by a region with the
3119+
% image.
30413120
%
3042-
% The format of the SetImageAlpha method is:
3121+
% The format of the SetImageRegionMask method is:
30433122
%
3044-
% MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
3045-
% ExceptionInfo *exception)
3123+
% MagickBooleanType SetImageRegionMask(Image *image,const PixelMask type,
3124+
% const RectangleInfo *region,ExceptionInfo *exception)
30463125
%
30473126
% A description of each parameter follows:
30483127
%
30493128
% o image: the image.
30503129
%
3051-
% o Alpha: the level of transparency: 0 is fully opaque and QuantumRange is
3052-
% fully transparent.
3130+
% o type: the mask type, ReadPixelMask or WritePixelMask.
3131+
%
3132+
% o geometry: the mask region.
3133+
%
3134+
% o exception: return any errors or warnings in this structure.
30533135
%
30543136
*/
3055-
MagickExport MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
3056-
ExceptionInfo *exception)
3137+
MagickExport MagickBooleanType SetImageRegionMask(Image *image,
3138+
const PixelMask type,const RectangleInfo *region,ExceptionInfo *exception)
30573139
{
30583140
CacheView
30593141
*image_view;
@@ -3064,16 +3146,34 @@ MagickExport MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
30643146
ssize_t
30653147
y;
30663148

3149+
/*
3150+
Set image mask as defined by the region.
3151+
*/
30673152
assert(image != (Image *) NULL);
30683153
if (image->debug != MagickFalse)
30693154
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
30703155
assert(image->signature == MagickCoreSignature);
3071-
image->alpha_trait=BlendPixelTrait;
3156+
if (region == (const RectangleInfo *) NULL)
3157+
{
3158+
switch (type)
3159+
{
3160+
case WritePixelMask: image->write_mask=MagickFalse; break;
3161+
default: image->read_mask=MagickFalse; break;
3162+
}
3163+
return(SyncImagePixelCache(image,exception));
3164+
}
3165+
switch (type)
3166+
{
3167+
case WritePixelMask: image->write_mask=MagickTrue; break;
3168+
default: image->read_mask=MagickTrue; break;
3169+
}
3170+
if (SyncImagePixelCache(image,exception) == MagickFalse)
3171+
return(MagickFalse);
30723172
status=MagickTrue;
30733173
image_view=AcquireAuthenticCacheView(image,exception);
30743174
#if defined(MAGICKCORE_OPENMP_SUPPORT)
30753175
#pragma omp parallel for schedule(static,4) shared(status) \
3076-
magick_threads(image,image,image->rows,1)
3176+
magick_threads(image,image,1,1)
30773177
#endif
30783178
for (y=0; y < (ssize_t) image->rows; y++)
30793179
{
@@ -3093,8 +3193,23 @@ MagickExport MagickBooleanType SetImageAlpha(Image *image,const Quantum alpha,
30933193
}
30943194
for (x=0; x < (ssize_t) image->columns; x++)
30953195
{
3096-
if (GetPixelReadMask(image,q) != 0)
3097-
SetPixelAlpha(image,alpha,q);
3196+
MagickRealType
3197+
intensity;
3198+
3199+
intensity=0;
3200+
switch (type)
3201+
{
3202+
case WritePixelMask:
3203+
{
3204+
SetPixelWriteMask(image,ClampToQuantum(QuantumRange-intensity),q);
3205+
break;
3206+
}
3207+
default:
3208+
{
3209+
SetPixelReadMask(image,ClampToQuantum(QuantumRange-intensity),q);
3210+
break;
3211+
}
3212+
}
30983213
q+=GetPixelChannels(image);
30993214
}
31003215
if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)

MagickCore/image.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ extern MagickExport MagickBooleanType
536536
SetImageExtent(Image *,const size_t,const size_t,ExceptionInfo *),
537537
SetImageInfo(ImageInfo *,const unsigned int,ExceptionInfo *),
538538
SetImageMask(Image *,const PixelMask type,const Image *,ExceptionInfo *),
539+
SetImageRegionMask(Image *,const PixelMask type,const RectangleInfo *,
540+
ExceptionInfo *),
539541
SetImageStorageClass(Image *,const ClassType,ExceptionInfo *),
540542
StripImage(Image *,ExceptionInfo *),
541543
SyncImage(Image *,ExceptionInfo *),

MagickWand/convert.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,15 @@ WandExport MagickBooleanType ConvertImageCommand(ImageInfo *image_info,
25102510
ThrowConvertInvalidArgumentException(option,argv[i]);
25112511
break;
25122512
}
2513+
if (LocaleCompare("read-mask",option+1) == 0)
2514+
{
2515+
if (*option == '+')
2516+
break;
2517+
i++;
2518+
if (i == (ssize_t) argc)
2519+
ThrowConvertException(OptionError,"MissingArgument",option);
2520+
break;
2521+
}
25132522
if (LocaleCompare("red-primary",option+1) == 0)
25142523
{
25152524
if (*option == '+')
@@ -3214,6 +3223,15 @@ WandExport MagickBooleanType ConvertImageCommand(ImageInfo *image_info,
32143223
ThrowConvertException(OptionError,"MissingArgument",option);
32153224
break;
32163225
}
3226+
if (LocaleCompare("write-mask",option+1) == 0)
3227+
{
3228+
if (*option == '+')
3229+
break;
3230+
i++;
3231+
if (i == (ssize_t) argc)
3232+
ThrowConvertException(OptionError,"MissingArgument",option);
3233+
break;
3234+
}
32173235
ThrowConvertException(OptionError,"UnrecognizedOption",option)
32183236
}
32193237
case '?':

0 commit comments

Comments
 (0)