*/}}

ouroperations.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. #include "ourpaint.h"
  2. #include "png.h"
  3. #include "lcms2.h"
  4. OurPaint *Our;
  5. extern LA MAIN;
  6. extern tnsMain* T;
  7. const char OUR_CANVAS_SHADER[]="#version 430\n\
  8. layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;\n\
  9. layout(rgba16, binding = 0) uniform image2D img;\n\
  10. layout(rgba16, binding = 1) coherent uniform image2D smudge_buckets;\n\
  11. uniform ivec2 uBrushCorner;\n\
  12. uniform vec2 uBrushCenter;\n\
  13. uniform float uBrushSize;\n\
  14. uniform float uBrushHardness;\n\
  15. uniform float uBrushSmudge;\n\
  16. uniform float uBrushSlender;\n\
  17. uniform float uBrushAngle;\n\
  18. uniform vec4 uBrushColor;\n\
  19. uniform vec4 uBackgroundColor;\n\
  20. float atan2(in float y, in float x){\n\
  21. bool s = (abs(x) > abs(y)); return mix(3.1415926535/2.0 - atan(x,y), atan(y,x), s);\n\
  22. }\n\
  23. vec2 rotate(vec2 v, float angle) {\n\
  24. float s = sin(angle); float c = cos(angle);\n\
  25. return mat2(c,-s,s,c) * v;\n\
  26. }\n\
  27. vec4 mix_over(vec4 colora, vec4 colorb){\n\
  28. vec4 c; c.a=colora.a+colorb.a*(1-colora.a);\n\
  29. c.rgb=(colora.rgb+colorb.rgb*(1-colora.a));\n\
  30. return c;\n\
  31. }\n\
  32. int dab(float d, vec4 color, float size, float hardness, float smudge, vec4 smudge_color, vec4 last_color, out vec4 final){\n\
  33. vec4 cc=color;\n\
  34. float fac=1-pow(d/size,1+1/(1-hardness+1e-4));\n\
  35. cc.a=color.a*fac*(1-smudge); cc.rgb=cc.rgb*cc.a;\n\
  36. vec4 c1=mix_over(cc,last_color);\n\
  37. vec4 c2=mix(c1,smudge_color,smudge*fac*color.a);\n\
  38. final=c2;\n\
  39. return 1;\n\
  40. }\n\
  41. subroutine void BrushRoutines();\n\
  42. subroutine(BrushRoutines) void DoDabs(){\n\
  43. ivec2 px = ivec2(gl_GlobalInvocationID.xy)+uBrushCorner;\n\
  44. if(px.x<0||px.y<0||px.x>1024||px.y>1024) return;\n\
  45. vec2 fpx=vec2(px);\n\
  46. fpx=uBrushCenter+rotate(fpx-uBrushCenter,uBrushAngle);\n\
  47. fpx.x=uBrushCenter.x+(fpx.x-uBrushCenter.x)*(1+uBrushSlender);\n\
  48. float dd=distance(fpx,uBrushCenter); if(dd>uBrushSize) return;\n\
  49. vec4 dabc=imageLoad(img, px);\n\
  50. vec4 smudgec=imageLoad(smudge_buckets,ivec2(0,0));\n\
  51. vec4 final_color;\n\
  52. dab(dd,uBrushColor,uBrushSize,uBrushHardness,uBrushSmudge,smudgec,dabc,final_color);\n\
  53. dabc=final_color;\n\
  54. imageStore(img, px, dabc);\n\
  55. }\n\
  56. subroutine(BrushRoutines) void DoSample(){\n\
  57. ivec2 p=ivec2(gl_GlobalInvocationID.xy);\n\
  58. if(p!=ivec2(0,0)) return;\n\
  59. ivec2 px=p+uBrushCorner;\n\
  60. vec4 color=imageLoad(img, px);\n\
  61. imageStore(smudge_buckets,ivec2(0,0),color);\n\
  62. }\n\
  63. subroutine uniform BrushRoutines uBrushRoutineSelection;\n\
  64. \n\
  65. void main() {\n\
  66. uBrushRoutineSelection();\n\
  67. }\n\
  68. ";
  69. void our_CanvasAlphaMix(uint16_t* target, uint16_t* source){
  70. real a_1=(real)(65535-source[3])/65535;
  71. target[3]=source[3]+target[3]*a_1;
  72. target[0]=source[0]+target[0]*a_1;
  73. target[1]=source[1]+target[1]*a_1;
  74. target[2]=source[2]+target[2]*a_1;
  75. }
  76. void our_InitsRGBProfile(int Linear, void** ptr, int* psize, char* copyright, char* manufacturer, char* description){
  77. cmsCIExyYTRIPLE srgb_primaries_pre_quantized = { {0.639998686, 0.330010138, 1.0}, {0.300003784, 0.600003357, 1.0}, {0.150002046, 0.059997204, 1.0} };
  78. cmsCIExyY d65_srgb_adobe_specs = {0.3127, 0.3290, 1.0};
  79. cmsToneCurve*tonecurve; cmsToneCurve*curve[3];
  80. if(Linear){ tonecurve = cmsBuildGamma (NULL, 1.0f); }
  81. else{
  82. cmsFloat64Number srgb_parameters[5] = { 2.4, 1.0 / 1.055, 0.055 / 1.055, 1.0 / 12.92, 0.04045 };
  83. tonecurve=cmsBuildParametricToneCurve(NULL, 4, srgb_parameters);
  84. }
  85. curve[0] = curve[1] = curve[2] = tonecurve;
  86. cmsHPROFILE profile4 = cmsCreateRGBProfile (&d65_srgb_adobe_specs, &srgb_primaries_pre_quantized, curve);
  87. cmsMLU *copy = cmsMLUalloc(NULL, 1);
  88. cmsMLUsetASCII(copy, "en", "US", copyright);
  89. cmsWriteTag(profile4, cmsSigCopyrightTag, copy);
  90. cmsMLU* manu = cmsMLUalloc(NULL, 1);
  91. cmsMLUsetASCII(manu, "en", "US", manufacturer);
  92. cmsWriteTag(profile4, cmsSigDeviceMfgDescTag, manu);
  93. cmsMLU *desc = cmsMLUalloc(NULL, 1);
  94. cmsMLUsetASCII(desc, "en", "US", description);
  95. cmsWriteTag(profile4, cmsSigProfileDescriptionTag, desc);
  96. cmsSaveProfileToMem(profile4, 0, psize);
  97. (*ptr)=calloc(1,*psize); cmsSaveProfileToMem(profile4, *ptr, psize);
  98. cmsMLUfree(copy); cmsMLUfree(manu); cmsMLUfree(desc); cmsFreeToneCurve(tonecurve); cmsCloseProfile(profile4);
  99. }
  100. void our_InitColorProfiles(){
  101. char* manu="sRGB chromaticities from A Standard Default Color Space for the Internet - sRGB, http://www.w3.org/Graphics/Color/sRGB; and http://www.color.org/specification/ICC1v43_2010-12.pdf";
  102. our_InitsRGBProfile(1,&Our->icc_LinearsRGB,&Our->iccsize_LinearsRGB,"Copyright Yiming 2022.",manu,"Yiming's linear sRGB icc profile.");
  103. our_InitsRGBProfile(0,&Our->icc_sRGB,&Our->iccsize_sRGB,"Copyright Yiming 2022.",manu,"Yiming's sRGB icc profile.");
  104. }
  105. void ourui_CanvasPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  106. laColumn* c=laFirstColumn(uil);
  107. laUiItem* ui=laShowCanvas(uil,c,0,"our.canvas",0,-1);
  108. }
  109. void ourui_Layer(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  110. laColumn* c=laFirstColumn(uil); laColumn* cl,*cr; laSplitColumn(uil,c,0.7); cl=laLeftColumn(c,0);cr=laRightColumn(c,1);
  111. laUiItem* b=laBeginRow(uil,cl,0,0);
  112. laShowHeightAdjuster(uil,cl,This,"__move",0);
  113. laShowItemFull(uil,cl,This,"name",LA_WIDGET_STRING_PLAIN,0,0,0)->Expand=1;
  114. laEndRow(uil,b);
  115. laUiItem* b1=laOnConditionToggle(uil,cr,0,0,0,0,0);{ strSafeSet(&b1->ExtraInstructions,"text=☰");
  116. b=laBeginRow(uil,c,0,0);
  117. laShowItem(uil,c,This,"remove")->Flags|=LA_UI_FLAGS_ICON;
  118. laShowSeparator(uil,c)->Expand=1;
  119. laShowItemFull(uil,c,This,"move",0,"direction=up;icon=🡱;",0,0)->Flags|=LA_UI_FLAGS_ICON;
  120. laShowItemFull(uil,c,This,"move",0,"direction=down;icon=🡳;",0,0)->Flags|=LA_UI_FLAGS_ICON;
  121. laEndRow(uil,b);
  122. }laEndCondition(uil,b1);
  123. }
  124. void ourui_LayersPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  125. laColumn* c=laFirstColumn(uil);
  126. laUiItem* b=laOnConditionThat(uil,c,laPropExpression(0,"our.canvas.current_layer"));{
  127. laUiItem* b1=laBeginRow(uil,c,0,0);
  128. laShowItem(uil,c,0,"our.canvas.current_layer.name")->Expand=1;
  129. laShowItem(uil,c,0,"OUR_new_layer")->Flags|=LA_UI_FLAGS_ICON;
  130. laEndRow(uil,b1);
  131. }laElse(uil,b);{
  132. laShowItem(uil,c,0,"OUR_new_layer");
  133. }laEndCondition(uil,b);
  134. laShowItemFull(uil,c,0,"our.canvas.layers",0,0,0,0);
  135. b=laBeginRow(uil,c,0,0);
  136. laShowLabel(uil,c,"Background",0,0)->Expand=1;
  137. laShowItemFull(uil,c,0,"our.background_color",LA_WIDGET_FLOAT_COLOR,0,0,0);
  138. laEndRow(uil,b);
  139. }
  140. void ourui_Brush(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  141. laColumn* c=laFirstColumn(uil); laColumn* cl,*cr; laSplitColumn(uil,c,0.7); cl=laLeftColumn(c,0);cr=laRightColumn(c,1);
  142. laUiItem* b=laBeginRow(uil,cl,0,0);
  143. laShowHeightAdjuster(uil,cl,This,"__move",0);
  144. laShowItemFull(uil,cl,This,"name",LA_WIDGET_STRING_PLAIN,0,0,0)->Expand=1;
  145. laEndRow(uil,b);
  146. laUiItem* b1=laOnConditionToggle(uil,cr,0,0,0,0,0);{ strSafeSet(&b1->ExtraInstructions,"text=☰");
  147. b=laBeginRow(uil,c,0,0);
  148. laShowItem(uil,c,This,"remove")->Flags|=LA_UI_FLAGS_ICON;
  149. laShowSeparator(uil,c)->Expand=1;
  150. laShowItemFull(uil,c,This,"move",0,"direction=up;icon=🡱;",0,0)->Flags|=LA_UI_FLAGS_ICON;
  151. laShowItemFull(uil,c,This,"move",0,"direction=down;icon=🡳;",0,0)->Flags|=LA_UI_FLAGS_ICON;
  152. laEndRow(uil,b);
  153. }laEndCondition(uil,b1);
  154. }
  155. void ourui_ToolsPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  156. laColumn* c=laFirstColumn(uil); laColumn* cl,*cr; laSplitColumn(uil,c,0.5); cl=laLeftColumn(c,0);cr=laRightColumn(c,0);
  157. laUiItem* b1, *b2;
  158. #define OUR_BR b1=laBeginRow(uil,c,0,0);
  159. #define OUR_ER laEndRow(uil,b1);
  160. #define OUR_PRESSURE(a)\
  161. b2=laOnConditionThat(uil,c,laNot(laPropExpression(0,"our.tools.current_brush.use_nodes")));\
  162. laShowItemFull(uil,c,0,"our.tools.current_brush." a,0,"text=P",0,0);\
  163. laEndCondition(uil,b2);
  164. laShowItem(uil,c,0,"our.tool")->Flags|=LA_UI_FLAGS_EXPAND;
  165. laShowLabel(uil,c,"Brush settings:",0,0);
  166. laUiItem* bt=laOnConditionThat(uil,c,laEqual(laPropExpression(0,"our.tool"),laIntExpression(OUR_TOOL_PAINT)));{
  167. laUiItem* b=laOnConditionThat(uil,c,laPropExpression(0,"our.tools.current_brush"));{
  168. laShowItem(uil,c,0,"our.tools.current_brush.name");
  169. laShowItem(uil,c,0,"our.tools.current_brush.use_nodes");
  170. OUR_BR laShowItem(uil,c,0,"our.tools.current_brush.size")->Expand=1; OUR_PRESSURE("pressure_size") OUR_ER
  171. OUR_BR laShowItem(uil,c,0,"our.tools.current_brush.transparency")->Expand=1; OUR_PRESSURE("pressure_transparency") OUR_ER
  172. OUR_BR laShowItem(uil,c,0,"our.tools.current_brush.hardness")->Expand=1; OUR_PRESSURE("pressure_hardness") OUR_ER
  173. laShowItem(uil,c,0,"our.tools.current_brush.slender");
  174. laShowItem(uil,c,0,"our.tools.current_brush.angle");
  175. OUR_BR laShowItem(uil,c,0,"our.tools.current_brush.smudge")->Expand=1; OUR_PRESSURE("pressure_smudge") OUR_ER
  176. laShowItem(uil,c,0,"our.tools.current_brush.dabs_per_size");
  177. laShowItem(uil,c,0,"our.tools.current_brush.smudge_resample_length");
  178. }laEndCondition(uil,b);
  179. laShowSeparator(uil,c);
  180. laShowLabel(uil,c,"Select a brush:",0,0);
  181. laShowItemFull(uil,c,0,"our.tools.brushes",0,0,0,0);
  182. laShowItem(uil,c,0,"OUR_new_brush");
  183. }laEndCondition(uil,bt);
  184. bt=laOnConditionThat(uil,c,laEqual(laPropExpression(0,"our.tool"),laIntExpression(OUR_TOOL_CROP)));{
  185. laShowItemFull(uil,c,0,"our.show_border",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0);
  186. laUiItem* b=laOnConditionThat(uil,c,laPropExpression(0,"our.show_border"));{
  187. laShowLabel(uil,cl,"Position:",0,0); laShowItem(uil,cr,0,"our.canvas.position")->Flags|=LA_UI_FLAGS_TRANSPOSE;
  188. laShowSeparator(uil,c);
  189. laShowLabel(uil,cl,"Size:",0,0); laShowItem(uil,cr,0,"our.canvas.size")->Flags|=LA_UI_FLAGS_TRANSPOSE;
  190. laShowSeparator(uil,c);
  191. laShowItem(uil,c,0,"our.border_alpha");
  192. }laEndCondition(uil,b);
  193. }laEndCondition(uil,bt);
  194. }
  195. void ourui_ColorPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  196. laColumn* c=laFirstColumn(uil);
  197. laShowItemFull(uil,c,0,"our.current_color",LA_WIDGET_FLOAT_COLOR_HCY,0,0,0);
  198. }
  199. void ourui_BrushPage(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  200. laColumn* c=laFirstColumn(uil);
  201. laShowItemFull(uil,c,0,"our.tools.current_brush",LA_WIDGET_COLLECTION_SELECTOR,0,laui_IdentifierOnly,0);
  202. laUiItem* b=laOnConditionThat(uil,c,laPropExpression(0,"our.tools.current_brush"));{
  203. laShowItemFull(uil,c,0,"our.tools.current_brush.rack_page",LA_WIDGET_COLLECTION_SINGLE,0,0,0)->Extra->HeightCoeff=-1;
  204. }laEndCondition(uil,b);
  205. }
  206. void our_CanvasDrawTextures(){
  207. tnsUseImmShader; tnsEnableShaderv(T->immShader);
  208. for(OurLayer* l=Our->Layers.pLast;l;l=l->Item.pPrev){
  209. int any=0;
  210. for(int row=0;row<OUR_TEX_TILES_PER_ROW;row++){
  211. if(!l->TexTiles[row]) continue;
  212. for(int col=0;col<OUR_TEX_TILES_PER_ROW;col++){
  213. if(!l->TexTiles[row][col] || !l->TexTiles[row][col]->Texture) continue;
  214. int sx=l->TexTiles[row][col]->l,sy=l->TexTiles[row][col]->b;
  215. real pad=(real)OUR_TEX_TILE_SEAM/OUR_TEX_TILE_W; int seam=OUR_TEX_TILE_SEAM;
  216. tnsDraw2DTextureArg(l->TexTiles[row][col]->Texture,sx+seam,sy+OUR_TEX_TILE_W-seam,OUR_TEX_TILE_W-seam*2,-OUR_TEX_TILE_W+seam*2,0,pad,pad,pad,pad);
  217. any=1;
  218. }
  219. }
  220. if(any) tnsFlush();
  221. }
  222. }
  223. void our_CanvasDrawTiles(){
  224. OurLayer* l=Our->CurrentLayer; if(!l) return;
  225. tnsUseImmShader; tnsEnableShaderv(T->immShader); tnsUniformUseTexture(T->immShader,0,0); tnsUseNoTexture();
  226. int any=0;
  227. for(int row=0;row<OUR_TEX_TILES_PER_ROW;row++){
  228. if(!l->TexTiles[row]) continue;
  229. for(int col=0;col<OUR_TEX_TILES_PER_ROW;col++){
  230. if(!l->TexTiles[row][col] || !l->TexTiles[row][col]->Texture) continue;
  231. int sx=l->TexTiles[row][col]->l,sy=l->TexTiles[row][col]->b;
  232. //tnsVertex2d(sx, sy); tnsVertex2d(sx+OUR_TEX_TILE_W,sy);
  233. //tnsVertex2d(sx+OUR_TEX_TILE_W, sy+OUR_TEX_TILE_W); tnsVertex2d(sx,sy+OUR_TEX_TILE_W);
  234. //tnsColor4dv(laAccentColor(LA_BT_NORMAL));
  235. //tnsPackAs(GL_TRIANGLE_FAN);
  236. tnsVertex2d(sx, sy); tnsVertex2d(sx+OUR_TEX_TILE_W,sy);
  237. tnsVertex2d(sx+OUR_TEX_TILE_W, sy+OUR_TEX_TILE_W); tnsVertex2d(sx,sy+OUR_TEX_TILE_W);
  238. tnsColor4dv(laAccentColor(LA_BT_TEXT));
  239. tnsPackAs(GL_LINE_LOOP);
  240. }
  241. }
  242. if(any) tnsFlush();
  243. }
  244. void our_CanvasDrawCropping(OurCanvasDraw* ocd){
  245. tnsUseImmShader; tnsEnableShaderv(T->immShader); tnsUniformUseTexture(T->immShader,0,0); tnsUseNoTexture();
  246. tnsColor4d(0,0,0,Our->BorderAlpha);
  247. tnsVertex2d(-1e6,Our->Y); tnsVertex2d(1e6,Our->Y); tnsVertex2d(-1e6,1e6); tnsVertex2d(1e6,1e6); tnsPackAs(GL_TRIANGLE_FAN);
  248. tnsVertex2d(-1e6,Our->Y); tnsVertex2d(Our->X,Our->Y); tnsVertex2d(Our->X,Our->Y-Our->H); tnsVertex2d(-1e6,Our->Y-Our->H); tnsPackAs(GL_TRIANGLE_FAN);
  249. tnsVertex2d(1e6,Our->Y); tnsVertex2d(Our->X+Our->W,Our->Y); tnsVertex2d(Our->X+Our->W,Our->Y-Our->H); tnsVertex2d(1e6,Our->Y-Our->H); tnsPackAs(GL_TRIANGLE_FAN);
  250. tnsVertex2d(-1e6,Our->Y-Our->H); tnsVertex2d(1e6,Our->Y-Our->H); tnsVertex2d(-1e6,-1e6); tnsVertex2d(1e6,-1e6); tnsPackAs(GL_TRIANGLE_FAN);
  251. if(Our->Tool==OUR_TOOL_CROP){
  252. tnsColor4dv(laAccentColor(LA_BT_TEXT));
  253. tnsVertex2d(Our->X,Our->Y); tnsVertex2d(Our->X+Our->W,Our->Y); tnsVertex2d(Our->X+Our->W,Our->Y-Our->H); tnsVertex2d(Our->X,Our->Y-Our->H);
  254. tnsPackAs(GL_LINE_LOOP);
  255. glLineWidth(3); tnsFlush(); glLineWidth(1);
  256. }
  257. }
  258. void our_CanvasDrawInit(laUiItem* ui){
  259. ui->Extra=memAcquireHyper(sizeof(OurCanvasDraw));
  260. laFirstColumn(laAddTabPage(ui, "New Group"));
  261. OurCanvasDraw* ocd=ui->Extra;
  262. ocd->Base.ParentUi=ui;
  263. ocd->Base.HeightCoeff = 10;
  264. ocd->Base.ZoomX = 1;
  265. ocd->Base.ZoomY = 1;
  266. ocd->Base.ImageDrawAlpha = 1;
  267. ocd->Base.ImageDrawBorder = 1;
  268. ocd->Base.AdaptiveLineWidth = 1;
  269. ocd->Base.ClearBackground = 1;
  270. ocd->ShowTiles=1;
  271. int work_grp_cnt[3];
  272. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 0, &work_grp_cnt[0]);
  273. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 1, &work_grp_cnt[1]);
  274. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 2, &work_grp_cnt[2]);
  275. printf("max global (total) work group counts x:%i y:%i z:%i\n", work_grp_cnt[0], work_grp_cnt[1], work_grp_cnt[2]);
  276. int work_grp_size[3];
  277. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0, &work_grp_size[0]);
  278. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 1, &work_grp_size[1]);
  279. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2, &work_grp_size[2]);
  280. printf("max local (in one shader) work group sizes x:%i y:%i z:%i\n", work_grp_size[0], work_grp_size[1], work_grp_size[2]);
  281. int work_grp_inv;
  282. glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &work_grp_inv);
  283. printf("max local work group invocations %i\n", work_grp_inv);
  284. }
  285. void our_CanvasDrawCanvas(laBoxedTheme *bt, OurPaint *unused_c, laUiItem* ui){
  286. OurCanvasDraw* ocd=ui->Extra; OurPaint* oc=ui->PP.EndInstance; laCanvasExtra*e=&ocd->Base;
  287. int W, H; W = ui->R - ui->L; H = ui->B - ui->U;
  288. tnsFlush();
  289. if (!e->OffScr || e->OffScr->pColor[0]->Height != ui->B - ui->U || e->OffScr->pColor[0]->Width != ui->R - ui->L){
  290. if (e->OffScr) tnsDelete2DOffscreen(e->OffScr);
  291. e->OffScr = tnsCreate2DOffscreen(GL_RGBA16, W, H, 0, 0);
  292. }
  293. //our_CANVAS_TEST(bt,ui);
  294. glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
  295. //glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,GL_ONE,GL_ONE);
  296. tnsDrawToOffscreen(e->OffScr,1,0);
  297. tnsViewportWithScissor(0, 0, W, H);
  298. tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  299. tnsOrtho(e->PanX - W * e->ZoomX / 2, e->PanX + W * e->ZoomX / 2, e->PanY - e->ZoomY * H / 2, e->PanY + e->ZoomY * H / 2, 100, -100);
  300. tnsClearColor(LA_COLOR3(Our->BackgroundColor),1); tnsClearAll();
  301. if(ocd->ShowTiles){ our_CanvasDrawTiles(); }
  302. our_CanvasDrawTextures();
  303. if(Our->ShowBorder){ our_CanvasDrawCropping(ocd); }
  304. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
  305. }
  306. void our_CanvasDrawOverlay(laUiItem* ui,int h){
  307. laCanvasExtra *e = ui->Extra;
  308. laBoxedTheme *bt = (*ui->Type->Theme);
  309. tnsUseImmShader();tnsEnableShaderv(T->immShader);
  310. tnsUniformColorMode(T->immShader, 2);
  311. tnsDraw2DTextureDirectly(e->OffScr->pColor[0], ui->L, ui->U, ui->R - ui->L, ui->B - ui->U);
  312. tnsFlush();
  313. tnsUniformColorMode(T->immShader, 0);
  314. la_CanvasDefaultOverlay(ui, h);
  315. }
  316. OurLayer* our_NewLayer(char* name){
  317. OurLayer* l=memAcquire(sizeof(OurLayer)); strSafeSet(&l->Name,name); lstPushItem(&Our->Layers, l);
  318. memAssignRef(Our, &Our->CurrentLayer, l);
  319. return l;
  320. }
  321. void ourbeforefree_Layer(OurLayer* l){
  322. for(int row=0;row<OUR_TEX_TILES_PER_ROW;row++){ if(!l->TexTiles[row]) continue;
  323. for(int col=0;col<OUR_TEX_TILES_PER_ROW;col++){ if(!l->TexTiles[row][col]) continue;
  324. if(l->TexTiles[row][col]->Texture) tnsDeleteTexture(l->TexTiles[row][col]->Texture); l->TexTiles[row][col]->Texture=0;
  325. if(l->TexTiles[row][col]->Data) free(l->TexTiles[row][col]->Data); l->TexTiles[row][col]->Data=0;
  326. if(l->TexTiles[row][col]->FullData) free(l->TexTiles[row][col]->FullData); l->TexTiles[row][col]->FullData=0;
  327. memFree(l->TexTiles[row][col]);
  328. }
  329. memFree(l->TexTiles[row]); l->TexTiles[row]=0;
  330. }
  331. memFree(l->TexTiles);
  332. }
  333. void our_RemoveLayer(OurLayer* l){
  334. strSafeDestroy(&l->Name); lstRemoveItem(&Our->Layers, l);
  335. if(Our->CurrentLayer==l){ OurLayer* nl=l->Item.pPrev?l->Item.pPrev:l->Item.pNext; memAssignRef(Our, &Our->CurrentLayer, nl); }
  336. ourbeforefree_Layer(l);
  337. memLeave(l);
  338. }
  339. OurBrush* our_NewBrush(char* name, real Size, real Hardness, real DabsPerSize, real Transparency, real Smudge, real SmudgeResampleLength,
  340. int PressureSize, int PressureHardness, int PressureTransparency, int PressureSmudge){
  341. OurBrush* b=memAcquireHyper(sizeof(OurBrush)); strSafeSet(&b->Name,name); lstAppendItem(&Our->Brushes, b);
  342. b->Size=Size; b->Hardness=Hardness; b->DabsPerSize=DabsPerSize; b->Transparency=Transparency; b->Smudge=Smudge;
  343. b->PressureHardness=PressureHardness; b->PressureSize=PressureSize; b->PressureTransparency=PressureTransparency; b->PressureSmudge=PressureSmudge;
  344. b->SmudgeResampleLength = SmudgeResampleLength;
  345. memAssignRef(Our, &Our->CurrentBrush, b);
  346. b->Rack=memAcquireHyper(sizeof(laRackPage)); b->Rack->RackType=LA_RACK_TYPE_DRIVER;
  347. return b;
  348. }
  349. void our_RemoveBrush(OurBrush* b){
  350. strSafeDestroy(&b->Name); lstRemoveItem(&Our->Brushes, b);
  351. if(Our->CurrentLayer==b){ OurLayer* nb=b->Item.pPrev?b->Item.pPrev:b->Item.pNext; memAssignRef(Our, &Our->CurrentBrush, nb); }
  352. memLeave(b->Rack); b->Rack=0;
  353. memLeave(b);
  354. }
  355. int our_BufferAnythingVisible(uint16_t* buf, int elemcount){
  356. for(int i=0;i<elemcount;i++){
  357. uint16_t* rgba=&buf[i*4]; if(rgba[3]) return 1;
  358. }
  359. return 0;
  360. }
  361. void our_TileEnsureUndoBuffer(OurTexTile* t, real xmin,real xmax, real ymin,real ymax){
  362. int _l=floor(xmin),_r=ceil(xmax),_u=ceil(ymax),_b=floor(ymin);
  363. if(t->CopyBuffer) free(t->CopyBuffer); t->CopyBuffer=0; //shouldn't happen
  364. if(_l>=t->r || _r<t->l || _b>=t->u || _u<t->b || _l==_r || _u==_b) return;
  365. t->cl=TNS_MAX2(_l,t->l)-t->l;t->cr=TNS_MIN2(_r,t->r)-t->l;t->cu=TNS_MIN2(_u,t->u)-t->b;t->cb=TNS_MAX2(_b,t->b)-t->b;
  366. int rows=t->cu-t->cb,cols=t->cr-t->cl;
  367. int bufsize=cols*4*rows*sizeof(uint16_t);
  368. t->CopyBuffer=calloc(1,bufsize);
  369. for(int row=0;row<rows;row++){
  370. memcpy(&t->CopyBuffer[row*cols*4],&t->FullData[((+row+t->cb)*OUR_TEX_TILE_W+t->cl)*4],sizeof(uint16_t)*4*cols);
  371. }
  372. uint16_t* temp=malloc(bufsize);
  373. tnsBindTexture(t->Texture);
  374. glGetTextureSubImage(t->Texture->GLTexHandle, 0, t->cl, t->cb, 0, cols,rows,1, GL_RGBA, GL_UNSIGNED_SHORT, bufsize, temp);
  375. for(int row=0;row<rows;row++){
  376. memcpy(&t->FullData[((+row+t->cb)*OUR_TEX_TILE_W+t->cl)*4],&temp[row*cols*4],sizeof(uint16_t)*4*cols);
  377. }
  378. }
  379. void our_TileSwapBuffers(OurTexTile* t, uint16_t* data, int IsRedo, int l, int r, int u, int b){
  380. int rows=u-b,cols=r-l; int bufsize=rows*cols*sizeof(uint16_t)*4;
  381. uint16_t* temp=malloc(bufsize);
  382. memcpy(temp,data,bufsize);
  383. for(int row=0;row<rows;row++){
  384. memcpy(&data[row*cols*4],&t->FullData[((+row+b)*OUR_TEX_TILE_W+l)*4],sizeof(uint16_t)*4*cols);
  385. memcpy(&t->FullData[((+row+b)*OUR_TEX_TILE_W+l)*4],&temp[row*cols*4],sizeof(uint16_t)*4*cols);
  386. }
  387. tnsBindTexture(t->Texture);
  388. glGetError();
  389. uint16_t* use_data=temp;
  390. if(IsRedo){ use_data=data; }
  391. glTexSubImage2D(GL_TEXTURE_2D, 0, l, b, cols,rows,GL_RGBA,GL_UNSIGNED_SHORT,use_data);
  392. free(temp);
  393. }
  394. void ourundo_Tiles(OurUndo* undo){
  395. for(OurUndoTile* ut=undo->Tiles.pFirst;ut;ut=ut->Item.pNext){
  396. our_TileSwapBuffers(undo->Layer->TexTiles[ut->row][ut->col], ut->CopyData, 0, ut->l, ut->r, ut->u, ut->b);
  397. }
  398. laNotifyUsers("our.canvas");
  399. }
  400. void ourredo_Tiles(OurUndo* undo){
  401. for(OurUndoTile* ut=undo->Tiles.pFirst;ut;ut=ut->Item.pNext){
  402. our_TileSwapBuffers(undo->Layer->TexTiles[ut->row][ut->col], ut->CopyData, 0, ut->l, ut->r, ut->u, ut->b);
  403. }
  404. laNotifyUsers("our.canvas");
  405. }
  406. void ourundo_Free(OurUndo* undo,int FromLeft){
  407. OurUndoTile* ut;
  408. while(ut=lstPopItem(&undo->Tiles)){ free(ut->CopyData); memFree(ut); }
  409. memFree(undo);
  410. }
  411. #define OUR_XXYY_TO_COL_ROW_RANGE\
  412. l=(int)(floor(OUR_TEX_TILE_CTR+(xmin-OUR_TEX_TILE_SEAM)/OUR_TEX_TILE_W_USE+0.5));\
  413. r=(int)(floor(OUR_TEX_TILE_CTR+(xmax+OUR_TEX_TILE_SEAM)/OUR_TEX_TILE_W_USE+0.5));\
  414. u=(int)(floor(OUR_TEX_TILE_CTR+(ymax+OUR_TEX_TILE_SEAM)/OUR_TEX_TILE_W_USE+0.5));\
  415. b=(int)(floor(OUR_TEX_TILE_CTR+(ymin-OUR_TEX_TILE_SEAM)/OUR_TEX_TILE_W_USE+0.5));
  416. void our_RecordUndo(OurLayer* ol, real xmin,real xmax, real ymin,real ymax){
  417. if(xmax<xmin || ymax<ymin) return;
  418. int l,r,u,b; OUR_XXYY_TO_COL_ROW_RANGE;
  419. OurUndo* undo=memAcquire(sizeof(OurUndo)); undo->Layer=ol;
  420. for(int row=b;row<=u;row++){ if(!ol->TexTiles[row]) continue;// Should not happen.
  421. for(int col=l;col<=r;col++){ if(!ol->TexTiles[row][col]) continue; OurTexTile*t=ol->TexTiles[row][col];
  422. our_TileEnsureUndoBuffer(t,xmin,xmax,ymin,ymax);
  423. if(!t->CopyBuffer) continue;
  424. OurUndoTile* ut=memAcquire(sizeof(OurUndoTile));
  425. ut->l=t->cl; ut->r=t->cr; ut->u=t->cu; ut->b=t->cb;
  426. ut->CopyData=t->CopyBuffer; t->CopyBuffer=0;
  427. ut->col=col;ut->row=row;
  428. lstAppendItem(&undo->Tiles,ut);
  429. }
  430. }
  431. if(!undo->Tiles.pFirst){ memFree(undo); return; /*unlikely;*/ }
  432. laFreeNewerDifferences();
  433. laRecordCustomDifferences(undo,ourundo_Tiles,ourredo_Tiles,ourundo_Free);
  434. laPushDifferences("Paint",0);
  435. }
  436. void our_LayerEnsureTiles(OurLayer* ol, real xmin,real xmax, real ymin,real ymax, int Aligned, int *tl, int *tr, int* tu, int* tb){
  437. int l,r,u,b;
  438. if(Aligned){
  439. l=(int)(floor(OUR_TEX_TILE_CTR+(xmin)/OUR_TEX_TILE_W_USE+0.5));
  440. r=(int)(floor(OUR_TEX_TILE_CTR+(xmax-1)/OUR_TEX_TILE_W_USE+0.5));
  441. u=(int)(floor(OUR_TEX_TILE_CTR+(ymax-1)/OUR_TEX_TILE_W_USE+0.5));
  442. b=(int)(floor(OUR_TEX_TILE_CTR+(ymin)/OUR_TEX_TILE_W_USE+0.5));
  443. }else{
  444. OUR_XXYY_TO_COL_ROW_RANGE;
  445. }
  446. TNS_CLAMP(l,0,OUR_TEX_TILES_PER_ROW-1);
  447. TNS_CLAMP(r,0,OUR_TEX_TILES_PER_ROW-1);
  448. TNS_CLAMP(u,0,OUR_TEX_TILES_PER_ROW-1);
  449. TNS_CLAMP(b,0,OUR_TEX_TILES_PER_ROW-1);
  450. for(int row=b;row<=u;row++){
  451. if(!ol->TexTiles[row]){ol->TexTiles[row]=memAcquireSimple(sizeof(OurTexTile*)*OUR_TEX_TILES_PER_ROW);}
  452. for(int col=l;col<=r;col++){
  453. if(ol->TexTiles[row][col]) continue;
  454. ol->TexTiles[row][col]=memAcquireSimple(sizeof(OurTexTile));
  455. OurTexTile*t=ol->TexTiles[row][col];
  456. t->Texture=tnsCreate2DTexture(GL_RGBA16,OUR_TEX_TILE_W,OUR_TEX_TILE_W,0);
  457. int sx=((real)col-OUR_TEX_TILE_CTR-0.5)*OUR_TEX_TILE_W_USE,sy=((real)row-OUR_TEX_TILE_CTR-0.5)*OUR_TEX_TILE_W_USE;
  458. t->l=sx-OUR_TEX_TILE_SEAM,t->b=sy-OUR_TEX_TILE_SEAM; t->r=t->l+OUR_TEX_TILE_W; t->u=t->b+OUR_TEX_TILE_W;
  459. uint16_t initColor[]={0,0,0,0};
  460. glClearTexImage(t->Texture->GLTexHandle, 0, GL_RGBA, GL_UNSIGNED_SHORT, 0);
  461. t->FullData=calloc(OUR_TEX_TILE_W*4,OUR_TEX_TILE_W*sizeof(uint16_t));
  462. }
  463. }
  464. *tl=l; *tr=r; *tu=u; *tb=b;
  465. }
  466. void our_TileTextureToImage(OurTexTile* ot, int SX, int SY, int composite){
  467. if(!ot->Texture) return;
  468. int bufsize=sizeof(uint16_t)*OUR_TEX_TILE_W_USE*OUR_TEX_TILE_W_USE*4;
  469. ot->Data=malloc(bufsize); int seam=OUR_TEX_TILE_SEAM; int width=OUR_TEX_TILE_W_USE;
  470. tnsBindTexture(ot->Texture); glPixelStorei(GL_PACK_ALIGNMENT, 2);
  471. glGetTextureSubImage(ot->Texture->GLTexHandle, 0, seam, seam, 0, width, width,1, GL_RGBA, GL_UNSIGNED_SHORT, bufsize, ot->Data);
  472. if(composite){
  473. for(int row=0;row<OUR_TEX_TILE_W_USE;row++){
  474. for(int col=0;col<OUR_TEX_TILE_W_USE;col++){
  475. our_CanvasAlphaMix(&Our->ImageBuffer[((SY+row)*Our->ImageW+SX+col)*4], &ot->Data[(row*OUR_TEX_TILE_W_USE+col)*4]);
  476. }
  477. }
  478. }else{
  479. for(int row=0;row<OUR_TEX_TILE_W_USE;row++){
  480. memcpy(&Our->ImageBuffer[((SY+row)*Our->ImageW+SX)*4],&ot->Data[(row*OUR_TEX_TILE_W_USE)*4],sizeof(uint16_t)*4*OUR_TEX_TILE_W_USE);
  481. }
  482. }
  483. free(ot->Data); ot->Data=0;
  484. }
  485. void our_TileImageToTexture(OurTexTile* ot, int SX, int SY){
  486. if(!ot->Texture) return;
  487. int pl=(SX!=0)?OUR_TEX_TILE_SEAM:0, pr=((SX+OUR_TEX_TILE_W_USE)!=Our->ImageW)?OUR_TEX_TILE_SEAM:0;
  488. int pu=(SY!=0)?OUR_TEX_TILE_SEAM:0, pb=((SY+OUR_TEX_TILE_W_USE)!=Our->ImageH)?OUR_TEX_TILE_SEAM:0;
  489. int bufsize=sizeof(uint16_t)*(OUR_TEX_TILE_W+pl+pr)*(OUR_TEX_TILE_W+pu+pb)*4;
  490. ot->Data=malloc(bufsize); int width=OUR_TEX_TILE_W_USE+pl+pr, height=OUR_TEX_TILE_W_USE+pu+pb;
  491. for(int row=0;row<height;row++){
  492. memcpy(&ot->Data[((row)*width)*4],&Our->ImageBuffer[((SY+row-pu)*Our->ImageW+SX-pl)*4],sizeof(uint16_t)*4*width);
  493. }
  494. if(!our_BufferAnythingVisible(ot->Data, bufsize/sizeof(uint16_t)/4)){ tnsDeleteTexture(ot->Texture); ot->Texture=0; }
  495. else{
  496. tnsBindTexture(ot->Texture);
  497. glTexSubImage2D(GL_TEXTURE_2D, 0, OUR_TEX_TILE_SEAM-pl, OUR_TEX_TILE_SEAM-pu, width, height, GL_RGBA, GL_UNSIGNED_SHORT, ot->Data);
  498. }
  499. free(ot->Data); ot->Data=0;
  500. }
  501. int our_LayerEnsureImageBuffer(OurLayer* ol, int OnlyCalculate){
  502. int l=1000,r=-1000,u=-1000,b=1000; int any=0;
  503. for(int row=0;row<OUR_TEX_TILES_PER_ROW;row++){ if(!ol->TexTiles[row]) continue;
  504. if(row<b) b=row; if(row>u) u=row;
  505. for(int col=0;col<OUR_TEX_TILES_PER_ROW;col++){ if(!ol->TexTiles[row][col]) continue;
  506. if(col<l) l=col; if(col>r) r=col; any++;
  507. }
  508. }
  509. if(!any) return 0;
  510. Our->ImageW = OUR_TEX_TILE_W_USE*(r-l+1); Our->ImageH = OUR_TEX_TILE_W_USE*(u-b+1);
  511. Our->ImageX =((real)l-OUR_TEX_TILE_CTR-0.5)*OUR_TEX_TILE_W_USE; Our->ImageY=((real)b-OUR_TEX_TILE_CTR-0.5)*OUR_TEX_TILE_W_USE;
  512. if(!OnlyCalculate){
  513. if(Our->ImageBuffer) free(Our->ImageBuffer);
  514. Our->ImageBuffer = calloc(Our->ImageW*4,Our->ImageH*sizeof(uint16_t));
  515. }
  516. return 1;
  517. }
  518. int our_CanvasEnsureImageBuffer(){
  519. int x=INT_MAX,y=INT_MAX,w=-INT_MAX,h=-INT_MAX;
  520. for(OurLayer* l=Our->Layers.pFirst;l;l=l->Item.pNext){
  521. our_LayerEnsureImageBuffer(l,1);
  522. if(Our->ImageX<x) x=Our->ImageX; if(Our->ImageY<y) y=Our->ImageY;
  523. if(Our->ImageW>w) w=Our->ImageW; if(Our->ImageH>h) h=Our->ImageH;
  524. }
  525. if(w<0||h<0) return 0;
  526. Our->ImageX=x; Our->ImageY=y; Our->ImageW=w; Our->ImageH=h;
  527. if(Our->ImageBuffer) free(Our->ImageBuffer);
  528. Our->ImageBuffer = calloc(Our->ImageW*4,Our->ImageH*sizeof(uint16_t));
  529. return 1;
  530. }
  531. void our_CanvasFillImageBufferBackground(){
  532. int count=Our->ImageW*Our->ImageH;
  533. Our->BColorU16[0]=Our->BackgroundColor[0]*65535; Our->BColorU16[1]=Our->BackgroundColor[1]*65535;
  534. Our->BColorU16[2]=Our->BackgroundColor[2]*65535; Our->BColorU16[3]=65535;
  535. for(int i=0;i<count;i++){
  536. uint16_t* p=&Our->ImageBuffer[i*4]; tnsVectorSet4v(p,Our->BColorU16);
  537. }
  538. }
  539. void our_LayerToImageBuffer(OurLayer* ol, int composite){
  540. for(int row=0;row<OUR_TEX_TILES_PER_ROW;row++){ if(!ol->TexTiles[row]) continue;
  541. for(int col=0;col<OUR_TEX_TILES_PER_ROW;col++){ if(!ol->TexTiles[row][col]) continue;
  542. int sx=ol->TexTiles[row][col]->l+OUR_TEX_TILE_SEAM,sy=ol->TexTiles[row][col]->b+OUR_TEX_TILE_SEAM;
  543. our_TileTextureToImage(ol->TexTiles[row][col], sx-Our->ImageX, sy-Our->ImageY, composite);
  544. }
  545. }
  546. }
  547. void our_LayerToTexture(OurLayer* ol){
  548. for(int row=0;row<OUR_TEX_TILES_PER_ROW;row++){ if(!ol->TexTiles[row]) continue;
  549. for(int col=0;col<OUR_TEX_TILES_PER_ROW;col++){ if(!ol->TexTiles[row][col]) continue;
  550. int sx=ol->TexTiles[row][col]->l+OUR_TEX_TILE_SEAM,sy=ol->TexTiles[row][col]->b+OUR_TEX_TILE_SEAM;
  551. our_TileImageToTexture(ol->TexTiles[row][col], sx-Our->ImageX, sy-Our->ImageY);
  552. }
  553. }
  554. }
  555. void our_GetFinalDimension(int UseFrame, int* x, int* y, int* w, int* h){
  556. if(UseFrame){ *x=Our->X; *y=Our->Y; *w=Our->W; *h=Our->H; }
  557. else{ *x=Our->ImageX; *y=Our->ImageY; *w=Our->ImageW; *h=Our->ImageH; }
  558. printf("%d %d %d %d, %d %d %d %d\n",Our->X, Our->Y, Our->W, Our->H,Our->ImageX, Our->ImageY, Our->ImageW, Our->ImageH);
  559. }
  560. uint16_t* our_GetFinalRow(int UseFrame, int row, int x, int y, int w, int h, uint16_t* temp){
  561. if(!UseFrame) return &Our->ImageBuffer[Our->ImageW*(Our->ImageH-row-1)*4];
  562. int userow=(h-row-1)-(Our->ImageY-(y-h));
  563. if(userow<0 || userow>=Our->ImageH){ for(int i=0;i<w;i++){ tnsVectorSet4v(&temp[i*4],Our->BColorU16); } return temp; }
  564. int sstart=x>Our->ImageX?x-Our->ImageX:0, tstart=x>Our->ImageX?0:Our->ImageX-x;
  565. int slen=(x+w>Our->ImageX+Our->ImageW)?(Our->ImageW-sstart):(Our->ImageW-sstart-(Our->ImageX+Our->ImageW-x-w));
  566. for(int i=0;i<sstart;i++){ tnsVectorSet4v(&temp[i*4],Our->BColorU16); }
  567. for(int i=sstart+slen;i<w;i++){ tnsVectorSet4v(&temp[i*4],Our->BColorU16); }
  568. memcpy(&temp[tstart*4],&Our->ImageBuffer[(Our->ImageW*(userow)+sstart)*4],slen*sizeof(uint16_t)*4);
  569. return temp;
  570. }
  571. static void _our_png_write(png_structp png_ptr, png_bytep data, png_size_t length){
  572. OurLayerWrite* LayerWrite=png_get_io_ptr(png_ptr);
  573. arrEnsureLength(&LayerWrite->data,LayerWrite->NextData+length,&LayerWrite->MaxData,sizeof(unsigned char));
  574. memcpy(&LayerWrite->data[LayerWrite->NextData], data, length);
  575. LayerWrite->NextData+=length;
  576. }
  577. int our_ImageExportPNG(FILE* fp, int WriteToBuffer, void** buf, int* sizeof_buf, int UseFrame){
  578. if((!fp)&&(!WriteToBuffer)) return 0;
  579. if(!Our->ImageBuffer) return 0;
  580. png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0);
  581. png_infop info_ptr = png_create_info_struct(png_ptr);
  582. OurLayerWrite LayerWrite={0};
  583. if(WriteToBuffer){
  584. arrEnsureLength(&LayerWrite.data,0,&LayerWrite.MaxData,sizeof(unsigned char));
  585. png_set_write_fn(png_ptr,&LayerWrite,_our_png_write,0);
  586. }else{
  587. png_init_io(png_ptr, fp);
  588. }
  589. int X,Y,W,H; our_GetFinalDimension(UseFrame, &X,&Y,&W,&H);
  590. png_set_IHDR(png_ptr, info_ptr,W,H,16,PNG_COLOR_TYPE_RGBA,PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_BASE,PNG_FILTER_TYPE_BASE);
  591. // Should not set gamma, we should set either srgb chunk or iccp. Gimp bug: https://gitlab.gnome.org/GNOME/gimp/-/issues/5363
  592. // But we still include a gamma 1.0 for convenience in OurPaint internal layer reading.
  593. //png_set_gAMA(png_ptr,info_ptr,0.45455);
  594. //png_set_sRGB(png_ptr,info_ptr,PNG_sRGB_INTENT_PERCEPTUAL);
  595. //png_set_iCCP(png_ptr,info_ptr,"LA_PROFILE",PNG_COMPRESSION_TYPE_BASE,Our->icc_sRGB,Our->iccsize_sRGB);
  596. png_set_iCCP(png_ptr,info_ptr,"LA_PROFILE",PNG_COMPRESSION_TYPE_BASE,Our->icc_LinearsRGB,Our->iccsize_LinearsRGB);
  597. png_set_gAMA(png_ptr,info_ptr,1.0);
  598. // Don't set alpha mode for internal data so read and write would be the same.
  599. png_write_info(png_ptr, info_ptr);
  600. png_set_swap(png_ptr);
  601. uint16_t* temp_row=calloc(W,sizeof(uint16_t)*4);
  602. for(int i=0;i<H;i++){
  603. uint16_t* final=our_GetFinalRow(UseFrame,i,X,Y,W,H,temp_row);
  604. png_write_row(png_ptr, (png_const_bytep)final);
  605. }
  606. png_write_end(png_ptr, info_ptr);
  607. png_destroy_write_struct(&png_ptr, &info_ptr);
  608. free(Our->ImageBuffer); Our->ImageBuffer=0;
  609. if(WriteToBuffer){ *buf=LayerWrite.data; *sizeof_buf=LayerWrite.NextData; }
  610. return 1;
  611. }
  612. void our_EnsureImageBufferOnRead(OurLayer*l, int W, int H, int UseOffsets, int StartX, int StartY){
  613. int tw=W/OUR_TEX_TILE_W_USE, th=H/OUR_TEX_TILE_W_USE;
  614. int w=tw*OUR_TEX_TILE_W_USE, h=th*OUR_TEX_TILE_W_USE;
  615. if(w<W){ tw+=1; w+=OUR_TEX_TILE_W_USE; } if(h<H){ th+=1; h+=OUR_TEX_TILE_W_USE; }
  616. int ix=UseOffsets?StartX:(-tw/2*OUR_TEX_TILE_W_USE-OUR_TEX_TILE_W_USE/2);
  617. int iy=UseOffsets?StartY:(th/2*OUR_TEX_TILE_W_USE+OUR_TEX_TILE_W_USE/2);
  618. int tl,tr,tu,tb;
  619. our_LayerEnsureTiles(l,ix,ix+W,iy-H,iy,1,&tl,&tr,&tu,&tb);
  620. our_LayerEnsureImageBuffer(l, 0);
  621. Our->LoadX = ix-Our->ImageX; Our->LoadY = Our->ImageY+Our->ImageH-iy;
  622. }
  623. static void _our_png_read(png_struct *ps, png_byte *data, png_size_t length){
  624. OurLayerRead *LayerRead = (OurLayerRead*)png_get_io_ptr(ps);
  625. memcpy(data,&LayerRead->data[LayerRead->NextData],length);
  626. LayerRead->NextData+=length;
  627. }
  628. int our_LayerImportPNG(OurLayer* l, FILE* fp, void* buf, int UseProfile, int UseOffsets, int StartX, int StartY){
  629. int result=0;
  630. if((!fp&&!buf) || !l) return 0;
  631. int srgb_intent = 0;
  632. png_charp icc_profile_name = NULL;
  633. png_uint_32 icc_proflen = 0;
  634. int icc_compression_type = 0;
  635. cmsHPROFILE input_buffer_profile = NULL;
  636. cmsHTRANSFORM cmsTransform = NULL;
  637. cmsToneCurve *cmsToneCurve = NULL;
  638. cmsUInt32Number input_buffer_format = 0;
  639. #if PNG_LIBPNG_VER < 10500 // 1.5.0beta36, according to libpng CHANGES
  640. png_charp icc_profile = NULL;
  641. #else
  642. png_bytep icc_profile = NULL;
  643. #endif
  644. OurLayerRead LayerRead={0};
  645. png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,0,0,0); if (!png_ptr) { return 0; }
  646. png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { return 0; }
  647. if (setjmp(png_jmpbuf(png_ptr))) { goto cleanup_png_read; }
  648. if(buf){
  649. LayerRead.data=buf; png_set_read_fn(png_ptr, &LayerRead, _our_png_read);
  650. }else{
  651. png_init_io(png_ptr, fp);
  652. }
  653. png_read_info(png_ptr, info_ptr);
  654. png_set_swap(png_ptr);
  655. int UseSRGB=0;
  656. if(UseProfile){
  657. if(png_get_iCCP (png_ptr, info_ptr, &icc_profile_name, &icc_compression_type, &icc_profile, &icc_proflen)) {
  658. input_buffer_profile = cmsOpenProfileFromMem(icc_profile, icc_proflen);
  659. if(!input_buffer_profile) { goto cleanup_png_read; }
  660. cmsColorSpaceSignature cs_sig = cmsGetColorSpace(input_buffer_profile);
  661. if (cs_sig != cmsSigRgbData) { logPrint(" png has grayscale iCCP, OurPaint doesn't supported that yet, will load as sRGB.\n");
  662. cmsCloseProfile(input_buffer_profile); input_buffer_profile = NULL; }
  663. else{
  664. char* desc="UNAMED PROFILE";
  665. cmsUInt32Number len=cmsGetProfileInfoASCII(input_buffer_profile,cmsInfoDescription,"en","US",0,0);
  666. if(len){ desc=calloc(1,sizeof(char)*len); cmsGetProfileInfoASCII(input_buffer_profile,cmsInfoDescription,"en","US",desc,len); }
  667. logPrint(" png has iCCP: %s.\n", desc); if(len){ free(desc); }
  668. }
  669. }elif(png_get_sRGB(png_ptr,info_ptr,&srgb_intent)){
  670. logPrint(" png is sRGB.\n");
  671. UseSRGB=1;
  672. }else{
  673. // should use png_get_cHRM and png_get_gAMA, but for simplicity we just treat them as srgb,
  674. logPrint(" png doesn't contain iCCP or sRGB flags, assuming sRGB.\n");
  675. UseSRGB=1;
  676. }
  677. }
  678. if (png_get_interlace_type (png_ptr, info_ptr) != PNG_INTERLACE_NONE){ logPrint(" Interlaced png not supported.\n");
  679. goto cleanup_png_read;
  680. }
  681. png_byte ColorType = png_get_color_type(png_ptr, info_ptr);
  682. png_byte BitDepth = png_get_bit_depth(png_ptr, info_ptr);
  683. int HasAlpha = ColorType & PNG_COLOR_MASK_ALPHA;
  684. if (ColorType == PNG_COLOR_TYPE_PALETTE) { png_set_palette_to_rgb(png_ptr); }
  685. //if (ColorType == PNG_COLOR_TYPE_GRAY && BitDepth < 8) { png_set_expand_gray_1_2_4_to_8(png_ptr); }
  686. if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { png_set_tRNS_to_alpha(png_ptr); HasAlpha = 1; }
  687. if (BitDepth<16) { png_set_expand_16(png_ptr); }
  688. if (!HasAlpha) { png_set_add_alpha(png_ptr, 0xFFFF, PNG_FILLER_AFTER); }
  689. if (ColorType == PNG_COLOR_TYPE_GRAY || ColorType == PNG_COLOR_TYPE_GRAY_ALPHA) { png_set_gray_to_rgb(png_ptr); }
  690. png_read_update_info(png_ptr, info_ptr);
  691. if (png_get_bit_depth(png_ptr, info_ptr)!=16) {
  692. logPrint(" Can't convert png into 16 bits per channel, aborting.\n"); goto cleanup_png_read;
  693. }
  694. if (png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_RGB_ALPHA) {
  695. logPrint(" Can't convert png into RGBA format, aborting.\n"); goto cleanup_png_read;
  696. }
  697. if (png_get_channels(png_ptr, info_ptr) != 4) {
  698. logPrint(" Can't convert png into 4 channel RGBA format, aborting.\n"); goto cleanup_png_read;
  699. }
  700. #ifdef CMS_USE_BIG_ENDIAN
  701. input_buffer_format = TYPE_RGBA_16;
  702. #else
  703. input_buffer_format = TYPE_RGBA_16_SE;
  704. #endif
  705. if(UseProfile){
  706. cmsTransform = cmsCreateTransform(
  707. input_buffer_profile, input_buffer_format,
  708. Our->icc_LinearsRGB, TYPE_RGBA_16,
  709. INTENT_PERCEPTUAL,
  710. 0
  711. );
  712. }
  713. int W = png_get_image_width(png_ptr, info_ptr);
  714. int H = png_get_image_height(png_ptr, info_ptr);
  715. our_EnsureImageBufferOnRead(l,W,H,UseOffsets,StartX,StartY);
  716. for(int i=0;i<H;i++){
  717. png_read_row(png_ptr, &Our->ImageBuffer[((H-i-1+Our->LoadY)*Our->ImageW+Our->LoadX)*4], NULL);
  718. }
  719. our_LayerToTexture(l);
  720. result=1;
  721. cleanup_png_read:
  722. if(input_buffer_profile) cmsCloseProfile(input_buffer_profile);
  723. if(png_ptr && info_ptr) png_destroy_read_struct(&png_ptr,&info_ptr,0);
  724. free(Our->ImageBuffer); Our->ImageBuffer=0;
  725. return result;
  726. }
  727. void our_UiToCanvas(laCanvasExtra* ex, laEvent*e, real* x, real *y){
  728. *x = (real)((real)e->x - (real)(ex->ParentUi->R - ex->ParentUi->L) / 2 - ex->ParentUi->L) * ex->ZoomX + ex->PanX;
  729. *y = (real)((real)(ex->ParentUi->B - ex->ParentUi->U) / 2 - (real)e->y + ex->ParentUi->U) * ex->ZoomY + ex->PanY;
  730. }
  731. void our_PaintResetBrushState(OurBrush* b){
  732. b->BrushRemainingDist = 0; b->SmudgeAccum=0; b->SmudgeRestart=1;
  733. }
  734. real our_PaintGetDabStepDistance(real Size,real DabsPerSize){
  735. real d=Size/DabsPerSize; if(d<1e-2) d=1e-2; return d;
  736. }
  737. int our_PaintGetDabs(OurBrush* b, OurLayer* l, real x, real y, real xto, real yto,
  738. real last_pressure, real last_angle_x, real last_angle_y, real pressure, real angle_x, real angle_y, int *tl, int *tr, int* tu, int* tb){
  739. Our->NextDab=0;
  740. if(!b->EvalDabsPerSize) b->EvalDabsPerSize=b->DabsPerSize;
  741. real size=b->Size; real dd=our_PaintGetDabStepDistance(b->EvalSize, b->EvalDabsPerSize); real len=tnsDistIdv2(x,y,xto,yto); real rem=b->BrushRemainingDist;
  742. real alllen=len+rem; real uselen=dd,step=0; if(!len)return 0; if(dd>alllen){ b->BrushRemainingDist+=len; return 0; }
  743. real xmin=FLT_MAX,xmax=-FLT_MAX,ymin=FLT_MAX,ymax=-FLT_MAX;
  744. b->EvalSize=b->Size; b->EvalHardness=b->Hardness; b->EvalSmudge=b->Smudge; b->EvalSmudgeLength=b->SmudgeResampleLength;
  745. b->EvalTransparency=b->Transparency; b->EvalDabsPerSize=b->DabsPerSize; b->EvalSlender=b->Slender; b->EvalAngle=b->Angle;
  746. if(Our->ResetBrush){ b->LastX=x; b->LastY=y; /*b->EvalLastAngle=atan2(yto-y,xto-x);*/ Our->ResetBrush=0; }
  747. while(1){
  748. arrEnsureLength(&Our->Dabs,Our->NextDab,&Our->MaxDab,sizeof(OurDab)); OurDab* od=&Our->Dabs[Our->NextDab];
  749. real r=tnsGetRatiod(0,len,uselen-rem); od->X=tnsInterpolate(x,xto,r); od->Y=tnsInterpolate(y,yto,r); TNS_CLAMP(r,0,1);
  750. if(b->UseNodes){
  751. b->EvalPressure=tnsInterpolate(last_pressure,pressure,r); b->EvalPosition[0]=od->X; b->EvalPosition[1]=od->Y;
  752. b->EvalPositionOut[0]=od->X; b->EvalPositionOut[1]=od->Y;
  753. b->EvalTilt[0]=tnsInterpolate(last_angle_x,angle_x,r); b->EvalTilt[1]=tnsInterpolate(last_angle_y,angle_y,r);
  754. ourEvalBrush();
  755. TNS_CLAMP(b->EvalSmudge,0,1); TNS_CLAMP(b->EvalSmudgeLength,0,100000); TNS_CLAMP(b->EvalTransparency,0,1); TNS_CLAMP(b->EvalHardness,0,1); TNS_CLAMP(b->DabsPerSize,0,100000);
  756. od->X=b->EvalPositionOut[0]; od->Y=b->EvalPositionOut[1];
  757. }
  758. if(!b->EvalDabsPerSize) b->EvalDabsPerSize=1;
  759. #define pfac(psw) (((!b->UseNodes)&&psw)?tnsInterpolate(last_pressure,pressure,r):1)
  760. od->Size = b->EvalSize*pfac(b->PressureSize); od->Hardness = b->EvalHardness*pfac(b->PressureHardness);
  761. od->Smudge = b->EvalSmudge*pfac(b->PressureSmudge); od->Color[3]=pow(b->EvalTransparency*pfac(b->PressureTransparency),2.718);
  762. tnsVectorSet3v(od->Color,Our->CurrentColor);
  763. #undef pfac;
  764. od->Slender = b->EvalSlender; od->Angle=b->EvalAngle;
  765. b->LastX=od->X; b->LastY=od->Y;
  766. xmin=TNS_MIN2(xmin, od->X-od->Size); xmax=TNS_MAX2(xmax, od->X+od->Size);
  767. ymin=TNS_MIN2(ymin, od->Y-od->Size); ymax=TNS_MAX2(ymax, od->Y+od->Size);
  768. if(od->Size>1e-1) Our->NextDab++;
  769. step=our_PaintGetDabStepDistance(od->Size, b->EvalDabsPerSize);
  770. od->ResampleSmudge=0;
  771. if(b->Smudge>1e-3){ b->SmudgeAccum+=step; if(b->SmudgeAccum>(b->EvalSmudgeLength*od->Size)){ b->SmudgeAccum-=(b->EvalSmudgeLength*od->Size); od->ResampleSmudge=1; } }
  772. if(step+uselen<alllen)uselen+=step; else break;
  773. }
  774. b->BrushRemainingDist=alllen-uselen;
  775. if(Our->NextDab) {
  776. our_LayerEnsureTiles(l,xmin,xmax,ymin,ymax,0,tl,tr,tu,tb);
  777. Our->xmin=TNS_MIN2(Our->xmin,xmin);Our->xmax=TNS_MAX2(Our->xmax,xmax);Our->ymin=TNS_MIN2(Our->ymin,ymin);Our->ymax=TNS_MAX2(Our->ymax,ymax);
  778. return 1;
  779. }
  780. return 0;
  781. }
  782. void our_PaintDoSample(int x, int y, int sx, int sy){
  783. glUniformSubroutinesuiv(GL_COMPUTE_SHADER,1,&Our->RoutineDoSample);
  784. glUniform2i(Our->uBrushCorner,x-sx,y-sy);
  785. glUniform2f(Our->uBrushCenter,x-sx,y-sy);
  786. glDispatchCompute(1,1,1);
  787. glUniformSubroutinesuiv(GL_COMPUTE_SHADER,1,&Our->RoutineDoDabs);
  788. glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
  789. }
  790. void our_PaintDoDab(OurDab* d, int tl, int tr, int tu, int tb){
  791. int corner[2]; corner[0]=floorf(d->X-d->Size); corner[1]=floorf(d->Y-d->Size);
  792. real MaxX,MaxY; MaxX=ceil(d->X+d->Size); MaxY=ceil(d->Y+d->Size);
  793. if(corner[0]>tr||MaxX<tl||corner[1]>tb||MaxY<tu) return;
  794. corner[0]=corner[0]-tl; corner[1]=corner[1]-tu;
  795. float center[2]; center[0]=d->X-tl; center[1]=d->Y-tu;
  796. glUniform2iv(Our->uBrushCorner,1,corner);
  797. glUniform2fv(Our->uBrushCenter,1,center);
  798. glUniform1f(Our->uBrushSize,d->Size);
  799. glUniform1f(Our->uBrushHardness,d->Hardness);
  800. glUniform1f(Our->uBrushSmudge,d->Smudge);
  801. glUniform1f(Our->uBrushSlender,d->Slender);
  802. glUniform1f(Our->uBrushAngle,d->Angle);
  803. glUniform4fv(Our->uBrushColor,1,d->Color);
  804. glDispatchCompute(ceil(d->Size/16), ceil(d->Size/16), 1);
  805. glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
  806. }
  807. void our_PaintDoDabs(OurLayer* l,int tl, int tr, int tu, int tb, int Start, int End){
  808. for(int row=tb;row<=tu;row++){
  809. for(int col=tl;col<=tr;col++){
  810. OurTexTile* ott=l->TexTiles[row][col];
  811. glBindImageTexture(0, ott->Texture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA16);
  812. int sx=l->TexTiles[row][col]->l,sy=l->TexTiles[row][col]->b;
  813. for(int i=Start;i<End;i++){
  814. our_PaintDoDab(&Our->Dabs[i],sx,sx+OUR_TEX_TILE_W,sy,sy+OUR_TEX_TILE_W);
  815. }
  816. }
  817. }
  818. }
  819. STRUCTURE(OurSmudgeSegement){
  820. laListItem Item;
  821. int Start,End,Resample;
  822. };
  823. void our_PaintDoDabsWithSmudgeSegments(OurLayer* l,int tl, int tr, int tu, int tb){
  824. laListHandle Segments={0}; int from=0,to=Our->NextDab; if(!Our->NextDab) return;
  825. OurSmudgeSegement* oss;
  826. oss=lstAppendPointerSized(&Segments, 0,sizeof(OurSmudgeSegement));
  827. for(int i=1;i<to;i++){
  828. if(Our->Dabs[i].ResampleSmudge){ oss->Resample=1;
  829. oss->Start=from; oss->End=i; from=i; oss=lstAppendPointerSized(&Segments, 0,sizeof(OurSmudgeSegement)); }
  830. }
  831. oss->Start=from; oss->End=to;
  832. if(Our->Dabs[0].ResampleSmudge){ ((OurSmudgeSegement*)Segments.pFirst)->Resample=1; }
  833. glUseProgram(Our->CanvasProgram);
  834. while(oss=lstPopItem(&Segments)){
  835. if(oss->Resample || Our->CurrentBrush->SmudgeRestart){
  836. float x=Our->Dabs[oss->Start].X, y=Our->Dabs[oss->Start].Y;
  837. int col=(int)(floor(OUR_TEX_TILE_CTR+x/OUR_TEX_TILE_W_USE+0.5)); TNS_CLAMP(col,0,OUR_TEX_TILES_PER_ROW-1);
  838. int row=(int)(floor(OUR_TEX_TILE_CTR+y/OUR_TEX_TILE_W_USE+0.5)); TNS_CLAMP(row,0,OUR_TEX_TILES_PER_ROW-1);
  839. glBindImageTexture(0, l->TexTiles[row][col]->Texture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA16);
  840. int sx=l->TexTiles[row][col]->l,sy=l->TexTiles[row][col]->b;
  841. glBindImageTexture(1, Our->SmudgeTexture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA16);
  842. our_PaintDoSample(x,y,sx,sy);
  843. Our->CurrentBrush->SmudgeRestart=0;
  844. }
  845. //printf("from to %d %d %d\n", oss->Start,oss->End,Our->Dabs[oss->Start].ResampleSmudge);
  846. our_PaintDoDabs(l,tl,tr,tu,tb,oss->Start,oss->End);
  847. }
  848. }
  849. void our_ReadWidgetColor(laCanvasExtra*e,int x,int y){
  850. uint8_t color[4];
  851. glBindFramebuffer(GL_READ_FRAMEBUFFER, e->OffScr->FboHandle);
  852. glReadBuffer(GL_COLOR_ATTACHMENT0);
  853. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  854. glReadPixels(x,y,1,1, GL_RGBA, GL_UNSIGNED_BYTE, color);
  855. real a=(real)color[3]/255;
  856. Our->CurrentColor[0]=(real)color[0]/255*a;
  857. Our->CurrentColor[1]=(real)color[1]/255*a;
  858. Our->CurrentColor[2]=(real)color[2]/255*a;
  859. tns2LinearsRGB(Our->CurrentColor);
  860. }
  861. void our_StartCropping(OurCanvasDraw* cd){
  862. if(cd->CanvasDownX<Our->X){
  863. if(cd->CanvasDownY<Our->Y-Our->H){ cd->AtCrop=OUR_AT_CROP_BL; }
  864. elif(cd->CanvasDownY>=Our->Y-Our->H&&cd->CanvasDownY<=Our->Y){ cd->AtCrop=OUR_AT_CROP_L; }
  865. elif(cd->CanvasDownY>Our->Y){ cd->AtCrop=OUR_AT_CROP_UL; }
  866. }elif(cd->CanvasDownX>=Our->X&&cd->CanvasDownX<=Our->X+Our->W){
  867. if(cd->CanvasDownY<Our->Y-Our->H){ cd->AtCrop=OUR_AT_CROP_B; }
  868. elif(cd->CanvasDownY>=Our->Y-Our->H&&cd->CanvasDownY<=Our->Y){ cd->AtCrop=OUR_AT_CROP_CENTER; }
  869. elif(cd->CanvasDownY>Our->Y){ cd->AtCrop=OUR_AT_CROP_U; }
  870. }elif(cd->CanvasDownX>Our->X+Our->W){
  871. if(cd->CanvasDownY<Our->Y-Our->H){ cd->AtCrop=OUR_AT_CROP_BR; }
  872. elif(cd->CanvasDownY>=Our->Y-Our->H&&cd->CanvasDownY<=Our->Y){ cd->AtCrop=OUR_AT_CROP_R; }
  873. elif(cd->CanvasDownY>Our->Y){ cd->AtCrop=OUR_AT_CROP_UR; }
  874. }
  875. }
  876. void our_DoCropping(OurCanvasDraw* cd, real x, real y){
  877. int dx=x-cd->CanvasLastX, dy=y-cd->CanvasLastY;
  878. if(cd->AtCrop==OUR_AT_CROP_B||cd->AtCrop==OUR_AT_CROP_BL||cd->AtCrop==OUR_AT_CROP_BR){ Our->H-=dy; }
  879. if(cd->AtCrop==OUR_AT_CROP_U||cd->AtCrop==OUR_AT_CROP_UL||cd->AtCrop==OUR_AT_CROP_UR){ Our->Y+=dy; Our->H+=dy; }
  880. if(cd->AtCrop==OUR_AT_CROP_L||cd->AtCrop==OUR_AT_CROP_BL||cd->AtCrop==OUR_AT_CROP_UL){ Our->X+=dx; Our->W-=dx; }
  881. if(cd->AtCrop==OUR_AT_CROP_R||cd->AtCrop==OUR_AT_CROP_BR||cd->AtCrop==OUR_AT_CROP_UR){ Our->W+=dx; }
  882. if(cd->AtCrop==OUR_AT_CROP_CENTER){ Our->Y+=dy; Our->X+=dx; }
  883. if(Our->W<32) Our->W=32; if(Our->H<32) Our->H=32;
  884. cd->CanvasLastX+=dx; cd->CanvasLastY+=dy;
  885. }
  886. int ourinv_NewLayer(laOperator* a, laEvent* e){
  887. our_NewLayer("Our Layer"); laNotifyUsers("our.canvas.layers");
  888. return LA_FINISHED;
  889. }
  890. int ourinv_RemoveLayer(laOperator* a, laEvent* e){
  891. OurLayer* l=a->This?a->This->EndInstance:0; if(!l) return LA_CANCELED;
  892. our_RemoveLayer(l); laNotifyUsers("our.canvas.layers");
  893. return LA_FINISHED;
  894. }
  895. int ourinv_MoveLayer(laOperator* a, laEvent* e){
  896. OurLayer* l=a->This?a->This->EndInstance:0; if(!l) return LA_CANCELED;
  897. char* direction=strGetArgumentString(a->ExtraInstructionsP,"direction");
  898. if(strSame(direction,"up")&&l->Item.pPrev){ lstMoveUp(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  899. elif(l->Item.pNext){ lstMoveDown(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  900. return LA_FINISHED;
  901. }
  902. int ourinv_ExportLayer(laOperator* a, laEvent* e){
  903. OurLayer* ol=a->This?a->This->EndInstance:0; if(!ol) ol=Our->CurrentLayer; if(!ol) return LA_FINISHED;
  904. laInvoke(a, "LA_file_dialog", e, 0, "warn_file_exists=true;", 0);
  905. return LA_RUNNING;
  906. }
  907. int ourmod_ExportLayer(laOperator* a, laEvent* e){
  908. OurLayer* ol=a->This?a->This->EndInstance:0; if(!ol) ol=Our->CurrentLayer; if(!ol) return LA_FINISHED;
  909. if (a->ConfirmData){
  910. if (a->ConfirmData->StrData){
  911. if(!our_LayerEnsureImageBuffer(ol, 0)) return LA_FINISHED;
  912. FILE* fp=fopen(a->ConfirmData->StrData,"wb");
  913. if(!fp) return LA_FINISHED;
  914. our_LayerToImageBuffer(ol, 0);
  915. our_ImageExportPNG(fp, 0, 0, 0, 0);
  916. fclose(fp);
  917. }
  918. return LA_FINISHED;
  919. }
  920. return LA_RUNNING;
  921. }
  922. int ourinv_ImportLayer(laOperator* a, laEvent* e){
  923. OurLayer* ol=a->This?a->This->EndInstance:0;
  924. laInvoke(a, "LA_file_dialog", e, 0, 0, 0);
  925. return LA_RUNNING;
  926. }
  927. int ourmod_ImportLayer(laOperator* a, laEvent* e){
  928. OurLayer* ol=a->This?a->This->EndInstance:0;
  929. if (a->ConfirmData){
  930. if (a->ConfirmData->StrData){
  931. FILE* fp=fopen(a->ConfirmData->StrData,"rb");
  932. if(!fp) return LA_FINISHED;
  933. if(!ol) ol=our_NewLayer("Imported");
  934. our_LayerImportPNG(ol, fp, 0, 0, 0, 0, 0);
  935. fclose(fp);
  936. }
  937. return LA_FINISHED;
  938. }
  939. return LA_RUNNING;
  940. }
  941. int ourinv_ExportImage(laOperator* a, laEvent* e){
  942. OurLayer* ol=a->This?a->This->EndInstance:0; if(!ol) ol=Our->CurrentLayer; if(!ol) return LA_FINISHED;
  943. laInvoke(a, "LA_file_dialog", e, 0, "warn_file_exists=true;", 0);
  944. return LA_RUNNING;
  945. }
  946. int ourmod_ExportImage(laOperator* a, laEvent* e){
  947. OurLayer* ol=a->This?a->This->EndInstance:0; if(!ol) ol=Our->CurrentLayer; if(!ol) return LA_FINISHED;
  948. if (a->ConfirmData){
  949. if (a->ConfirmData->StrData){
  950. if(!our_CanvasEnsureImageBuffer()) return LA_FINISHED;
  951. FILE* fp=fopen(a->ConfirmData->StrData,"wb");
  952. if(!fp) return LA_FINISHED;
  953. our_CanvasFillImageBufferBackground();
  954. for(OurLayer* l=Our->Layers.pLast;l;l=l->Item.pPrev){
  955. our_LayerToImageBuffer(ol, 1);
  956. }
  957. our_ImageExportPNG(fp, 0, 0, 0, Our->ShowBorder);
  958. fclose(fp);
  959. }
  960. return LA_FINISHED;
  961. }
  962. return LA_RUNNING;
  963. }
  964. int ourinv_NewBrush(laOperator* a, laEvent* e){
  965. our_NewBrush("Our Brush",15,0.95,9,0.5,0.5,5,0,0,0,0);
  966. laNotifyUsers("our.tools.current_brush"); laNotifyUsers("our.tools.brushes"); laRecordInstanceDifferences(Our,"our_tools"); laPushDifferences("Add brush",0);
  967. return LA_FINISHED;
  968. }
  969. int ourinv_RemoveBrush(laOperator* a, laEvent* e){
  970. OurBrush* b=a->This?a->This->EndInstance:0; if(!b) return LA_CANCELED;
  971. our_RemoveBrush(b);
  972. laNotifyUsers("our.tools.current_brush"); laNotifyUsers("our.tools.brushes"); laRecordInstanceDifferences(Our,"our_tools"); laPushDifferences("Remove brush",0);
  973. return LA_FINISHED;
  974. }
  975. int ourinv_MoveBrush(laOperator* a, laEvent* e){
  976. OurBrush* b=a->This?a->This->EndInstance:0; if(!b) return LA_CANCELED;
  977. char* direction=strGetArgumentString(a->ExtraInstructionsP,"direction");
  978. if(strSame(direction,"up")&&b->Item.pPrev){ lstMoveUp(&Our->Brushes, b); }
  979. elif(b->Item.pNext){ lstMoveDown(&Our->Brushes, b); }
  980. laNotifyUsers("our.tools.brushes"); laRecordInstanceDifferences(Our,"our_tools"); laPushDifferences("Move brush",0);
  981. return LA_FINISHED;
  982. }
  983. int ourinv_Action(laOperator* a, laEvent* e){
  984. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  985. our_PaintResetBrushState(ob);
  986. real x,y; our_UiToCanvas(&ex->Base,e,&x,&y); ex->CanvasLastX=x;ex->CanvasLastY=y;ex->LastPressure=e->Pressure;ex->LastTilt[0]=e->AngleX;ex->LastTilt[1]=e->AngleY;
  987. ex->CanvasDownX=x; ex->CanvasDownY=y;
  988. Our->ActiveTool=Our->Tool; Our->CurrentScale = 1.0f/ex->Base.ZoomX;
  989. Our->xmin=FLT_MAX;Our->xmax=-FLT_MAX;Our->ymin=FLT_MAX;Our->ymax=-FLT_MAX; Our->ResetBrush=1;
  990. if(Our->ActiveTool==OUR_TOOL_CROP){ if(!Our->ShowBorder) return LA_FINISHED; our_StartCropping(ex); }
  991. return LA_RUNNING;
  992. }
  993. int ourmod_Paint(laOperator* a, laEvent* e){
  994. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  995. if(e->Type==LA_L_MOUSE_UP || e->Type==LA_R_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){
  996. our_RecordUndo(l,Our->xmin,Our->xmax,Our->ymin,Our->ymax);
  997. return LA_FINISHED;
  998. }
  999. if(e->Type==LA_MOUSEMOVE||e->Type==LA_L_MOUSE_DOWN){
  1000. real x,y; our_UiToCanvas(&ex->Base,e,&x,&y);
  1001. int tl,tr,tu,tb;
  1002. if(our_PaintGetDabs(ob,l,ex->CanvasLastX,ex->CanvasLastY,x,y,
  1003. ex->LastPressure,ex->LastTilt[0],ex->LastTilt[1],e->Pressure,e->AngleX,e->AngleY,&tl,&tr,&tu,&tb)){
  1004. our_PaintDoDabsWithSmudgeSegments(l,tl,tr,tu,tb);
  1005. laNotifyUsers("our.canvas"); laMarkMemChanged(Our->CanvasSaverDummyList.pFirst);
  1006. }
  1007. ex->CanvasLastX=x;ex->CanvasLastY=y;ex->LastPressure=e->Pressure;ex->LastTilt[0]=e->AngleX;ex->LastTilt[1]=e->AngleY;
  1008. }
  1009. return LA_RUNNING;
  1010. }
  1011. int ourmod_Crop(laOperator* a, laEvent* e){
  1012. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  1013. if(e->Type==LA_L_MOUSE_UP || e->Type==LA_R_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){ return LA_FINISHED; }
  1014. if(e->Type==LA_MOUSEMOVE||e->Type==LA_L_MOUSE_DOWN){
  1015. real x,y; our_UiToCanvas(&ex->Base,e,&x,&y);
  1016. our_DoCropping(ex,x,y);
  1017. laNotifyUsers("our.canvas"); laMarkMemChanged(Our->CanvasSaverDummyList.pFirst);
  1018. }
  1019. return LA_RUNNING;
  1020. }
  1021. int ourmod_Action(laOperator* a, laEvent* e){
  1022. OurCanvasDraw *ex = a->This?a->This->EndInstance:0; if(!ex) return LA_CANCELED;
  1023. switch(Our->ActiveTool){
  1024. case OUR_TOOL_PAINT: OurLayer* l=Our->CurrentLayer; OurBrush* ob=Our->CurrentBrush; if(!l||!ob) return LA_CANCELED;
  1025. return ourmod_Paint(a,e);
  1026. case OUR_TOOL_CROP:
  1027. return ourmod_Crop(a,e);
  1028. default: return LA_FINISHED;
  1029. }
  1030. return LA_RUNNING;
  1031. }
  1032. int ourinv_PickColor(laOperator* a, laEvent* e){
  1033. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  1034. laUiItem* ui=ex->Base.ParentUi;
  1035. our_ReadWidgetColor(ex, e->x-ui->L, ui->B-e->y); laNotifyUsers("our.current_color");
  1036. return LA_RUNNING;
  1037. }
  1038. int ourmod_PickColor(laOperator* a, laEvent* e){
  1039. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  1040. laUiItem* ui=ex->Base.ParentUi;
  1041. if(e->Type==LA_R_MOUSE_UP || e->Type==LA_L_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){ return LA_FINISHED; }
  1042. if(e->Type==LA_MOUSEMOVE||e->Type==LA_R_MOUSE_DOWN){
  1043. our_ReadWidgetColor(ex, e->x-ui->L, ui->B-e->y); laNotifyUsers("our.current_color");
  1044. }
  1045. return LA_RUNNING;
  1046. }
  1047. void ourget_CanvasIdentifier(void* unused, char* buf, char** ptr){
  1048. *ptr="Main canvas";
  1049. }
  1050. void* ourget_FirstLayer(void* unused, void* unused1){
  1051. return Our->Layers.pFirst;
  1052. }
  1053. void* ourget_FirstBrush(void* unused, void* unused1){
  1054. return Our->Brushes.pFirst;
  1055. }
  1056. void* ourget_our(void* unused, void* unused1){
  1057. return Our;
  1058. }
  1059. void ourget_LayerTileStart(OurLayer* l, int* xy){
  1060. our_LayerEnsureImageBuffer(l, 1); xy[0]=Our->ImageX; xy[1]=Our->ImageY+Our->ImageH;
  1061. }
  1062. void ourset_LayerTileStart(OurLayer* l, int* xy){
  1063. Our->TempLoadX = xy[0]; Our->TempLoadY = xy[1];
  1064. }
  1065. void* ourget_LayerImage(OurLayer* l, int* r_size, int* r_is_copy){
  1066. void* buf=0;
  1067. if(!our_LayerEnsureImageBuffer(l, 0)){ *r_is_copy=0; return 0; }
  1068. our_LayerToImageBuffer(l, 0);
  1069. if(our_ImageExportPNG(0,1,&buf,r_size, 0)){ *r_is_copy=1; return buf; }
  1070. *r_is_copy=0; return buf;
  1071. }
  1072. void ourset_LayerImage(OurLayer* l, void* data, int size){
  1073. if(!data) return;
  1074. our_LayerImportPNG(l, 0, data, 0, 1, Our->TempLoadX, Our->TempLoadY);
  1075. }
  1076. void ourset_LayerMove(OurLayer* l, int move){
  1077. if(move<0 && l->Item.pPrev){ lstMoveUp(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  1078. elif(move>0 && l->Item.pNext){ lstMoveDown(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  1079. }
  1080. void ourset_BrushMove(OurBrush* b, int move){
  1081. if(move<0 && b->Item.pPrev){ lstMoveUp(&Our->Brushes, b); laNotifyUsers("our.tools.brushes"); }
  1082. elif(move>0 && b->Item.pNext){ lstMoveDown(&Our->Brushes, b); laNotifyUsers("our.tools.brushes"); }
  1083. }
  1084. void ourset_BackgroundColor(void* unused, real* arr){
  1085. memcpy(Our->BackgroundColor, arr, sizeof(real)*3); laNotifyUsers("our.canvas");
  1086. }
  1087. void ourset_BorderAlpha(void* unused, real a){
  1088. Our->BorderAlpha=a; laNotifyUsers("our.canvas");
  1089. }
  1090. void ourset_Tool(void* unused, int a){
  1091. Our->Tool=a; laNotifyUsers("our.canvas");
  1092. }
  1093. void ourset_ShowBorder(void* unused, int a){
  1094. Our->ShowBorder=a; laNotifyUsers("our.canvas");
  1095. }
  1096. void ourset_CanvasSize(void* unused, int* wh){
  1097. Our->W=wh[0]; Our->H=wh[1]; if(Our->W<32) Our->W=32; if(Our->H<32) Our->H=32; laNotifyUsers("our.canvas");
  1098. }
  1099. void ourset_CanvasPosition(void* unused, int* xy){
  1100. Our->X=xy[0]; Our->Y=xy[1]; laNotifyUsers("our.canvas");
  1101. }
  1102. void ourset_LayoutPosition(OurLayer* l, int* xy){
  1103. l->OffsetX=xy[0]; l->OffsetY=xy[1]; laNotifyUsers("our.canvas");
  1104. }
  1105. void ourreset_Canvas(OurPaint* op){
  1106. while(op->Layers.pFirst){ our_RemoveLayer(op->Layers.pFirst); }
  1107. }
  1108. void ourpropagate_Tools(OurPaint* p, laUDF* udf, int force){
  1109. for(OurBrush* b=p->Brushes.pFirst;b;b=b->Item.pNext){
  1110. if(force || !laget_InstanceActiveUDF(b)){ laset_InstanceUDF(b, udf); }
  1111. }
  1112. }
  1113. #define OUR_ADD_PRESSURE_SWITCH(p)\
  1114. laAddEnumItemAs(p,"NONE","None","Not using pressure",0,0);\
  1115. laAddEnumItemAs(p,"ENABLED","Enabled","Using pressure",1,0);
  1116. void ourui_MenuButtons(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  1117. laUiList *muil; laColumn *mc,*c = laFirstColumn(uil);
  1118. muil = laMakeMenuPage(uil, c, "File");{
  1119. mc = laFirstColumn(muil);
  1120. laShowLabel(muil, mc, "Our Paint", 0, 0);
  1121. laShowItem(muil, mc, 0, "OUR_export_image");
  1122. laShowSeparator(muil,mc);
  1123. laShowItem(muil, mc, 0, "OUR_export_layer");
  1124. laShowItem(muil, mc, 0, "OUR_import_layer");
  1125. laui_DefaultMenuButtonsFileEntries(muil,pp,actinst,extracol,0);
  1126. }
  1127. muil = laMakeMenuPage(uil, c, "Options"); {
  1128. mc = laFirstColumn(muil); laui_DefaultMenuButtonsOptionEntries(muil,pp,actinst,extracol,0);
  1129. }
  1130. }
  1131. void ourPreFrame(){
  1132. if(MAIN.Drivers->NeedRebuild){ ourRebuildBrushEval(); }
  1133. }
  1134. void ourRegisterEverything(){
  1135. laPropContainer* pc; laKeyMapper* km; laProp* p;
  1136. laCreateOperatorType("OUR_new_layer","New Layer","Create a new layer",0,0,0,ourinv_NewLayer,0,'+',0);
  1137. laCreateOperatorType("OUR_remove_layer","Remove Layer","Remove this layer",0,0,0,ourinv_RemoveLayer,0,L'🗴',0);
  1138. laCreateOperatorType("OUR_move_layer","Move Layer","Remove this layer",0,0,0,ourinv_MoveLayer,0,0,0);
  1139. laCreateOperatorType("OUR_export_layer","Export Layer","Export this layer",0,0,0,ourinv_ExportLayer,ourmod_ExportLayer,L'🖫',0);
  1140. laCreateOperatorType("OUR_import_layer","Import Layer","Import a PNG into a layer",0,0,0,ourinv_ImportLayer,ourmod_ImportLayer,L'🗁',0);
  1141. laCreateOperatorType("OUR_new_brush","New Brush","Create a new brush",0,0,0,ourinv_NewBrush,0,'+',0);
  1142. laCreateOperatorType("OUR_remove_brush","Remove Brush","Remove this brush",0,0,0,ourinv_RemoveBrush,0,L'🗴',0);
  1143. laCreateOperatorType("OUR_move_brush","Move Brush","Remove this brush",0,0,0,ourinv_MoveBrush,0,0,0);
  1144. laCreateOperatorType("OUR_action","Action","Doing action on a layer",0,0,0,ourinv_Action,ourmod_Action,0,LA_EXTRA_TO_PANEL);
  1145. laCreateOperatorType("OUR_pick","Pick color","Pick color on the widget",0,0,0,ourinv_PickColor,ourmod_PickColor,0,LA_EXTRA_TO_PANEL);
  1146. laCreateOperatorType("OUR_export_image","Export Image","Export the image",0,0,0,ourinv_ExportImage,ourmod_ExportImage,L'🖼',0);
  1147. laRegisterUiTemplate("panel_canvas", "Canvas", ourui_CanvasPanel, 0, 0,"Our Paint");
  1148. laRegisterUiTemplate("panel_layers", "Layers", ourui_LayersPanel, 0, 0,0);
  1149. laRegisterUiTemplate("panel_tools", "Tools", ourui_ToolsPanel, 0, 0,0);
  1150. laRegisterUiTemplate("panel_color", "Color", ourui_ColorPanel, 0, 0,0);
  1151. laRegisterUiTemplate("panel_brush_nodes", "Brush Nodes", ourui_BrushPage, 0, 0,0);
  1152. pc=laDefineRoot();
  1153. laAddSubGroup(pc,"our","Our","OurPaint main","our_paint",0,0,0,-1,ourget_our,0,0,0,0,0,0,LA_UDF_SINGLE);
  1154. pc=laAddPropertyContainer("our_paint","Our Paint","OurPaint main",0,0,sizeof(OurPaint),0,0,1);
  1155. laAddSubGroup(pc,"canvas","Canvas","OurPaint canvas","our_canvas",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1156. laAddSubGroup(pc,"tools","Tools","OurPaint tools","our_tools",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  1157. laAddFloatProperty(pc,"current_color","Current Color","Current color used to paint",0,0,0,1,0,0.05,0.8,0,offsetof(OurPaint,CurrentColor),0,0,3,0,0,0,0,0,0,0,LA_PROP_IS_LINEAR_SRGB);
  1158. laAddFloatProperty(pc,"background_color","Background Color","Background color of the canvas",0,0,0,1,0,0.05,0.8,0,offsetof(OurPaint,BackgroundColor),0,0,3,0,0,0,0,ourset_BackgroundColor,0,0,LA_PROP_IS_LINEAR_SRGB);
  1159. laAddFloatProperty(pc,"border_alpha","Border Alpha","Alpha of the border region around the canvas",0,0,0,1,0,0.05,0.5,0,offsetof(OurPaint,BorderAlpha),0,0,0,0,0,0,0,ourset_BorderAlpha,0,0,0);
  1160. p=laAddEnumProperty(pc,"tool","Tool","Tool to use on the canvas",0,0,0,0,0,offsetof(OurPaint,Tool),0,ourset_Tool,0,0,0,0,0,0,0,0);
  1161. laAddEnumItemAs(p,"PAINT","Paint","Paint stuff on the canvas",OUR_TOOL_PAINT,L'🖌');
  1162. laAddEnumItemAs(p,"CROP","Cropping","Crop the focused region",OUR_TOOL_CROP,L'🖼');
  1163. p=laAddEnumProperty(pc,"show_border","Show Border","Whether to show border on the canvas",0,0,0,0,0,offsetof(OurPaint,ShowBorder),0,ourset_ShowBorder,0,0,0,0,0,0,0,0);
  1164. laAddEnumItemAs(p,"FALSE","No","Dont' show border on the canvas",OUR_TOOL_PAINT,L'🖌');
  1165. laAddEnumItemAs(p,"TRUE","Yes","Show border on the canvas",OUR_TOOL_CROP,L'🖼');
  1166. pc=laAddPropertyContainer("our_tools","Our Tools","OurPaint tools",0,0,sizeof(OurPaint),0,0,1);
  1167. laPropContainerExtraFunctions(pc,0,0,0,ourpropagate_Tools,0);
  1168. laAddSubGroup(pc,"brushes","Brushes","Brushes","our_brush",0,0,ourui_Brush,offsetof(OurPaint,CurrentBrush),0,0,0,0,0,0,offsetof(OurPaint,Brushes),0);
  1169. laAddSubGroup(pc,"current_brush","Current Brush","Current brush","our_brush",0,0,0,offsetof(OurPaint,CurrentBrush),ourget_FirstBrush,0,laget_ListNext,0,0,0,0,LA_UDF_REFER);
  1170. pc=laAddPropertyContainer("our_brush","Our Brush","OurPaint brush",0,0,sizeof(OurBrush),0,0,2);
  1171. laAddStringProperty(pc,"name","Name","Name of the layer",0,0,0,0,1,offsetof(OurBrush,Name),0,0,0,0,LA_AS_IDENTIFIER);
  1172. laAddIntProperty(pc,"__move","Move Slider","Move Slider",LA_WIDGET_HEIGHT_ADJUSTER,0,0,0,0,0,0,0,0,0,ourset_LayerMove,0,0,0,0,0,0,0,0,0);
  1173. laAddFloatProperty(pc,"size","Size","Base size(radius) of the brush",0,0,"px",1000,0,1,10,0,offsetof(OurBrush,Size),0,0,0,0,0,0,0,0,0,0,0);
  1174. laAddFloatProperty(pc,"transparency","Transparency","Transparency of a dab",0,0,0,1,0,0.05,0.5,0,offsetof(OurBrush,Transparency),0,0,0,0,0,0,0,0,0,0,0);
  1175. laAddFloatProperty(pc,"hardness","Hardness","Hardness of the brush",0,0,0,1,0,0.05,0.95,0,offsetof(OurBrush,Hardness),0,0,0,0,0,0,0,0,0,0,0);
  1176. laAddFloatProperty(pc,"smudge","Smudge","Smudge of the brush",0,0,0,1,0,0.05,0.95,0,offsetof(OurBrush,Smudge),0,0,0,0,0,0,0,0,0,0,0);
  1177. laAddFloatProperty(pc,"dabs_per_size","Dabs Per Size","How many dabs per size of the brush",0,0,0,0,0,0,0,0,offsetof(OurBrush,DabsPerSize),0,0,0,0,0,0,0,0,0,0,0);
  1178. laAddFloatProperty(pc,"smudge_resample_length","Smudge Resample Length","How long of a distance (based on size) should elapse before resampling smudge",0,0,0,0,0,0,0,0,offsetof(OurBrush,SmudgeResampleLength),0,0,0,0,0,0,0,0,0,0,0);
  1179. laAddFloatProperty(pc,"slender","Slender","Slenderness of the brush",0,0, 0,10,0,0.1,0,0,offsetof(OurBrush,Slender),0,0,0,0,0,0,0,0,0,0,0);
  1180. laAddFloatProperty(pc,"angle","Angle","Angle of the brush",0,0, 0,TNS_PI,-TNS_PI,0.1,0,0,offsetof(OurBrush,Angle),0,0,0,0,0,0,0,0,0,0,LA_RAD_ANGLE);
  1181. p=laAddEnumProperty(pc,"pressure_size","Pressure Size","Use pen pressure to control size",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(OurBrush,PressureSize),0,0,0,0,0,0,0,0,0,0);
  1182. OUR_ADD_PRESSURE_SWITCH(p);
  1183. p=laAddEnumProperty(pc,"pressure_transparency","Pressure Transparency","Use pen pressure to control transparency",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(OurBrush,PressureTransparency),0,0,0,0,0,0,0,0,0,0);
  1184. OUR_ADD_PRESSURE_SWITCH(p);
  1185. p=laAddEnumProperty(pc,"pressure_hardness","Pressure Hardness","Use pen pressure to control hardness",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(OurBrush,PressureHardness),0,0,0,0,0,0,0,0,0,0);
  1186. OUR_ADD_PRESSURE_SWITCH(p);
  1187. p=laAddEnumProperty(pc,"pressure_smudge","Pressure Smudge","Use pen pressure to control smudging",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(OurBrush,PressureSmudge),0,0,0,0,0,0,0,0,0,0);
  1188. OUR_ADD_PRESSURE_SWITCH(p);
  1189. p=laAddEnumProperty(pc,"use_nodes","Use Nodes","Use nodes to control brush dynamics",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(OurBrush,UseNodes),0,0,0,0,0,0,0,0,0,0);
  1190. laAddEnumItemAs(p,"NONE","None","Not using nodes",0,0);
  1191. laAddEnumItemAs(p,"ENABLED","Enabled","Using nodes",1,0);
  1192. laAddSubGroup(pc,"rack_page","Rack Page","Nodes rack page of this brush","la_rack_page",0,0,laui_RackPage,offsetof(OurBrush,Rack),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  1193. laAddOperatorProperty(pc,"move","Move","Move brush","OUR_move_brush",0,0);
  1194. laAddOperatorProperty(pc,"remove","Remove","Remove brush","OUR_remove_brush",L'🗴',0);
  1195. pc=laAddPropertyContainer("our_canvas","Our Canvas","OurPaint canvas",0,0,sizeof(OurPaint),0,0,1);
  1196. laPropContainerExtraFunctions(pc,0,ourreset_Canvas,0,0,0);
  1197. Our->CanvasSaverDummyProp=laPropContainerManageable(pc, offsetof(OurPaint,CanvasSaverDummyList));
  1198. laAddStringProperty(pc,"identifier","Identifier","Canvas identifier placeholder",0,0,0,0,0,0,0,ourget_CanvasIdentifier,0,0,0);
  1199. laAddSubGroup(pc,"layers","Layers","Layers","our_layer",0,0,ourui_Layer,offsetof(OurPaint,CurrentLayer),0,0,0,0,0,0,offsetof(OurPaint,Layers),0);
  1200. laAddSubGroup(pc,"current_layer","Current Layer","Current layer","our_layer",0,0,0,offsetof(OurPaint,CurrentLayer),ourget_FirstLayer,0,laget_ListNext,0,0,0,0,LA_UDF_REFER);
  1201. laAddIntProperty(pc,"size","Size","Size of the cropping area",0,"X,Y","px",0,0,0,2400,0,offsetof(OurPaint,W),0,0,2,0,0,0,0,ourset_CanvasSize,0,0,0);
  1202. laAddIntProperty(pc,"position","Position","Position of the cropping area",0,"X,Y","px",0,0,0,2400,0,offsetof(OurPaint,X),0,0,2,0,0,0,0,ourset_CanvasPosition,0,0,0);
  1203. pc=laAddPropertyContainer("our_layer","Our Layer","OurPaint layer",0,0,sizeof(OurLayer),0,0,1);
  1204. laPropContainerExtraFunctions(pc,ourbeforefree_Layer,ourbeforefree_Layer,0,0,0);
  1205. laAddStringProperty(pc,"name","Name","Name of the layer",0,0,0,0,1,offsetof(OurLayer,Name),0,0,0,0,LA_AS_IDENTIFIER);
  1206. laAddIntProperty(pc,"__move","Move Slider","Move Slider",LA_WIDGET_HEIGHT_ADJUSTER,0,0,0,0,0,0,0,0,0,ourset_LayerMove,0,0,0,0,0,0,0,0,0);
  1207. laAddIntProperty(pc,"offset","Offset","Offset of the layer",0,"X,Y","px",0,0,0,0,0,offsetof(OurLayer,OffsetX),0,0,2,0,0,0,0,ourset_LayoutPosition,0,0,0);
  1208. laAddIntProperty(pc,"tile_start","Tile Start","Tile starting position for loading",0,0,0,0,0,0,0,0,0,0,0,2,0,0,ourget_LayerTileStart,0,ourset_LayerTileStart,0,0,LA_UDF_ONLY);
  1209. laAddRawProperty(pc,"image","Image","The image data of this tile",0,0,ourget_LayerImage,ourset_LayerImage,LA_UDF_ONLY);
  1210. laAddOperatorProperty(pc,"move","Move","Move Layer","OUR_move_layer",0,0);
  1211. laAddOperatorProperty(pc,"remove","Remove","Remove layer","OUR_remove_layer",L'🗴',0);
  1212. laCanvasTemplate* ct=laRegisterCanvasTemplate("our_CanvasDraw", "our_canvas", 0, our_CanvasDrawCanvas, our_CanvasDrawOverlay, our_CanvasDrawInit, la_CanvasDestroy);
  1213. pc = laCanvasHasExtraProps(ct,sizeof(OurCanvasDraw),2);
  1214. km = &ct->KeyMapper;
  1215. laAssignNewKey(km, 0, "LA_2d_view_zoom", LA_KM_SEL_UI_EXTRA, 0, LA_MOUSE_WHEEL_DOWN, 0, "direction=out");
  1216. laAssignNewKey(km, 0, "LA_2d_view_zoom", LA_KM_SEL_UI_EXTRA, 0, LA_MOUSE_WHEEL_UP, 0, "direction=in");
  1217. laAssignNewKey(km, 0, "LA_2d_view_move", LA_KM_SEL_UI_EXTRA, LA_KEY_ALT, LA_L_MOUSE_DOWN, 0, 0);
  1218. laAssignNewKey(km, 0, "LA_2d_view_move", LA_KM_SEL_UI_EXTRA, 0, LA_M_MOUSE_DOWN, 0, 0);
  1219. laAssignNewKey(km, 0, "OUR_action", LA_KM_SEL_UI_EXTRA, 0, LA_L_MOUSE_DOWN, 0, 0);
  1220. laAssignNewKey(km, 0, "OUR_pick", LA_KM_SEL_UI_EXTRA, 0, LA_R_MOUSE_DOWN, 0, 0);
  1221. laSetMenuBarTemplates(ourui_MenuButtons, laui_DefaultMenuExtras, "OurPaint v0.1");
  1222. ourRegisterNodes();
  1223. laSaveProp("our.canvas");
  1224. laSaveProp("our.tools");
  1225. laGetSaverDummy(Our,Our->CanvasSaverDummyProp);
  1226. laSetFrameCallbacks(ourPreFrame,0,0);
  1227. }
  1228. void ourInit(){
  1229. Our=memAcquire(sizeof(OurPaint));
  1230. ourRegisterEverything();
  1231. our_InitColorProfiles();
  1232. char error[1024]; int status;
  1233. Our->SmudgeTexture=tnsCreate2DTexture(GL_RGBA16,256,1,0);
  1234. Our->CanvasShader = glCreateShader(GL_COMPUTE_SHADER);
  1235. const GLchar* source = OUR_CANVAS_SHADER;
  1236. glShaderSource(Our->CanvasShader, 1, &source, NULL);
  1237. glCompileShader(Our->CanvasShader);
  1238. glGetShaderiv(Our->CanvasShader, GL_COMPILE_STATUS, &status);
  1239. if (status == GL_FALSE){
  1240. glGetShaderInfoLog(Our->CanvasShader, sizeof(error), 0, error);
  1241. printf("Compute shader error:\n%s", error);
  1242. glDeleteShader(Our->CanvasShader);
  1243. return -1;
  1244. }
  1245. Our->CanvasProgram = glCreateProgram();
  1246. glAttachShader(Our->CanvasProgram, Our->CanvasShader);
  1247. glLinkProgram(Our->CanvasProgram);
  1248. glGetProgramiv(Our->CanvasProgram, GL_LINK_STATUS, &status);
  1249. if (status == GL_FALSE){
  1250. glGetProgramInfoLog(Our->CanvasProgram, sizeof(error), 0, error);
  1251. printf("Shader Linking error:\n%s", error);
  1252. return 0;
  1253. }
  1254. Our->uBrushCorner=glGetUniformLocation(Our->CanvasProgram,"uBrushCorner");
  1255. Our->uBrushCenter=glGetUniformLocation(Our->CanvasProgram,"uBrushCenter");
  1256. Our->uBrushSize=glGetUniformLocation(Our->CanvasProgram,"uBrushSize");
  1257. Our->uBrushHardness=glGetUniformLocation(Our->CanvasProgram,"uBrushHardness");
  1258. Our->uBrushSmudge=glGetUniformLocation(Our->CanvasProgram,"uBrushSmudge");
  1259. Our->uBrushColor=glGetUniformLocation(Our->CanvasProgram,"uBrushColor");
  1260. Our->uBrushSlender=glGetUniformLocation(Our->CanvasProgram,"uBrushSlender");
  1261. Our->uBrushAngle=glGetUniformLocation(Our->CanvasProgram,"uBrushAngle");
  1262. Our->uBrushRoutineSelection=glGetSubroutineUniformLocation(Our->CanvasProgram, GL_COMPUTE_SHADER, "uBrushRoutineSelection");
  1263. Our->RoutineDoDabs=glGetSubroutineIndex(Our->CanvasProgram, GL_COMPUTE_SHADER, "DoDabs");
  1264. Our->RoutineDoSample=glGetSubroutineIndex(Our->CanvasProgram, GL_COMPUTE_SHADER, "DoSample");
  1265. Our->X=-2800/2; Our->W=2800;
  1266. Our->Y=2400/2; Our->H=2400;
  1267. Our->BorderAlpha=0.6;
  1268. tnsEnableShaderv(T->immShader);
  1269. }