G'MIC exercises

Something like : rep_sort_images_by_order[0--1] A,C,Z,B,X,T that would sort images in the particular order you want instead of using mv?

It’s only partly related. The difference is that sort_list sorts by lexicographic order, and rep_sort_images_by_order sorts images so that it matches the input order.

It is optimized though a C++ solution would be faster, I just want to extend it.

Yes, something like that. Also, it use mv inside. See the more formatted code:

#@cli rep_sort_images_by_order: number_a,number_b,....
#@cli : Sort images accordingly to numerical arguments. The number of numerical arguments has to be equal to the number of images.
#@cli : $ sample cat,dog,lena,david,house,flower,waterfall,tiger name {expr('x',$!)} rep_sort_images_by_order 5,1,7,0,4,2,3,6
rep_sort_images_by_order:
  if $#!=$! error invalid_image_count fi

  input_v=[{[$*]%$#}]
  sorted_v=[{sort([expr('x',$#)])}]

  if $input_v==$sorted_v return
  elif $input_v==reverse($sorted_v) reverse
  else
     $#,1,1,1

     $#,1,1,1,>"begin(
        const max_index=w-1;
        index_counter=vector(#w,0);
        order=[$*];
      );
      value_index=order[x]%w;
      if(++index_counter[value_index]>1,run('error dup_ind_inp'););
      off_position=value_index-i(#-1,value_index);
      polygon(#-1,2,value_index,0,max_index,0,-1,1);
      off_position+x;
      "

     remove[-2]

     repeat $# {
       move[{i(#-1,$>)}] $>
     }

     remove[-1]

  fi

Hmm, sorted_v doesn’t need sort(), I will fix that.

How do I get this to work?

$  pal 10,50 move[\"Zenit-241 by Zenit40\"] 1

I would like to move images with spaces in its name. This name can be any arbitrary name. And this is why my current script to order images by user input set of names doesn’t work.

Have you tried this:

  1. storing the name in a var
  2. giving a temp name to the image
  3. moving image with temp name
  4. renaming image with $var

gmic run 'pal 10,50 sp d temp={0,n} nm[0] what d move[what] -1 nm[what] $temp '

For some reason you have to use move[img] 2 to make it move to 1 if you only have 2 images in your list (may as well just use reverse in that case)

1 Like

That would be my first instinct. mv has that one quirk where a certain position cannot be moved (details can be found elsewhere). FYI, higher or lower indexes just wrap around.
Goes back to work :dotted_line_face:

Indeed, selection can contain only numbers and image names, but only when they have a name compatible with a variable name.
To manage any name, use command named:

~$ gmic h named    

  named (+):
      _mode,"name1","name2",...

    Return the set of indices corresponding to images of the selection with specified names.
    After this command returns, the status contains a list of indices (unsigned integers),
    separated by commas (or an empty string if no images with those names have been found).
    (equivalent to shortcut command 'nmd').

    'mode' can be { 0=all indices (default) | 1=lowest index | 2=highest index | 3 = all indices (case insensitive) | 4 = lowest index (case insensitive) 
    | 5 = highest index (case insensitive)}                                                                                                                     

So, this work:

foo :
  sp portrait1,portrait2 => "Mr Brownstone","Mr Tambourine Man"
  named "Mr Tambourine Man" 
  mv[${}] 0
2 Likes

Imagine if a variable contains nothing. How would I force a loop to continue if a variable has been changed to NULL instead of a error?

For context:

repeat $# {
 m=${var{$>+1}}
 
 if isnum($m)
  if isint($m)
   m=${arg\ 1+$m,bw,rgb,b_rgb,bw_rgb,cmy,cmyk,wcmyk,rgbcmy,1bitrgb,aurora,zenit_241,gbg,duel,hocuspocus,playpal,srb2,uzebox,kens16,kens32,kens54,aap12,aap16,aap64,aap96,aap128,aap_dga16,cheerful32,db8,db16,db32,db_iso22,dimwid17,dimwid23,edg4,edg8,edg16,edg32,edg36,edg64,famicube,juicy32,juicy56,xaiue,15pdx,20pdx,24pdx,cabana64,fant16,fant24,tf23,tfp39,faraway48,fleja_m,koni32,linearbasic,lego,lego2021,vinelinear,arcade29,arq16,atom8,blk36,blkneo,brokefac,bubblegum,cpcboy,cade15,calder8,cdbac,cgarne,dino,4l,fzteth16,indc,island16,journey,shallowmarsh26,lago_nenufar,juicy,chocolateganache,brightwinter1,brightwinter2,kawaii,xdb_01,gzxp,chrom16,piet_code,boltbait_matrix,material_design,thehamster_rainbow,boltbait_rainbow,scrj36,pxls_default,lospec,moderna,oak,nature55,rbypgo,new_worlds,nauris16,dynamite,interstate_28,downgraded_32,pear36,pineapple32,peach,resurrect,rosy42,slso,softy15,spec12,roarin80s,starmancer,superfuture,sunshine_35,sweetie16,calm48,optimism,taffy16,todayland,trirampo,reddit_place_2022,tropical_cone_24,vivid17,shido76,intacto,itatsi,enos16,grixel_grotto,sup8,undertones,tango,cheese,equpix15,zu32,voodo,franzston_30,night16,star29,star34,stilllife,simjpc16,acid15,battery24,clump18,cthul,crimso11,coptec,drz15,eggy15,europa,greyt,jewel,polar11,sheltzy,wyrm,yume_nikki,rube_goldberg,boomboom,g8,crayola,funkyfuture8,golden_wall,matriax8c,nt1h,jerrypie22,nineties_nine,on70,anb16,retrocal,punolit,luap_40,autumvil6,au15,au15y,galaxy_flame,cretaceous16,antiquity16,mushroom,aerugo,hotel_paintings,nopal,gpy,blessing,fairydust_8,milkshake,fuzz4,fairy,naji16,easter,pastel_and_darks58,pastel17,nostalgia15,ocaso,pastel,pollen8,kule,hydrangea,fluffy8,st8rb,neon_space,cyclope6,sy17,syz15,tui15,cave,psygnosia,marshmellow32,lost_century,finlal11,industrial_factory,murder_mystery,fatedestiny,vinik24,ykb22,halloween,graveyard,steamlords,frostical,deuterospill,cool_bone_7,muted_ally_6,ephemera,ink,violet_dreams,tinyfolks,old_gold_7,rosemoss,aaprad,aapmaj,dead_weight_8,mojave,pet8,pet8d,xaiue_rad,daruda,firestorm,borkfest,spicy_07,rust6,apricot,supernova7,pastry,sandy_06,illumination,nyx8,dreamhaze,oil6,regal,softdemon,sgm,midnight_ablaze,black_cherry,sunset_red,inkpink,brash_pink,pink_neon_sign_6,enchanted_purple,arch,spaceyeaster,fornaxvoid1,fornaxvoid2,pixelwave,s1_6,berry_nebula,abyss9,moonlight,moon39,h2o,magic_waters_9,bluemold,moss,deep_maze,toxic_slime,lush_green,tsunami,cryptic_ocean,marsh_madness,oxyd,pinkgreen,walking_in_the_woods,paper_8,sahara_pastell,sunflower_painting_7,arthoe7,sky5,ocean_glass,royalguard,eulbink,winter_wonderland,moon_squid,stratus,arctic_dust,clouds_sunset,lilac_skies,sea_of_fire,autochrome3,autochrome5,gb_d_1,gb_d_2,gb_andrade,gb_blue,gb_bz,gb_suburb,gb_crimson,gb_didi,gb_dirty,gb_arne,gb_easy,gb_forest,gb_hg,gb_lg,gb_nostalgia,gb_platinum,gb_kirokaze,gb_cyber,gb_wish,gb_grapefruit,gb_ice_cream,gb_rb,gb_gold,gb_choco,gb_gray,gb_space,gb_purpdawn,moon_crystal,arne4,autumn_chill,cherrymelon,hal4,hollow,lavender4,maw,voltage_warning,tritanopia,rabbit7,amiga2600ntsc,amiga2600pal,amiga2600secam,amiga7800mess,amiga7800,amstrad_cpc,apple2,atari8bit,cga,cga00,cga01,cga10,cga11,cga20,cga21,c64_pepto,c64_colodore,com_vic_20,colecovision,jmp,mac2,mac8,msx,nes,pico_8,risc,samcoupe,mo5,trs80,virtualboy,vga,win95,zx,gnome32,elc22,chip16,deluxepaint,flat_ui,makecode_arcade,oekaki,lms,msxp,vis,piet,japwood,humanfaces,madjikhsv,st24_christmas,ladybugreds,pastel_rainbow}
  fi
 fi
 
 pal_name=${n_$m} v + e[^-1] $>:$pal_name v -
 
 if !narg("$pal_name") continue fi
 
 ${p_$m} => $pal_name
 
 if $> out.=,$m
 else out=$m
 fi
 
 p_$m,n_$m=
}

As you can see, p_$m,n_$m= puts them into NULL. And pal_name would be empty if there is a duplicate. And echo shows it’s empty. And yet it bypass the if !narg…

See here to see what I mean:

C:\Windows\System32>gmic +new_pal bw,rgb,rgb
[gmic]./ Start G'MIC interpreter (v.3.3.0).
[gmic]./+new_pal/+_new_pal/ 0:Black & White
[gmic]./+new_pal/+_new_pal/ 0:pass
[gmic]./+new_pal/+_new_pal/ 1:Red-Green-Blue
[gmic]./+new_pal/+_new_pal/ 1:pass
[gmic]./+new_pal/+_new_pal/ 2:
[gmic]./+new_pal/+_new_pal/ 2:pass
[gmic] *** Error in ./+new_pal/+_new_pal/*repeat#427/ (file 'C:\Users\User\user.gmic', line #442) *** Unknown command or filename ''; did you mean '%'?

pass means it passed the if !narg.

EDIT: Answer was to do p_$m,n_$m=continue,. This way, it will continue instead of creating new palette.

Any idea what I can do about a single line of code that is has over 160,000 characters? For example, line 14927 in reptorian.gmic within github/gmic-community? There’s probably nothing that can done, I think.

Just use \ to specify several lines as a single one:

var="this is a very long line but I can use the backslash to ensure "\
    "I can define a very long line by using in fact multiple lines. This is "\
    "actually very convenient!"

In gmic_stdlib.gmic, there are no line longer than 120 chars.

I find that too hard to set up with the 100000+ chars line. I’m thinking about making a new command which convert a single value of a high base into a color. That way, I can reduce the numbers of characters significantly. I’m thinking of 16,777,216 base 10 => ? base that is greater than 10. Maybe, I’ll leave it to be.

EDIT:

I figured it out. Not sure if I broke the latest update though.

Why won’t this work? I’m treating values_of_bins vector as if it was a image.

rep_test_script:
eval "
 ref_bin='1111'-_'0';
 values_of_bins=expr('sum([x,y,z,c]<<[3,2,1,0])',2,2,2,2);
 print(i(values_of_bins,ref_bin));
 "

Should print 15 because 1111 from bin2dec is 15.

The purpose: This is used to develop a double dabble approach to converting really large binary and print the decimal value and vice versa.

I will use this instead:

#@cli rep_bin2dec_dd:
#@cli : Convert binary numbers to digit representation using the double dabble method. Large binary number is supported here.
rep_bin2dec_dd:
   $=arg

   2,2,2,2,sum([x,y,z,c]<<[3,2,1,0]) => bin_box

   repeat $# {
       p:=$>+1
       ('${arg$p}')
       if im<_'0'||iM>_'1' error inv_char_det fi
       -. {'0'}
   
       eval "
           const num_of_bins_set=(w#-1>>2)+((w#-1&3)?1);
           bin_sets=vector(#num_of_bins_set);
           index=w#-1-4;
           for(ins_pos=num_of_bins_set-1,ins_pos>-1,--ins_pos,
               bin_sets[ins_pos]=i(#$bin_box,crop(#-1,index,0,0,0,4,1,1,1));
               index-=4;
           );
           bin_sets;"
           
       echo ${} rm.
   }

   rm.

@David_Tschumperle

What does _M, and _max_M in draw(_#ind,S,_x,_y,_z,_c,_dx,_dy,_dz,_dc,_opacity,_M,_max_M) mean? I’m thinking they’re to cut values?

Not at all,

M is actually a vector that is the mask used for the drawing.
_max_M is the value you consider of being maximai opacity (typically 1 (default) or 255).

Could _M be changed into _vector_mask, and could _max_M could be _max_mask_value? I feel that’s easier to understand as I remember I use those two before, but as you can see, I forgot what they are.

If we take the help of the corresponding command image:

$ gmic h image

  image (+):
      [sprite],_x[%|~],_y[%|~],_z[%|~],_c[%|~],_opacity,_[opacity_mask],_max_opacity_mask

    Draw specified sprite on selected images.
    (equivalent to shortcut command 'j').

    If one of the x,y,z or c argument ends with a '~', its value is expected to be
    a centering ratio (in [0,1]) rather than a position.
    Usual centering ratio are { 0:left-justified | 0.5:centered | 1:right-justified }.

    Default values: 'x=y=z=c=0', 'opacity=1', 'opacity_mask=(undefined)' and 'max_opacity_mask=1'.

I would rather change them to opacity_mask and max_opacity_mask.

2 Likes

What does this mean?

You can also prepend a hyphen - to a feature (that supports it) to flush the corresponding event immediately after reading its state (works for keys, mouse and window events).

Source: G'MIC - GREYC's Magic for Image Computing: A Full-Featured Open-Source Framework for Image Processing - Substitution Rules

Another question:

Let’s say I want a string without a substring in it. How do I do it?

var1=part_test
var2=??? # Return _test using $var1

Right now, I have this code:

_$0_list_of_blending_modes=_$0_add,_$0_alpha,_$0_and,_$0_average,_$0_burn,_$0_darken,_$0_difference,_$0_divide,_$0_dodge,_$0_exclusion,_$0_freeze,_$0_grainextract,_$0_grainmerge,_$0_hardlight,_$0_hardmix,_$0_interpolation,_$0_lighten,_$0_linearburn,_$0_linearlight,_$0_multiply,_$0_normal,_$0_negation,_$0_or,_$0_overlay,_$0_pinlight,_$0_reflect,_$0_screen,_$0_shapeaverage,_$0_softburn,_$0_softdodge,_$0_softlight,_$0_stamp,_$0_subtract,_$0_vividlight,_$0_xor
num_of_blending_modes={narg(${_$0_list_of_blending_modes})}
${_$0_list_of_blending_modes}={expr('x',$num_of_blending_modes)}

And I want to remove _$0_ within all variables of _$0_list_of_blending_modes without changing the above code.

EDIT:

My answer seem to be adding these two lines:

('${_$0_list_of_blending_modes}')
replace_str. _$0_,"" _$0_list_of_blending_modes={t} rm.

How to preview an animation of 3D image without exporting to .mp4?

Can g’mic do real time animations?
Maybe because I think there are some in the demo command. Maybe you can check the code?

I saw this command in the scripting tutorial/ fun part… G'MIC - GREYC's Magic for Image Computing: A Full-Featured Open-Source Framework for Image Processing - Funny Oneliners

torus3d 100,80,36,24 s3d f.. 1+int(y/3) a y repeat 20 800,800 +r3d[0] 0,0,1,$> pose3d. 0.88,-0.08,-0.46,0,0.36,0.75,0.56,0,0.3,-0.66,0.7,0 j3d.. .,50%,50%,-500,1,2 rm. f. "boundary=1;i==j(-1)&&i==j(0,-1)?0:255" +b. 6 equalize. 256 *. 1.7 max[-2,-1] done rm[0] r2dx 50% n 0,255

…but not sure if it does animation from the command line (?)