How did Brazil gain its independence?

A. It was conquered by France and then liberated.
B. It was freed after a slave uprising overthrew the plantation owners.
C. It was taken away from Spain by the Treaty of Tordesillas.
D. It was declared an independent empire by Portugal's royal family.