Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

If it ain't Lisp, it's crap

Name: Anonymous 2012-06-24 22:04

Lisp

(defun png-defilter (g s)
  (let* ((w (gfx-w g))
         (h (gfx-h g))
         (c (gfx-c g))
         (s (cons s 0))
         (l (* w c))
         (d (cons (vec (* h l) u1) 0)))
    (dotimes (y h)
      (let* ((f (q s))
             (p  (cons (car d) (cdr d)))  ; prev pixel
             (u  (cons (car d) (- (cdr d) l)))  ; upper row
             (pu (cons (car u) (cdr u)))) ; upper row, prev pixel
        (when (and (= y 0) (case f ((2 3 4) t)))
          (setf p (cons (vec l u1) 0)))
        (case f
          (0 (dotimes (_ l)       (q d (q s)))) ; plaintext
          (1 (dotimes (_ c)       (q d (q s))) ; sub
             (dotimes (_ (- l c)) (q d (+ (q p) (q s)))))
          (2 (dotimes (_ l)       (q d (+ (q u) (q s))))) ; up
          (3 (dotimes (_ c)       (q d (+ (avg     0 (q u)) (q s)))) ; average
             (dotimes (_ (- l c)) (q d (+ (avg (q p) (q u)) (q s)))))
          (4 (dotimes (_ c)       (q d (+ (paeth     0 (q u)      0) (q s)))) ; paeth
             (dotimes (_ (- l c)) (q d (+ (paeth (q p) (q u) (q pu)) (q s)))))
          (otherwise (gfx-load-error "Invalid line filter (~a)" f)))))
    (setf (gfx-d g) (rgb-to-ints c 1 (car d)))))


C/C++

static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon,
                                 size_t bytewidth, unsigned char filterType, size_t length)
{
  /*
  For PNG filter method 0
  unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte,
  the filter works byte per byte (bytewidth = 1)
  precon is the previous unfiltered scanline, recon the result, scanline the current one
  the incoming scanlines do NOT include the filtertype byte, that one is given in the parameter filterType instead
  recon and scanline MAY be the same memory address! precon must be disjoint.
  */

  size_t i;
  switch(filterType)
  {
    case 0:
      for(i = 0; i < length; i++) recon[i] = scanline[i];
      break;
    case 1:
      for(i =         0; i < bytewidth; i++) recon[i] = scanline[i];
      for(i = bytewidth; i <    length; i++) recon[i] = scanline[i] + recon[i - bytewidth];
      break;
    case 2:
      if(precon)
      {
        for(i = 0; i < length; i++) recon[i] = scanline[i] + precon[i];
      }
      else
      {
        for(i = 0; i < length; i++) recon[i] = scanline[i];
      }
      break;
    case 3:
      if(precon)
      {
        for(i =         0; i < bytewidth; i++) recon[i] = scanline[i] + precon[i] / 2;
        for(i = bytewidth; i <    length; i++) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2);
      }
      else
      {
        for(i =         0; i < bytewidth; i++) recon[i] = scanline[i];
        for(i = bytewidth; i <    length; i++) recon[i] = scanline[i] + recon[i - bytewidth] / 2;
      }
      break;
    case 4:
      if(precon)
      {
        for(i = 0; i < bytewidth; i++)
        {
          recon[i] = (scanline[i] + precon[i]); /*paethPredictor(0, precon[i], 0) is always precon[i]*/
        }
        for(i = bytewidth; i < length; i++)
        {
          recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth]));
        }
      }
      else
      {
        for(i = 0; i < bytewidth; i++)
        {
          recon[i] = scanline[i];
        }
        for(i = bytewidth; i < length; i++)
        {
          /*paethPredictor(recon[i - bytewidth], 0, 0) is always recon[i - bytewidth]*/
          recon[i] = (scanline[i] + recon[i - bytewidth]);
        }
      }
      break;
    default: return 36; /*error: unexisting filter type given*/
  }
  return 0;
}

static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp)
{
  /*
  For PNG filter method 0
  this function unfilters a single image (e.g. without interlacing this is called once, with Adam7 seven times)
  out must have enough bytes allocated already, in must have the scanlines + 1 filtertype byte per scanline
  w and h are image dimensions or dimensions of reduced image, bpp is bits per pixel
  in and out are allowed to be the same memory address (but aren't the same size since in has the extra filter bytes)
  */

  unsigned y;
  unsigned char* prevline = 0;

  /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/
  size_t bytewidth = (bpp + 7) / 8;
  size_t linebytes = (w * bpp + 7) / 8;

  for(y = 0; y < h; y++)
  {
    size_t outindex = linebytes * y;
    size_t inindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/
    unsigned char filterType = in[inindex];

    unsigned error = unfilterScanline(&out[outindex], &in[inindex + 1], prevline, bytewidth, filterType, linebytes);
    if(error) return error;

    prevline = &out[outindex];
  }

  return 0;
}

