Archive for March, 2011

Anti-aliasing in Cocoa OpenGL app using Xcode

Tuesday, March 29th, 2011

It was incredibly difficult to figure this out. I finally found a solution to allowing my OpenGL app to be anti-aliased in an OpenGL context within a Cocoa App’s window using Xcode.

In the end the steps are quite simple. Open you .xib that contains the OpenGL context. Select that container and open View > Utilities > Attributes Inspector

xcode utilities attributes inspector

Then find the Sampling and Anti-Aliasing parameters and make sure they’re both set to something reasonable.

xcode cocoa anti-aliasing

And here’s a screen capture comparing without anti-aliasing (left) and with anti-aliasing (right).
with and without anti-aliasing

Mac OS X Web sharing, .cgi scripts not executing: showing up as plain text

Saturday, March 26th, 2011

I wanted cgi scripts to be executed everywhere in the website I am hosting from a macbook pro. The default, is to have cgi script only execute inside /Library/Webserver/CGI-Executables and not in the main site /Library/Webserver/Documents. So if you have a test script saved in test.cgi:


#!/bin/bash
echo "Content-type: text/html";
echo "";
echo "Hello";

You can put it in /Library/Webserver/CGI-Executables. Then change its permissions:


chmod 755 test.cgi

And see the result at http://localhost/cgi-bin/test.cgi.

If you want to have .cgi scripts run anywhere then you need to change your apache configuration (mine is located at /private/etc/apache2/httpd.conf )

Make sure this line is uncommented:


    AddHandler cgi-script .cgi

And be sure that ExecCGI is added to you options for the /Library/Webserver/Documents directory like this:


<Directory "/Library/WebServer/Documents">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks MultiViews +ExecCGI

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all

</Directory>

Now if you move your test.cgi script to /Library/Webserver/Documents you should be able to see it in action at http://localhost/test.cgi.

BUT for me the script just shows up in the browser in plain text. I searched and searched, but couldn’t figure out what I’d done wrong.

The answer was simple. Restart Apache. You can do this by opening System Preferences > Sharing and toggling off and on “Web Sharing”. Now everything works great!

black table

Wednesday, March 23rd, 2011

black table

http://alecjacobson.com/art/digital/
http://alecjacobson.com/art/

During upgrade to snow leopard stuck on apple logo

Tuesday, March 15th, 2011

Yesterday I finally got around to updating the OS on my macbook pro from Leopard (mac os x 10.5) to Snow Leopard (mac os x 10.6). I had debated waiting until Lion (10.7), set to appear this summer, but while I would probably want to wait until it had stabilized in the fall and applications were already turning their backs on Leopard I decided to just give in early.

I bought the Snow Leopard DVD, inserted it, pushed start and let it go. For about 30 mins it had a standard dialog box and progress bar, reporting that it was busy unpacking and installing. Then it rebooted itself at hung on the apple logo start up screen for 3 hours.
apple logo start up screen

I read a few other reports of this online and it seemed a lot of people had given up sooner than 3 hours and had lots of problems, so I let it go. The computer started getting warm after a few hours: the DVD drive seemed to be busy the entire time. Finally I gave up and held the power button to force a shutdown. When I restarted it never even got to the apple logo, just a grey screen.
grey start up screen

At this point I really gave up. Being in New York, where there is a 24 hour apple store it’s easy to give up. I held the power button down to force another shutdown and biked my laptop to the Genius Bar.

There the clerk told me that the installation had paused for whatever reason only partly done. He force ejected the DVD then plugged in a Firewire drive and finished the installation booting from the Firewire device.

After 45 mins everything was installed and so far everything has been working fine.

Homonym translator

Sunday, March 13th, 2011

I wrote a little ruby program that accepts a string and translates as many words as it can to homonyms.
Save this in a file called homotranslate.rb:


#!/usr/local/bin/ruby -w

class Homo
  def initialize(filename)
    lines = File.open(filename, "r").readlines;
    homonyms = lines.collect{|e| e.strip.split(' ')};
    @homo_map = Hash.new;
    homonyms.each{|h| h.each{|e| @homo_map[e] = h - [e]; }; };
  end

  def random_translate(str)
    str.gsub(/\w+/) do |word| 
      lw = word.downcase;
  
      if(@homo_map[lw].nil? || word.size == 0)
        word;
      else
        homonym = @homo_map[lw][rand(@homo_map[lw].size)];
        if(word[0] >= 'A'[0] and word[0] <= 'Z'[0])
          homonym.capitalize!
        end
        homonym;
      end
    end
  end
end


# Default main program
if $0 == __FILE__
  homo = Homo.new("homonyms.txt");
  puts homo.random_translate(ARGF.read);
end
__END__

Then run with:


echo "I owe you." | ruby homotranslate.rb

Or just try it online now:


Random homonym translator web app
Source
ASCII list of homonyms, one set per line, space separated

Automatically reformat any annoying/badly laid out image gallery on the web

