sk1project / uniconvertor

UniConvertor is a cross-platform universal vector graphics translator
https://sk1project.net
GNU Affero General Public License v3.0
155 stars 42 forks source link

CGM --> SVG Conversion fails #43

Open jupeu opened 2 years ago

jupeu commented 2 years ago

UniConvertor 2.0rc5 build 20210831 fails to convert the attached .CGM file:

λ uniconvertor.exe EW02Y.cgm EW02Y.svg
ERROR  | Error while loading "C:\Users\juhau\Downloads\EW02Y.cgm"The file may be corrupted or contains unknown file format.
STOP   | Loading is interrupted

The file uc2.log contents:

 INFO     | 02:32:03 PM | uc2.application --> Translation of "C:\Users\juhau\Downloads\EW02Y.cgm" into "C:\Users\juhau\Downloads\EW02Y.svg"
 INFO     | 02:32:03 PM | uc2.application --> Start to search saver by file extension svg
 INFO     | 02:32:03 PM | uc2.application --> Saver is found for extension svg
 INFO     | 02:32:03 PM | uc2.application --> Start to search for loader by file extension cgm
 INFO     | 02:32:03 PM | uc2.application --> Loader "cgm_loader" is found for C:\Users\juhau\Downloads\EW02Y.cgm
 INFO     | 02:32:03 PM | uc2.application --> Parsing in progress...
 INFO     | 02:32:03 PM | uc2.application --> <CGM> document model is created
 INFO     | 02:32:03 PM | uc2.application --> <CGM> document model is updated successfully
 INFO     | 02:32:03 PM | uc2.application --> <SK2> document model is updated successfully
 ERROR    | 02:32:03 PM | uc2.application --> Error while loading "C:\Users\juhau\Downloads\EW02Y.cgm"The file may be corrupted or contains unknown file format.
 ERROR    | 02:32:03 PM | uc2.cmds.translate --> Error while loading "C:\Users\juhau\Downloads\EW02Y.cgm"The file may be corrupted or contains unknown file format.
Traceback (most recent call last):
  File "/vagrant/uniconvertor-2.0rc5-20210831-win64-portable/libs/uc2/cmds/translate.py", line 89, in convert
  File "/vagrant/uniconvertor-2.0rc5-20210831-win64-portable/libs/uc2/formats/cgm/__init__.py", line 39, in cgm_loader
  File "/vagrant/uniconvertor-2.0rc5-20210831-win64-portable/libs/uc2/formats/cgm/cgm_presenter.py", line 51, in translate_to_sk2
  File "/vagrant/uniconvertor-2.0rc5-20210831-win64-portable/libs/uc2/formats/cgm/cgm_to_sk2.py", line 55, in translate
  File "/vagrant/uniconvertor-2.0rc5-20210831-win64-portable/libs/uc2/formats/cgm/cgm_to_sk2.py", line 73, in process_element
  File "/vagrant/uniconvertor-2.0rc5-20210831-win64-portable/libs/uc2/formats/cgm/cgm_to_sk2.py", line 360, in _maximum_colour_index
  File "/vagrant/uniconvertor-2.0rc5-20210831-win64-portable/libs/uc2/formats/cgm/cgm_const.py", line 445, in create_color_table
ZeroDivisionError: float division by zero

EW02Y.zip

burkaysucu commented 2 years ago

I got the exact same error with many CGM files. Below is the code from cgm_const file from where exception is thrown.

def create_color_table(sz):
    mx = 1
    bs = 0
    while mx < sz:                                             # >> line 423
        mx = mx * 2
        bs = bs + 1
    cb = bs / 3
    tb = bs % 3
    mc = (1 << (cb + tb)) - 1.0
    table = mx * [(0.0, 0.0, 0.0)]
    for i in range(mx):
        j = i + mx - 1
        j = j % mx
        red, grn, blu = 0, 0, 0
        for k in range(cb):
            red = (red << 1) + j % 2
            j = j >> 1
            grn = (grn << 1) + j % 2
            j = j >> 1
            blu = (blu << 1) + j % 2
            j = j >> 1
        tint = j
        red = (red << tb) + tint
        grn = (grn << tb) + tint
        blu = (blu << tb) + tint
        table[i] = (red / mc, grn / mc, blu / mc)    # >> line 445
    return table

When sz is 1, mc gets calculated to be 0. CGM files I use have " maxcolrindex 1;" command. The one @jupeu attached also has it. I guess this can be fixed easily by changing the while loop's condition at line #423 to a "<=" comparison so mc is set to 1 in the erroneous case.