*/}}

ouroperations.c 69 KB

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