Name: Anonymous 2012-06-24 22:27

I like the latter version.  It has comments.

Name: Anonymous 2012-06-24 23:20

I'm with >>2
maybe if the lisp code were commented it'd be a bit better

it does look nice though

Name: Anonymous 2012-06-25 0:15

>>3
I think all it needs is more obvious variable names. Granted this would make the lines longer, and with lisp, it becomes harder to read the syntax when the names are long. And it isn't fun to reason about code that uses cons as a generic data structure, but this is short enough to be ok.

Name: Anonymous 2012-06-25 1:14

LOC does not determine code quality.

Name: Anonymous 2012-06-25 3:55

>>5
yes it does.

signed,
an exhausted C++ programmer.

Name: Anonymous 2012-06-25 5:28

>>4
uses cons as a generic data structure
are there other way to create a pointer to array? I think compiler will optimize CONS on stack.

Name: Anonymous 2012-06-25 6:06

#define _albanian(a, b) a ## b
#define albanian(a, b) _albanian(a, b)
#define sausage print
#define shape f
#define strnig(x) #x
#define strjew ","
#define strchink " "

int main()
{
  albanian(sausage, shape)(strnig(Hello) strjew strchink strnig(world!));
  return 0;
}

Name: Anonymous 2012-06-25 6:06

>>7
it would have to be a really smart compiler

Name: Anonymous 2012-06-25 6:10

Common Lisp has symbols. SEPPLES has abominations like http://www.humus.name/index.php?ID=296.

Name: Anonymous 2012-06-25 6:14

try using C, the preprocessor and K&R style next time

Name: Anonymous 2012-06-25 8:50

>>10
Has he never heard of templates?

Name: Anonymous 2012-06-25 9:28

>>10
what if compiler changes?

Name: Anonymous 2012-06-25 10:33

>>11
why not use an enum?

Name: Anonymous 2012-06-25 11:39

Damn. When I need universally unique identifiers for objects in a program, I use universally unique identifiers...

Name: Anonymous 2012-06-25 12:16

IJ鑥镖蠦w熓戴奈ᙃ䒅聂㒈葵♶ᙑ䄴ቁ煠蘧錧ᦀ碙祖萐吅䞑茥瞖㠁㥡ڄ夂顢祐㍘傅码杒ᢗ千ࡢ⌳瞁灄錅䡴虙ᑇ蒇䂖ℂ栧ĵ၅ဤ靆䝧匰䌕呶㜖蜃戱もၘ㥔陵ځᢁ䁐䑨⠴蔥ጅ舳猆虱率瘲敔✡⦆癸鎐锁劘鈧戓煑䌱ᙷ䒅ሰ薒ę䘒枈愧獈儢聂癰昒➄΅䠤㘑啄耓⥵錡㠥㙘ᥨ阁顖ᅗ顣撒坰蠠ᝥᕈ–挗☓㉥蜖㑙ᖇࡱ㕸আ’靥失䉠ᤡ补挦鎔噥䝠∢鞖妄ᝤ葆桱㖓㘩㌄ᕣ鉄䞄Ŵぴ襓餉搶ဂህ㔗̡祑ᚗ䝤吩䡑吗ᤁ䑅栥ᔇ喐脹坖ᕂ傄∷遢朄〒划冄ធ馁莃荈sᠷܠ鈨ᒘ䑂㒅⦉❅遐鑴圈畵熆䉠略ₕ敳፰ሰ঄茖甓Ⅷ襳䑤ᥰू䁳鈑႘ㄱ‖蘐⍳傆᠕桡锘蝄⍂㕤挐偳㠱栳膃┗鉆䦂灃ㅶ甗牡ᐗᥲ褵慣㡰薒࠸㍩ࡳ͓࢑敤ㄴ犃⍣✡ᢀ用䖘㠒࠸ᆖ蘓╅ɢ栔砉奇袃瑲र獄䑹t全儘腉၅ţ䠳╳袗團怘褰⊃へ餒遳堓慹塙琡祉瘉桃

