Entries Tagged 'random' ↓
November 12th, 2007 — code, random, life, events
I’ve gotten a few emails about the lack of posts, so rather than reply to everyone, I figure I’ll set it straight:
I’ve been working on a compiler for class. Boom!
Something kinda cool though:
I can take code that looks like this:
extern void print_int(int x);
extern void print_string(char x[]);
int two[2];
int five[5], one;
void love_me(void) {
print_string("You love me, don't you?n");
}
void main( void ) {
two = 4;
love_me();
}
And turn it into code that looks like this:
.data
two: .space 8
five: .space 20
one: .space 4
_string_0: .asciiz "You love me, dont you?n"
.text
jal love_me
la $sp, 4($sp)
love_me:
# Function prologue
sw $fp, -4($sp) # save old $fp
sw $ra, -8($sp) # save return address
la $fp, 0($sp) # set up frame pointer
la $sp, -12($sp) # Update stack pointer and allocate stack frame
# Load variables on to the stack
la $t9, _string_0
sw $t9, -4($sp)
la $sp, -4($sp)
# Call the method
jal print_string
la $sp, 4($sp)
# Function epilogue
la $sp, 0($fp) # Restore stack pointer
lw $ra, -8($sp) # Restore return address
lw $fp, -4($sp) # Restore frame pointer
jr $ra # Return
jal main
la $sp, 4($sp)
main:
# Function prologue
sw $fp, -4($sp) # save old $fp
sw $ra, -8($sp) # save return address
la $fp, 0($sp) # set up frame pointer
la $sp, -12($sp) # Update stack pointer and allocate stack frame
li $4, 4 # Load _tmp_0
sw $4, -16($fp)
lw $4, -16($fp) # Load two
sw $4, two
# Load variables on to the stack
# Call the method
jal love_me
la $sp, 4($sp)
# Function epilogue
la $sp, 0($fp) # Restore stack pointer
lw $ra, -8($sp) # Restore return address
lw $fp, -4($sp) # Restore frame pointer
jr $ra # Return
print_int:
li $v0, 1
lw $a0, 0($sp)
syscall
jr $ra
print_string:
li $v0, 4
lw $a0, 0($sp)
syscall
jr $ra
When I get it done, I’ll open-source it and be back for more ruby joy! So stay tuned amigos!
April 30th, 2007 — code, random, life, ruby, rmagick, tutorial
LA adventures
I have been in Los Angeles for the past several days doing some work with… well, my work. I promised a friend I would help him out with his graphics. Unfortunately, I am on a plane and also on a laptop that does not have photoshop/illustrator so I can’t do any work with it now. I had planned on doing some homework up here, but rather than doing that, I think it’s probably a fun idea to try to see what I can do design-wise with a little bit of code. Ooo, could be fun.
Let’s get started.
First off, the project I am helping him out with is a graphics project. He is assigned to develop and brand a music publishing company for a class of his. We had come up with a name and a graphic, so let’s actually design it.
First things first,