Sunday, March 13th, 2011

Often I come across very annoying javascript image galleries on the web. Like this one, where when you click on a thumbnail a full size image slowly unveils somewhere down the page. You actually have to scroll down to get to it.

So I’m presenting an automatic re-gallery-izing web application.

Turn this …

bad gallery screen capture

into this…

auto gallery dump

There are many other examples. Facebook’s was pretty bad for a while, though I think it’s a little better now. Jezebel and the other Gawker sites have a notoriously bad image gallery. But for me, the most common is artists’ pages. Artists’ image galleries are rarely easy to flip through and that’s usually exactly what I want to do: quickly see a lot of images and occasionally make one bigger and look at it in detail.

I’ve tried to do this with my own galleries. So you can tell me if I’ve got a big stick in my eye.

In any case here’s a proof of concept to hopefully relieve some of this. Just enter the URL of the gallery index page (it needs to be a page that links to all of the images in the gallery, so this won’t work with flash galleries), and I automatically generate a page that simply lists the images so you can quickly view all of them and click on the ones you want to see full size.

Right now the regallery is more or less a raw dump of the images into an html page. There’s no reason it couldn’t be spiced up a little bit, but that’s where all the trouble began in the first place.

Bathroom wall, html5 canvas app

Friday, March 11th, 2011

bathroom wall canvas app

I made a small experiment with the canvas tag. The bulk of the code is in javascript but I have a server-side php script which is used to remember anything that’s written on the wall. I still have to come up with the right amount of persistence. But for now, after a while older scrawls start to disappear.

How to write on the bathroom wall:

DRAG the mouse to write.
HOLD SHIFT and DRAG the mouse to change pens.

Adding html doctype ruins fullscreen canvas

Friday, March 11th, 2011

Recently I experienced a very minor but annoying problem. I made a fullscreen canvas like this one:

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
    <style type="text/css">
    html,body
    {
      height: 100%;
      width: 100%;
      margin: 0;
      background-color: #F54;
    }
    canvas#full
    {
      height: 100%;
      width: 100%;
      margin: 0;
      background-color: #4F5;
    }
    </style>
    <title>Properly fullscreen canvas, but no doctype</title>
  </head>
  <body>
    <canvas id="full">
    </canvas>
  </body>
</html>

which you can see for yourself.

Then all I did was add to the top:

<!DOCTYPE HTML>

and you can see on the right a scroll bar now appears and there are 3 pixels below my canvas showing the background. This is a minor but very annoying problem.

I fixed this by adjusting the CSS style of the fullscreen canvas to:

    canvas#full
    {
      display: block;
      height: 100%;
      width: 100%;
      margin: 0;
      background-color: #4F5;
    } 

The result validates and looks right.

Get largest image from webpage using php, wget and imagemagick

Thursday, March 10th, 2011

Here’s a script I wrote to make autoblog a little more interesting. Now any time a spammer makes a comment it checks the URL they provide for a large image and appends it to their comment when their comment becomes a post.

I use a little php script to get the largest image from the url and return an image tag (as long as the image is big enough). Here it is:


<?php

function isValidURL($url)
{
  return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

function image_from_URL($URL)
{
  $tempdir = "temp_images";
  if(isValidURL($URL))
  {
    `wget -r -l1 -H -t1 -nd -N -np -A jpg,gif,png -P $tempdir -erobots=off $URL`;
    $handle = opendir($tempdir); 
    $max_size = 0;
    $biggest = "";
    while (false !== ($file = readdir($handle)))
    {
      $extension = strtolower(substr(strrchr($file, '.'), 1)); 
      if($extension == 'jpg' || $extension == 'gif' || $extension == 'png')
      {
        // identify from imagemagick can return the w and h as a string "w*h"
        // which then bc can compute as a multiplication giving the area in pixels
        $size = (int) exec(
          "identify -format \"%[fx:w]*%[fx:h]\" \"$tempdir/$file\" | bc", $ret);
        if($size > $max_size)
        {
          $max_size = $size;
          $biggest = $file;
        }
      } 
    } 

   // HERE YOU CAN ADD CODE TO DELETE THE TEMP FILES ETC

    if($max_size >= 80000)
    {
      return "<img src='$tempdir/$biggest' class='center'>";
    }
  }
  return "";
}

isvalidurl source
wget one-liner source

Convert vector graphics into raster without anti-aliasing, using Illustrator and Photoshop

Thursday, March 3rd, 2011

I recently made an image in Photoshop using the Shape Tool. Then I want to save the image at a certain size >b?without anti-aliasing. This turns out to be very difficult. I couldn’t find a way to save/rasterize the shapes I’d already drawn with anti-aliasing turned off just in Photoshop.

The way I finally did it was:

  1. Photoshop: Save the image as a .psd
  2. Illustrator: Loading that .psd and save as a .ai
  3. Photoshop: Load that .ai, when prompted uncheck anti-aliased and pick your size