Braincopterify an image

10

1

Introduction

Braincopter is an esoteric Brainf*** derivative that encodes a Brainf*** program as an image. As a 2D language, it uses two additional commands; they rotate the instruction pointer (initially pointing right) clockwise and counterclockwise. Braincopter is very similar to Brainloller, except that instead of using distinct colours as different commands, it instead calculates the command based on the RGB value of each pixel.

Braincopter calculates the command for each pixel based on the formula N = (65536 * R + 256 * G + B) % 11, where N is the command number. The mapping from command numbers to Brainf*** commands is as follows:

0  >
1  <
2  +
3  -
4  .
5  ,
6  [
7  ]
8  rotate instruction pointer to the right (clockwise)
9  rotate instruction pointer to the left (counterclockwise)
10 NOP

Braincopter is useful for steganography, or the hiding of secret messages in something else, as the colour of each pixel in any given photo can be changed just slightly to give the desired operation. Such changed pictures are often indistinguishable from the originals.

Challenge

Write a program or function that takes an image and a string of brainf*** code as input and produces the original image with the brainf*** code encoded in it.

To do this, take each pixel in the original image and replace it with the RGB value closest to the colour of the original pixel that evaluates to the correct Braincopter instruction. Colour difference for the purposes of this challenge is defined as abs(R1 - R2) + abs(G1 - G2) + abs(B1 - B2). In the case of a tie in which two colours resolving to the same Braincopter command are equally close to the original colour, either can be chosen.

For example, if the original colour is #FF8040 and needs to be modified to produce a '1' instruction in Braincopter, #FF7F40 should be chosen.

Braincopter will exit when the instruction pointer runs off the edge of the image, so we'll keep it on the image by using the 8 and 9 commands (rotate instruction pointer clockwise and counterclockwise respectively). It's easiest to explain the format of the encoding with an example.

For the input +[[->]-[-<]>-]>.>>>>.<<<<-.>>-.>.<<.>>>>-.<<<<<++.>>++. and an 8x9 image, the instructions will be laid out like this (using Brainf*** commands instead of the Braincopter equivalents, and unicode representations of the turns ):

+ [ [ - > ] - ↲
↳ - > ] < - [ ↲
↳ ] > . > > > ↲
↳ < < < < . > ↲
↳ - . > > - . ↲
↳ > . < < . > ↲
↳ > > > - . < ↲
↳ + + < < < < ↲
↳ . > > + + . N

(Where N is a NOP.) As you can see, control flow travels from left to right until it hits the first set of clockwise turns, then travels right to left across the second row, et cetera. Your program must use this control flow. The brainf*** input will always be able to fit in the image; however, you may not assume it will always fit the image exactly. If it's too small, pad it with NOPs; the Braincopter code must still have the clockwise/counterclockwise turns on either side, though.

You may assume that the Brainf*** input will contain only the eight characters ><+-,.[].

Use the official Braincopter interpreter to test the outputs of your program or function.

Test cases

Input:

20x20 square of #EE2056

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

Example output:

20x20 square modified

Braincopter output:

Hello World!

Input:

50x40 Starry Night

>++++++++++[<++++++++++>-]>>>>>>>>>>>>>>>>++++[>++++<-]>[<<<<<<<++>+++>++++>++++++>+++++++>+++++++>++++>-]<++<+++++<++++++++++<+++++++++<++++++<<<<<<<<<<<<<[>+>+>[-]>>>>[-]>[-]<<<<<<<[>>>>>>+>+<<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]+>---[<->[-]]<[>>>>>>.>.>..<<<<<<<<<<<<+<<[-]>>>>>>-]<<<<<[>>>>>+>+<<<<<<-]>>>>>[<<<<<+>>>>>-]+>-----[<->[-]]<[>>>>>>>>>>.<.<..<<<<<<<<<<<<+<[-]>>>>>-]<+>[-]>[-]>[-]<<<[>+>+>+<<<-]>[<+>-]+>----------[<->[-]]<[<<+>[-]>-]>[-]>[-]<<<<[>>+>+>+<<<<-]>>[<<+>>-]+>----------[<->[-]]<[<<<+>[-]>>-][-]>[-]<<<<<[>>>>+>+<<<<<-]>>>>[<<<<+>>>>-]+>[<->[-]]<[[-]>[-]<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<[>++++++++[<++++++>-]<.-.[-]][-]>[-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<[>++++++++[<++++++>-]<.[-]][-]>[-]<<[>+>+<<-]>>[<<+>>-]++++++++[<++++++>-]<.[-]]>>>>.<<<<<<<<<<<-]

Output:

Starry Night modified

Braincopter output:

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fiz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[>>[>]>+<<[<]<-]+<-[-[>-<[-]]>[->>[>]>[>]>>>>>[>[>>]>>>]>[>>]>>>[>[>>]>>>]>[>]<-<[<]<<<<<[<<]>[<<<<<[<<]>]<<<<<[<<]>[<<<<<[<<]>]<<[<]<[<]<]<]>[->>[>]>[>]>>>>>[>[>>]>>>]>[>>]>>>[>[>>]>>>]>[>]+[<]<<<<<[<<]>[<<<<<[<<]>]<<<<<[<<]>[<<<<<[<<]>]<<[<]<[<]<]>>[>]>[>]>>>>>[>[>>]>>>]>[>>]>>>[>[>>]>>>]>]<<<<<<[<<]>[<<<<<[<<]>]<<<<<[<<]>[<<<<<[<<]>]<<[<]>[<+>-]>[>]>>>>>[>[>>]>>>]<<]<<<[<<]>[<<<<<[<<]>]<<[<]<[<]<]>>[>]>]

Output:

Mona Lisa modified

Braincopter output:

This is a Brainf*** self-interpreter. It separates code from input by !; for example, the input ,[.,]!Hello, World!\0 will output Hello, World!, assuming the \0 was replaced by a null byte.

Scoring

This is , so the shortest answer wins. Happy golfing!

Copper

Posted 2016-10-10T17:35:40.247

Reputation: 3 684

It seems that Braincopter makes art. Real art. – Erik the Outgolfer – 2016-10-10T18:26:37.523

Answers

1

Python, 664 656

usage : python snippet.py image.png <bf script>

R=range;_=R(256);a=abs
def C(c):R,G,B=c;return'><+-.,[]RLN'[(65536*R+256*G+B)%11]
def D(c,d):R,G,B=c;F,V,H=d;return a(R-F)+a(G-V)+a(B-H)
def E(c1,i):return min([(c,D(c1,c))for c in[(r,g,b)for r in _ for g in _ for b in _]if C(c)==i],key=lambda x:x[1])[0]
def B(b,w,h):
 x,y,d=0,0,1
 for c in(b[0]+'R'.join(['L'+s for s in[s[::-1]if i%2 else s for i,s in enumerate([(b[1:]+'N'*w*h)[q:q+w-2]for q in R(0,w*h,w-2)])]])[1:])[:w*h][:-1]+'N':
  yield c,(a(x),y)
  x+=d
  if c=='R':x=0;y+=1;d*=-1
import sys
from PIL import Image
I=Image.open(sys.argv[1]).convert('RGB')
W,H=I.size
P=I.load()
for i,p in B(sys.argv[2],W,H):P[p]=E(P[p],i)
I.save(sys.stdout,"PNG")

explanations to come ...

dieter

Posted 2016-10-10T17:35:40.247

Reputation: 2 010