Skip to content

Commit f51788e

Browse files
authored
Merge pull request #216 from shritesh/fix-texture-nif
Fix Texture clear Nifs
2 parents a87e6ff + afd83e7 commit f51788e

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

c_src/texture.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ nif_clear_g(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
333333
static ERL_NIF_TERM
334334
nif_clear_ga(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
335335
ErlNifBinary pixels;
336-
unsigned int size;
337336
unsigned int g;
338337
unsigned int a;
339338

@@ -343,8 +342,7 @@ nif_clear_ga(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
343342
if ( !enif_get_uint(env, argv[2], &a) ) {return enif_make_badarg(env);}
344343

345344
// clear the pixels
346-
size = pixels.size / 2;
347-
for( unsigned int i = 0; i < size; i += 2) {
345+
for( unsigned int i = 0; i < pixels.size; i += 2) {
348346
pixels.data[i] = g;
349347
pixels.data[i+1] = a;
350348
}
@@ -356,7 +354,6 @@ nif_clear_ga(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
356354
static ERL_NIF_TERM
357355
nif_clear_rgb(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
358356
ErlNifBinary pixels;
359-
unsigned int size;
360357
unsigned int r;
361358
unsigned int g;
362359
unsigned int b;
@@ -368,8 +365,7 @@ nif_clear_rgb(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
368365
if ( !enif_get_uint(env, argv[3], &b) ) {return enif_make_badarg(env);}
369366

370367
// clear the pixels
371-
size = pixels.size / 3;
372-
for( unsigned int i = 0; i < size; i += 3) {
368+
for( unsigned int i = 0; i < pixels.size; i += 3) {
373369
pixels.data[i] = r;
374370
pixels.data[i+1] = g;
375371
pixels.data[i+2] = b;
@@ -382,7 +378,6 @@ nif_clear_rgb(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
382378
static ERL_NIF_TERM
383379
nif_clear_rgba(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
384380
ErlNifBinary pixels;
385-
unsigned int size;
386381
unsigned int r;
387382
unsigned int g;
388383
unsigned int b;
@@ -396,8 +391,7 @@ nif_clear_rgba(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
396391
if ( !enif_get_uint(env, argv[4], &a) ) {return enif_make_badarg(env);}
397392

398393
// clear the pixels
399-
size = pixels.size / 4;
400-
for( unsigned int i = 0; i < size; i += 4) {
394+
for( unsigned int i = 0; i < pixels.size; i += 4) {
401395
pixels.data[i] = r;
402396
pixels.data[i+1] = g;
403397
pixels.data[i+2] = b;

test/utilities/texture_test.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,21 +255,25 @@ defmodule Scenic.Utilities.TextureTest do
255255
tex = Texture.clear!(tex, 4)
256256
assert Texture.get(tex, 1, 1) == 4
257257
assert Texture.get(tex, 1, 2) == 4
258+
assert Texture.get(tex, 10, 12) == 4
258259
end
259260

260261
test "clear! works with g textures uses black by default" do
261262
tex =
262263
Texture.build!(:g, @width, @height)
263264
|> Texture.put!(1, 1, 7)
264265
|> Texture.put!(1, 2, 9)
266+
|> Texture.put!(10, 12, 11)
265267

266268
assert Texture.get(tex, 1, 1) == 7
267269
assert Texture.get(tex, 1, 2) == 9
270+
assert Texture.get(tex, 10, 12) == 11
268271
assert Texture.get(tex, 1, 3) == 0
269272

270273
tex = Texture.clear!(tex)
271274
assert Texture.get(tex, 1, 1) == 0
272275
assert Texture.get(tex, 1, 2) == 0
276+
assert Texture.get(tex, 10, 12) == 0
273277
assert Texture.get(tex, 1, 3) == 0
274278
end
275279

@@ -278,80 +282,96 @@ defmodule Scenic.Utilities.TextureTest do
278282
Texture.build!(:g, @width, @height, clear: :dark_khaki)
279283
|> Texture.put!(1, 1, 7)
280284
|> Texture.put!(1, 2, 9)
285+
|> Texture.put!(10, 12, 11)
281286

282287
assert Texture.get(tex, 1, 1) == 7
283288
assert Texture.get(tex, 1, 2) == 9
289+
assert Texture.get(tex, 10, 12) == 11
284290
assert Texture.get(tex, 1, 3) == 159
285291

286292
tex = Texture.clear!(tex)
287293
assert Texture.get(tex, 1, 1) == 159
288294
assert Texture.get(tex, 1, 2) == 159
295+
assert Texture.get(tex, 10, 12) == 159
289296
assert Texture.get(tex, 1, 3) == 159
290297
end
291298

292299
test "clear! works with ga textures" do
293300
{:ok, tex} = Texture.build(:ga, @width, @height, clear: {3, 7})
294301
assert Texture.get(tex, 1, 1) == {3, 7}
295302
assert Texture.get(tex, 1, 2) == {3, 7}
303+
assert Texture.get(tex, 10, 12) == {3, 7}
296304

297305
tex = Texture.clear!(tex, {4, 8})
298306
assert Texture.get(tex, 1, 1) == {4, 8}
299307
assert Texture.get(tex, 1, 2) == {4, 8}
308+
assert Texture.get(tex, 10, 12) == {4, 8}
300309
end
301310

302311
test "clear! works with ga textures uses black by default" do
303312
tex =
304313
Texture.build!(:ga, @width, @height)
305314
|> Texture.put!(1, 1, {3, 7})
306315
|> Texture.put!(1, 2, {4, 8})
316+
|> Texture.put!(10, 12, {6, 9})
307317

308318
assert Texture.get(tex, 1, 1) == {3, 7}
309319
assert Texture.get(tex, 1, 2) == {4, 8}
320+
assert Texture.get(tex, 10, 12) == {6, 9}
310321
assert Texture.get(tex, 1, 3) == {0, 0xFF}
311322

312323
tex = Texture.clear!(tex)
313324
assert Texture.get(tex, 1, 1) == {0, 0xFF}
314325
assert Texture.get(tex, 1, 2) == {0, 0xFF}
326+
assert Texture.get(tex, 10, 12) == {0, 0xFF}
315327
end
316328

317329
test "clear! works with ga textures uses texture clear by default" do
318330
tex =
319331
Texture.build!(:ga, @width, @height, clear: :dark_khaki)
320332
|> Texture.put!(1, 1, {3, 7})
321333
|> Texture.put!(1, 2, {4, 8})
334+
|> Texture.put!(10, 12, {6, 9})
322335

323336
assert Texture.get(tex, 1, 1) == {3, 7}
324337
assert Texture.get(tex, 1, 2) == {4, 8}
338+
assert Texture.get(tex, 10, 12) == {6, 9}
325339
assert Texture.get(tex, 1, 3) == {159, 0xFF}
326340

327341
tex = Texture.clear!(tex)
328342
assert Texture.get(tex, 1, 1) == {159, 0xFF}
329343
assert Texture.get(tex, 1, 2) == {159, 0xFF}
344+
assert Texture.get(tex, 10, 12) == {159, 0xFF}
330345
end
331346

332347
test "clear! works with rgb textures" do
333348
{:ok, tex} = Texture.build(:rgb, @width, @height, clear: {3, 7, 11})
334349
assert Texture.get(tex, 1, 1) == {3, 7, 11}
335350
assert Texture.get(tex, 1, 2) == {3, 7, 11}
351+
assert Texture.get(tex, 10, 12) == {3, 7, 11}
336352

337353
tex = Texture.clear!(tex, {4, 8, 12})
338354
assert Texture.get(tex, 1, 1) == {4, 8, 12}
339355
assert Texture.get(tex, 1, 2) == {4, 8, 12}
356+
assert Texture.get(tex, 10, 12) == {4, 8, 12}
340357
end
341358

342359
test "clear! works with rgb textures uses black by default" do
343360
tex =
344361
Texture.build!(:rgb, @width, @height)
345362
|> Texture.put!(1, 1, {3, 7, 11})
346363
|> Texture.put!(1, 2, {4, 8, 12})
364+
|> Texture.put!(10, 12, {6, 9, 11})
347365

348366
assert Texture.get(tex, 1, 1) == {3, 7, 11}
349367
assert Texture.get(tex, 1, 2) == {4, 8, 12}
368+
assert Texture.get(tex, 10, 12) == {6, 9, 11}
350369
assert Texture.get(tex, 1, 3) == {0, 0, 0}
351370

352371
tex = Texture.clear!(tex)
353372
assert Texture.get(tex, 1, 1) == {0, 0, 0}
354373
assert Texture.get(tex, 1, 2) == {0, 0, 0}
374+
assert Texture.get(tex, 10, 12) == {0, 0, 0}
355375
assert Texture.get(tex, 1, 3) == {0, 0, 0}
356376
end
357377

@@ -360,40 +380,48 @@ defmodule Scenic.Utilities.TextureTest do
360380
Texture.build!(:rgb, @width, @height, clear: :dark_khaki)
361381
|> Texture.put!(1, 1, {3, 7, 11})
362382
|> Texture.put!(1, 2, {4, 8, 12})
383+
|> Texture.put!(10, 12, {6, 9, 11})
363384

364385
assert Texture.get(tex, 1, 1) == {3, 7, 11}
365386
assert Texture.get(tex, 1, 2) == {4, 8, 12}
387+
assert Texture.get(tex, 10, 12) == {6, 9, 11}
366388
assert Texture.get(tex, 1, 3) == {189, 183, 107}
367389

368390
tex = Texture.clear!(tex)
369391
assert Texture.get(tex, 1, 1) == {189, 183, 107}
370392
assert Texture.get(tex, 1, 2) == {189, 183, 107}
393+
assert Texture.get(tex, 10, 12) == {189, 183, 107}
371394
assert Texture.get(tex, 1, 3) == {189, 183, 107}
372395
end
373396

374397
test "clear! works with rgba textures" do
375398
{:ok, tex} = Texture.build(:rgba, @width, @height, clear: {3, 7, 11, 13})
376399
assert Texture.get(tex, 1, 1) == {3, 7, 11, 13}
377400
assert Texture.get(tex, 1, 2) == {3, 7, 11, 13}
401+
assert Texture.get(tex, 10, 12) == {3, 7, 11, 13}
378402

379403
tex = Texture.clear!(tex, {4, 8, 12, 14})
380404
assert Texture.get(tex, 1, 1) == {4, 8, 12, 14}
381405
assert Texture.get(tex, 1, 2) == {4, 8, 12, 14}
406+
assert Texture.get(tex, 10, 12) == {4, 8, 12, 14}
382407
end
383408

384409
test "clear! works with rgba textures uses black by default" do
385410
tex =
386411
Texture.build!(:rgba, @width, @height)
387412
|> Texture.put!(1, 1, {3, 7, 11, 13})
388413
|> Texture.put!(1, 2, {4, 8, 12, 14})
414+
|> Texture.put!(10, 12, {6, 9, 11, 13})
389415

390416
assert Texture.get(tex, 1, 1) == {3, 7, 11, 13}
391417
assert Texture.get(tex, 1, 2) == {4, 8, 12, 14}
418+
assert Texture.get(tex, 10, 12) == {6, 9, 11, 13}
392419
assert Texture.get(tex, 1, 3) == {0, 0, 0, 0xFF}
393420

394421
tex = Texture.clear!(tex)
395422
assert Texture.get(tex, 1, 1) == {0, 0, 0, 0xFF}
396423
assert Texture.get(tex, 1, 2) == {0, 0, 0, 0xFF}
424+
assert Texture.get(tex, 10, 12) == {0, 0, 0, 0xFF}
397425
assert Texture.get(tex, 1, 3) == {0, 0, 0, 0xFF}
398426
end
399427

@@ -402,14 +430,17 @@ defmodule Scenic.Utilities.TextureTest do
402430
Texture.build!(:rgba, @width, @height, clear: :dark_khaki)
403431
|> Texture.put!(1, 1, {3, 7, 11, 13})
404432
|> Texture.put!(1, 2, {4, 8, 12, 14})
433+
|> Texture.put!(10, 12, {6, 9, 11, 13})
405434

406435
assert Texture.get(tex, 1, 1) == {3, 7, 11, 13}
407436
assert Texture.get(tex, 1, 2) == {4, 8, 12, 14}
437+
assert Texture.get(tex, 10, 12) == {6, 9, 11, 13}
408438
assert Texture.get(tex, 1, 3) == {189, 183, 107, 255}
409439

410440
tex = Texture.clear!(tex)
411441
assert Texture.get(tex, 1, 1) == {189, 183, 107, 255}
412442
assert Texture.get(tex, 1, 2) == {189, 183, 107, 255}
443+
assert Texture.get(tex, 10, 12) == {189, 183, 107, 255}
413444
assert Texture.get(tex, 1, 3) == {189, 183, 107, 255}
414445
end
415446

0 commit comments

Comments
 (0)