Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ;*****************************************************************************
  2. ; BMP to RAW convert plugin - for zSea image viewer
  3. ; Copyright (c) 2008-2011, Marat Zakiyanov aka Mario79, aka Mario
  4. ; All rights reserved.
  5. ;
  6. ; Redistribution and use in source and binary forms, with or without
  7. ; modification, are permitted provided that the following conditions are met:
  8. ;        * Redistributions of source code must retain the above copyright
  9. ;          notice, this list of conditions and the following disclaimer.
  10. ;        * Redistributions in binary form must reproduce the above copyright
  11. ;          notice, this list of conditions and the following disclaimer in the
  12. ;          documentation and/or other materials provided with the distribution.
  13. ;        * Neither the name of the <organization> nor the
  14. ;          names of its contributors may be used to endorse or promote products
  15. ;          derived from this software without specific prior written permission.
  16. ;
  17. ; THIS SOFTWARE IS PROVIDED BY Marat Zakiyanov ''AS IS'' AND ANY
  18. ; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. ; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. ; DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  21. ; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. ; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. ; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. ; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. ;*****************************************************************************
  28.  
  29. format MS COFF
  30.  
  31. public EXPORTS
  32.  
  33. section '.flat' code readable align 16
  34.  
  35. ;include 'macros.inc'
  36. include '../../../../macros.inc'
  37. ;---------------------------------------------------------------------
  38. START:
  39.         pushad
  40.         mov     eax,dword [esp+36]
  41.         call    .check_header
  42.         je      @f
  43. .no_bmp_file:
  44.         xor     eax,eax
  45.         mov     [raw_area],eax
  46.         inc     eax     ; data corrupt eax = 1
  47.         jmp     .ret
  48.  
  49. .check_header:
  50.         mov     [pointer],eax
  51.         mov     eax,[eax]
  52.         mov     [image_file],eax
  53.         cmp     [eax],word 'BM'
  54.         ret
  55. ;---------------------------------------------------------------------
  56. @@:
  57.         mov     [MinusHeight],byte 0
  58.         mov     edx,[eax+22]
  59.         test    edx,0x80000000
  60.         jz      @f
  61.         neg     edx
  62.         mov     [eax+22],edx
  63.         mov     [MinusHeight],byte 1
  64. @@:
  65.         xor     edx,edx
  66.         mov     dx,[eax+28]     ; color resolution 1,4,8,16,24,32 bits
  67. ;       mov     [ebx+16],edx    ; store resolution BMP
  68.         mov     [resolution],edx
  69.         mov     ecx,[eax+30]
  70.         mov     [compression],ecx
  71. ;--------------------------------------------------------------------- 
  72.         mov     ecx,[eax+18]    ; width BMP
  73.         imul    ecx,edx ;[resolution]
  74.         mov     edi,ecx
  75.         shr     ecx,3
  76.         test    edi,7
  77.         jz      @f
  78.         inc     ecx
  79. @@:    
  80.         imul    ecx,[eax+22]    ; size of pixels data area
  81. ;--------------------------------------------------------------------- 
  82.         cmp     edx,16
  83.         jae     .get_memory     ; no palette
  84.        
  85.         mov     eax,4
  86.         xchg    cl,dl
  87.         shl     eax,cl
  88.         xchg    cl,dl
  89.         add     ecx,eax         ; palette size
  90. ;--------------------------------------------------------------------- 
  91. .get_memory:
  92.         add     ecx,44          ; header
  93.         mcall   68,12
  94.         cmp     eax,0
  95.         jne     @f
  96.         xor     eax,eax
  97.         mov     [raw_area],eax
  98.         mov     eax,2           ; not enough memory
  99.         jmp     .ret
  100. @@:
  101.         mov     [raw_area],eax
  102. ;--------------------------------------------------------------------- 
  103.         mov     edi,eax
  104.         xor     eax,eax
  105.         shr     ecx,2
  106.         cld
  107.         rep     stosd           ; clear memory
  108. ;---------------------------------------------------------------------
  109. ; Create RAW header
  110. ;---------------------------------------------------------------------
  111.         mov     eax,[raw_area]
  112.         mov     [eax],dword 'RAW '
  113.         mov     ebx,[image_file]
  114. ;--------------------------------------------------------------------- 
  115.         mov     ecx,[ebx+18]    ; width BMP
  116.         mov     [eax+4],ecx     ; width RAW
  117. ;---------------------------------------------------------------------
  118.         mov     ecx,[ebx+22]    ; high BMP
  119.         mov     [eax+8],ecx     ; high RAW
  120. ;---------------------------------------------------------------------
  121.         mov     ecx,[resolution]        ; color resolution BMP
  122.         mov     [eax+12],ecx    ; color resolution RAW 
  123. ;---------------------------------------------------------------------
  124.         mov     ecx,[resolution]
  125.         cmp     ecx,16
  126.         jb      .1
  127.         push    eax
  128.         mov     eax,ecx
  129.         mov     ecx,3
  130.         xor     edx,edx
  131.         div     ecx
  132.         cmp     eax,8
  133.         jbe     @f
  134.         mov     eax,8
  135. @@:
  136.         mov     ecx,eax
  137.         pop     eax
  138. .1:
  139.         mov     [eax+16],cx     ; channel color resolution RAW
  140. ;---------------------------------------------------------------------
  141.         mov     ecx,[resolution]
  142.         cmp     ecx,16
  143.         jae     @f
  144.         mov     [eax+18],word 1 ; channels amount RAW
  145.         jmp     .2
  146. @@:
  147.         mov     [eax+18],word 3 ; channels amount RAW
  148. .2:
  149. ;---------------------------------------------------------------------
  150.         xor     edx,edx
  151.         cmp     ecx,16
  152.         jae     @f
  153.         add     edx,44
  154. @@:
  155.         mov     [eax+20],edx    ; palette pointer (offset from file start)
  156. ;---------------------------------------------------------------------
  157.         mov     ecx,[resolution]
  158.         cmp     ecx,16
  159.         jae     @f
  160.         mov     ecx,[resolution]
  161.         push    eax
  162.         mov     eax,1
  163.         shl     eax,cl
  164.         mov     ecx,eax
  165.         pop     eax
  166.         shl     ecx,2
  167.         jmp     .3
  168. @@:
  169.         xor     ecx,ecx
  170. .3:
  171.         mov     [eax+24],ecx    ; palette area size
  172. ;--------------------------------------------------------------------- 
  173.         add     ecx,dword 44
  174.         mov     [eax+28],ecx    ; pixels pointer (offset from file start)
  175. ;---------------------------------------------------------------------
  176.         mov     ecx,[ebx+18]    ; width BMP
  177.         imul    ecx,[resolution]
  178.         mov     edi,ecx
  179.         shr     ecx,3
  180.         test    edi,7
  181.         jz      @f
  182.         inc     ecx
  183. @@:    
  184.         imul    ecx,[ebx+22]    ; high BMP
  185.         mov     [eax+32],ecx    ; pixels area size
  186. ;--------------------------------------------------------------------- 
  187.         xor     ecx,ecx         ; Stub!!!
  188.         mov     [eax+36],ecx    ; Transparency pointer  (offset from    file    start)
  189.         mov     [eax+40],ecx    ; Transparency area     size
  190.  
  191. ;--------------------------------------------------------------------- 
  192. ; Finish create RAW header
  193. ;--------------------------------------------------------------------- 
  194. ;       mov     ebx,[pointer]
  195. ;       mov     [ebx+4],eax     ; store [soi] pointer of image area
  196. ;       mov     ecx,eax
  197. .convert:
  198.         cmp     [resolution],16
  199.         jae     .no_palette
  200. ;       mov     [ebx+24],eax
  201. ;       mov     edi,eax
  202.         mov     edi,[raw_area]
  203.         mov     edi,[edi+20]    ; palette pointer (offset from file start)
  204.         add     edi,[raw_area]
  205.         mov     esi,[image_file]
  206.         add     esi,54
  207.         mov     ecx,[resolution]
  208.         mov     eax,1
  209.         shl     eax,cl
  210.         mov     ecx,eax
  211.         rep     movsd
  212.  
  213. ;       mov     [ebx+4],edi     ; store [soi] pointer of image area
  214. .no_palette:
  215.         mov     ecx,[raw_area]
  216.         mov     ecx,[ecx+28]    ; pixels pointer (offset from file start)
  217.         add     ecx,[raw_area]
  218. ;---------------------------------------------------------------------
  219.         mov     eax,[image_file]
  220.         mov     ebp,[eax+18]    ; width BMP
  221. ;       mov     [ebx+8],ebp     ; store width
  222.         imul    ebp,[resolution]
  223.         mov     edi,ebp
  224.         shr     ebp,3           ; ebp = size of output scanline
  225.         test    edi,7
  226.         jz      @f
  227.         inc     ebp
  228. @@:    
  229.         mov     eax,[eax+22]    ; high BMP
  230. ;       mov     [ebx+12],eax    ; store high
  231.         dec     eax
  232.         mul     ebp
  233.         add     eax,ecx
  234.        
  235.         mov     edi,ecx
  236.         mov     bl,[MinusHeight]
  237.         test    bl,bl
  238.         jnz     @f
  239.         mov     edi,eax         ; edi points to last scanline
  240. @@:
  241.         mov     esi,[image_file]
  242.         add     esi,[esi+10]    ; start of pixels data
  243.         mov     ebx,[image_file]
  244.         mov     edx,[ebx+22]    ; high BMP
  245.         add     ebx,54
  246.         lea     eax,[ebp*2]
  247.         mov     [delta],eax
  248.         test    edx,edx
  249.         jz      .ret
  250.         jns     @f
  251.         neg     edx
  252.         and     [delta], 0
  253.         mov     edi,ecx
  254. @@:
  255. ;---------------------------------------------------------------------
  256.         cmp     [compression],3
  257.         je      .BI_BITFIELDS   ; @f
  258.         cmp     [compression],2
  259.         je      RLE4
  260.         cmp     [compression],1
  261.         je      RLE8
  262.         cmp     [compression],0
  263.         je      @f
  264.         jmp     .no_bmp_file
  265. ;---------------------------------------------------------------------
  266. @@:
  267.         cmp     [resolution],16
  268.         jne     .continue
  269.         mov     ebx,[raw_area]
  270.         mov     [ebx+12],dword 15
  271.         jmp     .continue
  272. ;--------------------------------------------------------------------- 
  273. .BI_BITFIELDS:
  274.         cmp     [resolution],32
  275.         je      .32
  276.         cmp     [resolution],16
  277.         jne     .continue
  278.         mov     ebx,[raw_area]
  279.         mov     eax,[image_file]
  280.         cmp     [eax+54],dword 0x7C00
  281.         jne     @f
  282.         mov     [ebx+12],dword 15
  283.         jmp     .continue
  284. ;---------------------------------------------------------------------
  285. @@:
  286.         cmp     [eax+54],dword 0xF800
  287.         jne     @f
  288.         mov     [ebx+12],dword 16
  289.         jmp     .continue
  290. ;---------------------------------------------------------------------
  291. @@:
  292.         cmp     [eax+54],dword 0xF00
  293.         jne     .no_bmp_file    ; @f
  294.         mov     [ebx+12],dword 15
  295.        
  296. @@:
  297.         mov     ebx,ebp
  298.         neg     ebx
  299.         and     ebx,3
  300.         mov     [aligner],ebx
  301.         shr     ebp,1
  302.         mov     cl,[MinusHeight]
  303.         test    cl,cl
  304.         jnz     .start_16_1
  305. align   4
  306. .start_16:
  307.         mov     ecx,ebp
  308.         call    .process_16b_x4r4g4b4
  309.         sub     edi,[delta]
  310.         add     esi,[aligner]
  311.         dec     edx
  312.         jnz     .start_16
  313.         jmp     .ret_ok
  314. ;---------------------------------------------------------------------
  315. .process_16b_x4r4g4b4:
  316.         cld
  317. @@:
  318.         lodsw
  319.         xor     ebx,ebx
  320.         mov     bx,ax
  321.         xor     eax,eax
  322.         ror     ebx,8
  323.         and     bl,0xf
  324.         mov     al,bl
  325.         shl     eax,6
  326.         rol     ebx,4
  327.         and     bx,0xf
  328.         shl     bl,1
  329.         add     ax,bx
  330.         shl     eax,5
  331.         rol     ebx,4
  332.         and     bx,0xf
  333.         shl     bl,1
  334.         add     ax,bx
  335.         stosw
  336.         dec     ecx
  337.         jnz     @r
  338.         ret
  339. ;---------------------------------------------------------------------
  340. .start_16_1:    ;negative value of Height
  341.         mov     ecx,ebp
  342.         call    .process_16b_x4r4g4b4
  343.         add     esi,[aligner]
  344.         dec     edx
  345.         jnz     .start_16_1
  346.         jmp     .ret_ok
  347. ;---------------------------------------------------------------------
  348. .32:
  349.         mov     eax,[image_file]
  350.         cmp     [eax+54],dword 0xFF000000
  351.         jne     .no_bmp_file
  352.         shr     ebp,2
  353.  
  354.         mov     al,[MinusHeight]
  355.         test    al,al
  356.         jnz     .start_32_1
  357. align   4
  358. .start_32:
  359.         mov     ecx,ebp
  360. @@:
  361.         cld
  362.         lodsd
  363.         shr     eax,8
  364.         stosd
  365.         dec     ecx
  366.         jnz     @r
  367.         sub     edi,[delta]
  368.         dec     edx
  369.         jnz     .start_32
  370.         jmp     .ret_ok
  371. ;---------------------------------------------------------------------
  372. .start_32_1:    ;negative value of Height
  373.         mov     ecx,ebp
  374.         imul    ecx,edx
  375. @@:
  376.         cld
  377.         lodsd
  378.         shr     eax,8
  379.         stosd
  380.         dec     ecx
  381.         jnz     @r
  382.         jmp     .ret_ok
  383. ;---------------------------------------------------------------------
  384. .continue:
  385.         mov     eax,ebp
  386.         neg     eax
  387.         and     eax,3
  388.         mov     cl,[MinusHeight]
  389.         test    cl,cl
  390.         jz      .start_24
  391.         cld
  392. align   4
  393. @@:     ;negative value of Height
  394.         mov     ecx,ebp
  395.         rep     movsb
  396.         add     esi,eax
  397.         dec     edx
  398.         jnz     @r
  399.         jmp     .ret_ok
  400. ;--------------------------------------------------------------------- 
  401. align   4
  402. .start_24:
  403. @@:
  404.         mov     ecx,ebp
  405.         rep     movsb
  406.         sub     edi,[delta]
  407.         add     esi,eax
  408.         dec     edx
  409.         jnz     .start_24       ; @r
  410. .ret_ok:
  411.         mov     eax,0           ; convert OK
  412. .ret:
  413.         mov     ebx,[pointer]
  414.         mov     [ebx+8],eax     ; store return code
  415.         mov     eax,[raw_area]
  416.         mov     [ebx+4],eax     ; store RAW pointer
  417.         popad
  418.         ret     4
  419. ;---------------------------------------------------------------------
  420. RLE4:
  421.         cmp     [resolution],4
  422.         jne     START.no_bmp_file
  423.         xor     ebx,ebx
  424.         xor     edx,edx
  425. align   4
  426. .start:
  427.         cld
  428.         lodsb
  429.         cmp     al,0
  430.         jnz     .Encoded_Mode
  431.         cld
  432.         lodsb
  433.         cmp     al,0
  434.         jz      .end_line
  435.         cmp     al,1
  436.         jz      START.ret_ok    ; .end_bitmap
  437.         cmp     al,2
  438.         jz      .Delta
  439.        
  440. .Absolute_Mode:
  441.         xor     ecx,ecx
  442.         mov     cl,al
  443.         add     ebx,ecx
  444.         cmp     dl,0
  445.         je      @f
  446.         mov     dh,[esi]
  447.         shr     dh,4
  448.         mov     dl,[edi-1]
  449.         and     dl,11110000b
  450.         add     dl,dh
  451.         mov     [edi-1],dl
  452.         dec     ecx
  453.         xor     edx,edx
  454.         mov     dh,1
  455. @@:
  456.         test    cl,1b
  457.         jz      @f
  458.         inc     ecx
  459.         inc     edx
  460. @@:
  461.         shr     ecx,1
  462.         cmp     dh,1
  463.         je      .1
  464.         mov     eax,ecx
  465.         cld
  466.         rep     movsb
  467.         test    eax,1b
  468.         jz      @f
  469.         inc     esi
  470. @@:    
  471.         jmp     .start
  472. ;---------------------------------------------------------------------
  473. .1:
  474.         mov     dh,cl
  475. @@:
  476.         cld
  477.         lodsb
  478.         mov     ah,[esi]
  479.         shl     al,4
  480.         shr     ah,4
  481.         add     al,ah
  482.         cld
  483.         stosb
  484.         dec     ecx
  485.         jnz     @r
  486.         test    dh,1b
  487.         jz      @f
  488.         inc     esi
  489. @@:    
  490.         xor     dh,dh
  491.         jmp     .start
  492. ;---------------------------------------------------------------------
  493. .Encoded_Mode:
  494.         xor     ecx,ecx
  495.         mov     cl,al
  496.         add     ebx,ecx
  497.         cld
  498.         lodsb
  499.         cmp     dl,0
  500.         je      @f
  501.         rol     al,4
  502.         mov     dh,al
  503.         and     dh,00001111b
  504.         mov     dl,[edi-1]
  505.         and     dl,11110000b
  506.         add     dl,dh
  507.         mov     [edi-1],dl
  508.         dec     ecx
  509.         xor     edx,edx
  510. @@:
  511.         test    cl,1b
  512.         jz      @f
  513.         inc     ecx
  514.         inc     edx
  515. @@:
  516.         shr     ecx,1
  517.         cld
  518.         rep     stosb
  519.         cmp     dl,0
  520.         je      @f
  521.         mov     al,[edi-1]
  522.         and     al,11110000b
  523.         mov     [edi-1],al
  524. @@:
  525.         jmp     .start 
  526. ;---------------------------------------------------------------------
  527. .Delta:
  528.         xor     eax,eax
  529.         cld
  530.         lodsb
  531.         add     ebx,eax
  532.         cmp     dl,0
  533.         je      @f
  534.         dec     eax
  535.         xor     edx,edx
  536. @@:
  537.         test    al,1b
  538.         jz      @f
  539.         inc     eax
  540.         inc     edx
  541. @@:    
  542.         shr     eax,1
  543.         add     edi,eax
  544.         cld
  545.         lodsb
  546.         imul    eax,ebp
  547.         sub     edi,eax
  548.         jmp     .start
  549. ;---------------------------------------------------------------------
  550. .end_line:
  551.         mov     eax,ebp
  552.         test    ebx,1b
  553.         jz      @f
  554.         inc     ebx
  555. @@:
  556.         shr     ebx,1
  557.         sub     eax,ebx
  558.         add     edi,eax
  559.         sub     edi,[delta]
  560.         xor     ebx,ebx
  561.         xor     edx,edx
  562.         jmp     .start
  563. ;---------------------------------------------------------------------
  564. RLE8:
  565.         cmp     [resolution],8
  566.         jne     START.no_bmp_file
  567.         xor     ebx,ebx
  568. align   4
  569. .start:
  570.         cld
  571.         lodsb
  572.         cmp     al,0
  573.         jnz     .Encoded_Mode
  574.         cld
  575.         lodsb
  576.         cmp     al,0
  577.         jz      .end_line
  578.         cmp     al,1
  579.         jz      START.ret_ok    ; .end_bitmap
  580.         cmp     al,2
  581.         jz      .Delta
  582.        
  583. .Absolute_Mode:
  584.         xor     ecx,ecx
  585.         mov     cl,al
  586.         add     ebx,ecx
  587.         mov     eax,ecx
  588.         cld
  589.         rep     movsb
  590.         test    eax,1b
  591.         jz      @f
  592.         inc     esi
  593. @@:    
  594.         jmp     .start
  595. ;---------------------------------------------------------------------
  596. .Encoded_Mode:
  597.         xor     ecx,ecx
  598.         mov     cl,al
  599.         add     ebx,ecx
  600.         cld
  601.         lodsb
  602.         rep     stosb
  603.         jmp     .start
  604. ;---------------------------------------------------------------------
  605. .Delta:
  606.         xor     eax,eax
  607.         cld
  608.         lodsb
  609.         add     edi,eax
  610.         add     ebx,eax
  611.         cld
  612.         lodsb
  613.         imul    eax,ebp
  614.         sub     edi,eax
  615.         jmp     .start
  616. ;---------------------------------------------------------------------
  617. .end_line:
  618.         mov     eax,ebp
  619.         sub     eax,ebx
  620.         add     edi,eax
  621.         xor     ebx,ebx
  622.         sub     edi,[delta]
  623.         jmp     .start
  624. ;---------------------------------------------------------------------
  625. Check_Header:
  626.         pushad
  627.         mov     eax,dword [esp+36]
  628.         call    START.check_header
  629.         jne     START.no_bmp_file
  630.         popad
  631.         ret     4
  632. ;---------------------------------------------------------------------
  633. Associations:
  634.         dd Associations.end - Associations
  635.         db 'BMP',0
  636. .end:
  637.         db 0
  638. ;---------------------------------------------------------------------
  639. align   16
  640. EXPORTS:
  641.         dd szStart, START
  642.         dd szVersion, 0x00010002
  643.         dd szCheck, Check_Header
  644.         dd szAssoc, Associations
  645.         dd 0
  646.  
  647. pointer dd 0
  648. image_file      dd 0
  649. delta           dd 0
  650. aligner dd 0
  651. resolution      dd 0
  652. compression     dd 0
  653. raw_area        dd 0
  654. MinusHeight     db 0
  655.        
  656. szStart db 'START',0
  657. szVersion       db 'version',0
  658. szCheck db 'Check_Header',0
  659. szAssoc db 'Associations',0
  660. ;---------------------------------------------------------------------