the logo
The name of the company is R3CORDS. We can easily create an image using ruby and rmagick:
require 'rubygems'
require "RMagick"
string = "R3cords".upcase
# prepare drawing surface
text = Magick::Draw.new
text.fill = 'white'
text.pointsize = 40
# Create and composite the images
temp_image = Magick::Image.new(w, h) { self.background_color = "none" }
temp_image = temp_image.annotate(text, 0, 0, 5, 40, string)
# WRITE OUT
temp_image.format = "JPG"
temp_image.write('temp.jpg')
As you can see, we are just creating an RMagick Drawing object, telling it a few things we want to look like, such as the background color of the image. We say we want white text against a “none” (black) color background and then we write the text out at 5 pixels from the left and 40 from the top. The pointsize is 40. Then we write it out to an image and we are done, right?
No way, I’m stuck on a plane and there is so much more we can do, let’s not stop here, ey?
Let’s make the logo a little more “recordy.” We’ll add a small outline to the logo text and give the font a weight that makes it stand out a little more:
text.stroke = 'black'
text.stroke_width = 1
OO, that looks fun. Oh, but there is still more! We are going to put this on top of another image and kind of blend it in. The only way a black background would work is if the image were black, and it’s not (which you’ll see in a few minutes).
So rather than using a black background, we’ll change a few things and make it even cooler with a transparent background and perhaps a shadow.
# prepare drawing surface
text = Magick::Draw.new
text.fill = 'white'
text.pointsize = 40
text.font_weight = 800
text.stroke_width = 0
temp_image = Magick::Image.new(w, h) { self.background_color = "transparent" }
temp_image = temp_image.annotate(text, 0, 0, 5, 40, string)
# Make the shadow
shadow = temp_image
shadow = shadow.colorize(1, 1, 8, Magick::Pixel.new(100, 100, 100, 20)) # shadow color can vary to taste
shadow.background_color = "grey24" # was "none"
shadow.border!(4, 4, "white")
shadow = shadow.blur_image(0, 3) # shadow blurriness can vary according to taste
# Composite image over shadow. The y-axis adjustment can vary according to taste.
temp_image = shadow.composite(temp_image, -amplitude, 0, Magick::OverCompositeOp)
OO, weee. So now we added a shadow over the temp_image and took out the background of the other. In order to get this on top of another image, we have to open the new image and composite it on top.
I think we are close to finished with the logo, but I want to add one more cool thing to it.
I wanted to add some neat funky-looking boxes to make it look tech-cool.
Where do we start? Well, I’ve already done it
# Lets create a cool background behind it
neat = Magick::ImageList.new
neat.new_image(background_image.columns, background_image.rows) {
self.background_color = 'transparent'
}
1.upto(background_image.rows) do |i|
c = Magick::Image.new(1, 1+rand(25)) do |m|
m.background_color = '#cccccc'
end
c.rotate!(i*rand(360))
neat.composite!(c, i*rand(5), i+rand(background_image.rows), Magick::OverCompositeOp)
end
And we are done
The full code is down below.
Finished, just in time for beverage service
Oh, and LA was sweet.
require 'rubygems'
require "RMagick"
w = 200
h = 100
string = "R3cords".upcase
amplitude = h * 0.01 # vary according to taste
wavelength = w * 2
temp_image = Magick::Image.new(w*2, h) { self.background_color = "transparent" }
# Make the shadow
shadow = temp_image
shadow = shadow.colorize(1, 1, 8, "#883333") # shadow color can vary to taste
shadow.border!(4, 1, "transparent")
shadow = shadow.blur_image(0, 10) # shadow blurriness can vary according to taste
temp_image = shadow.composite(temp_image, 0, 0, Magick::OverCompositeOp)
# Let's set this aside for a moment and do..
background_image = Magick::Image.read("bg.jpg").first
temp_image = background_image.composite(temp_image, Magick::SouthEastGravity, Magick::OverCompositeOp)
# Lets create a cool background behind it
neat = Magick::ImageList.new
neat.new_image(background_image.columns, background_image.rows) {
self.background_color = 'transparent'
}
1.upto(background_image.rows) do |i|
c = Magick::Image.new(1, 1+rand(25)) do |m|
m.background_color = '#cccccc'
end
c.rotate!(i*rand(360))
neat.composite!(c, i*rand(5), i+rand(background_image.rows), Magick::OverCompositeOp)
end
temp_image = temp_image.dissolve(neat, 0.2, 4.9)
# prepare drawing surface
text = Magick::Draw.new
text.fill = 'white'
# text.text_antialias = false
text.pointsize = 80
# text.rotation = 0
text.font_family = "Helvetica"
text.font_weight = 800
text.stroke_width = 1
text.stroke = 'black'
text.gravity = Magick::SouthEastGravity
temp_image = temp_image.annotate(text, 0, 0, 0, 0, string)
# WRITE OUT
temp_image.format = "JPG"
# self.rmagick_image = canvas
temp_image.write('temp.jpg')
From:

To:

All done entirely without a photo editor. Just one sweet computer and a kick-butt text editor!
I enjoyed this tutorial. I think I’ll write some more in the future. Let me know if it was helpful.
April 16th, 2007 — Uncategorized, code, random
For a project I am working on, I have had to research a rather antiquated, but still widely used (and not patented by unisys anymore) compression algorithm. It took me a rather long time to actually understand the algorithm and be able to effectively be able to use it.
So, for the future and if anyone is actually going to use the compression scheme again, hopefully this helps you out so you don’t have to wander through the ugly c code.
First, the compression algorithm.
The way LZW works is that it “looks ahead” in the string array to be coded and looks if it has been inserted into the local dictionary.
Meaning…
Let’s say that we have a string
string = “ababa”
Now, with that string, the module will go through each character and “look ahead” to see if it has been encountered before. So… first output would be the number for a, which in our case will be the number 97. That is saved in the dictionary. The second number will be 98 (b). Now, the dictionary will look into the next byte and see if it is in the dictionary. If it is not (like in our case) it will output the first byte to the result. If it is, then it will set the code as the last output byte (the pair of bytes in the dictionary).
See below for the algorithm.
data = "ababa"
codes = []
## encode
dict = {}
prev = nil
newcode = 258
data.each_byte do |byte|
if prev.nil?
prev = byte
next
end
pair = [prev, byte]
if oldcode = dict[pair]
prev = oldcode
else
codes << prev
dict[pair] = newcode
prev = byte
newcode += 1
end
end
if !prev.nil?
codes << prev
end
*Decoding*
This is a little more difficult. In fact, I’m still trying to understand this, but here goes.
First, you want to go through the coded array (In our case, from the encoded string “ababa”, the array would be [97, 98, 258, 97]) and see if we have a previous byte. If we do, then we are going to put whatever the code is and set it as the last byte read. If we don’t then we are going to look through the code and set the coded array into the bytes until we have no more outside the literal 8-bit values.
Meaning…, we are going through the dictionary until we have a code that is a literal.
We takes the bytes are reverse them as they will be the outputted data.
We continue down this path until all the codes are taken care of!
## decode
dict = {}
prev = nil
newcode = 258
codes.each do |oldcode|
if prev.nil?
puts oldcode
prev = oldcode
next
end
bytes = []
code = oldcode
while 256 <= code
code, byte = dict[code]
bytes << byte
end
puts code
bytes.reverse!
bytes.each do |byte|
puts byte
end
if oldcode == newcode
puts oldcode
end
dict[newcode] = [prev, code]
prev = oldcode
newcode += 1
end
I may post the module when I am done with it in a few
April 14th, 2007 — random
So you see someone from across the bar.
She smiles.
You nervously turn away and make a racist joke to your friend.
She hears.
You smile and take a sip of your Corona you didn’t pay for
She turns away.
You find her phone left at the bar and dial her mom.
She is lost
You walk out of the bar with your friend.
Good luck getting to sleep.
April 13th, 2007 — Uncategorized, random
It seems as close as I get to something new, I get so far away.
I think it smells good outside when it rains. It’s also good looking too, especially when someone falls down or is wearing all white.
Teddy bears are for the weak, teddy chain-saws are for the strong
Anyone own a camera around here? Good. I’m typing in my undies
The lighter it gets outside, the louder I turn up my alarm clocks
Fish babies are exciting. Sadly, you can’t pet them, but you can enjoy them. It’s also sad knowing their parents will eat them for dinner… or lunch. I don’t think fish know about brunch (that’s a human thing)
New York City is overrated. The subway is underrated.
Does the CEO of Ford know he’s an enemy of the White House or is he so far hidden behind his money he can’t?
Girls who wear shorts to work are not lawyers
Guys who wear ties to work are not fishermen
April 12th, 2007 — random, life, events
After about 8 months of the site being down…
I am announcing a revamp and restyle of my blog. Go figure.
There will be content here in a few hours, promise.