Name: Anonymous 2012-06-25 12:33

舑晇傃ᅙ䡖嘐⦔瘅坖卣膈ጩ偈Ή㕠䝑㡇ፕং┨٠肓ᙹ朆㉢攣ݑ枇霹失䤓煹椘捖蔑灢ᠳ䕅⁡瀉硘ㄐₔ芔噄䒘顡吨㘑⑗怹݆᠐霑䜠獕㔄酧䌰㠨԰d阒愨ॕ荄傕摇ᢘ膖祂Ʉ頔ࠑ㞔䀠㌳颐ȧ䢇炓搇衠候畵ₐ≀䂂≖͔ᕳ⍕蜑㠡舐④㉨甕暔┲⡠⍂礗萲虑㠕長䁘ပ煶☣挀⠵戸逶虖ᡐ䀒ᅱ䅀→Ґ瀃鉶ㄢ㌣ᑦ朠鉦䅨㉈蕤ሇᖇᘔㄢ✱⠶㑖ʘ餁礱睳夢㥉ᙶͩ礔ጹ禉瑀䦔咅靀㕆㙣玂ᎂ恲砀楤⊀牵㈔⠲⍰啱❇葐⚀䆕ƈ⥱䎓時䠴䞂鞂顄ቂ疄恥塘᥹ក鄇锵〲ᅒ̅树΁夤则煩桑ጉ疆椅倨失㊇䐗呂ኀمㆀई咗瀔☸硙嘥㜳ঙ㔓䄷 䚗鍑ƒ❁堨॰փ㉲ނ‶ѥ⤶覙癱ѹᄈ䁕爔备㘒䝳酅䙶蕱散腓⦑ʐ牀朖ㅠᠠ阤␠焧堄煖桲栠艅⥆饨␃扇ܷ隁⁵虦蘴霅牰録葳䉃邇䙙ぉڕぷ蒀联抗⚙理襔䔓&䝐ₖ蘴ᔤ㎇悈Ձ呥喒₂怹蚃阶袗

Name: Anonymous 2012-06-25 12:47

ڇ薕摩ၖ椶䁈葥᝸舥炕܀鈖刣焦牔肖色⢃瀗䖃牠瘒栩ђH֐剶蜣䎅袘䒉銑领͙的瀩΃णဨ♑⌡瑙ᖃ礘垔錔栗暉䚑ᤢᝉ桵呤朢᝖№榆┙ሠ酓錁ℳ薗癩ኘ鄉協ԡ襶疀睄Ű┒炇怙眓䤡ぱ琄蘦杕႙扵ࠧ㙃熕㈱ᤇ䄓颃圡腙ℓ┒憗ㅗ奩‥呒蔇垒゙睳ᤣ鍁ŵ䉁傓牣▔戁၆ʗ瑒⠐➐ᙳ⎒倘桇┤⁸┗鐀ᙣ襉掐啩疕䁔奒睃刱蝥锨甁ឈƕ颉㕓皈阳㝢ᠳ堈荔奰肀饀ԩ聀㕧䙴䝢甙塤͓䦃栂▔ᅃ㉥㞉堉防ၡ㜔慖甃睩㌅ቔ饕牲炃砉ᢄᠣ⤄䅐␑嚄卷匕晔३聅梅督战晆⤈碁㘀㦁ޅ摨䅖䄙␃灃ࠦ餔荠ၩ杂⁗䘩ၙ倴楘虉祆睧大㉗ᐕ昡Ձ➁逅腰覅Ɨᜩ⅐ስ፡楅蠰鍓蠳葴╆瞙ᒀ銙䦖頃焄ِ〨敖砓煩【茥᥸昴匀ᝄ蒄扩ᤶ䕤䜙顦噗䠢攢墘蕥⚉㘦萹憔䉱䉈ᥩٕ恂閑䁁睂ᤅІ倀桘ច噸≐撖喘⡩冔枑鑧褳⅒炖㖓犁蔇禆螓ቨ䑲 Ȑ頩

Name: Anonymous 2012-06-25 12:50

>>15
Yes. But languages like C/C++ are trying to convince you, that you dont actually need universally unique identifiers.

It's like communism: when North Korean people say they need this and that, commies respond that people dont really need this or that, and can survive even on grass diet.

This approach was invented to lower consumption by educating people that wanting nourishing breakfast is irrational and bourgeois.

Name: bampu pantsu 2012-06-26 3:00

bampu pantsu

Don't change these.
Name: Email:
Entire Thread Thread List